from odoo import models, fields, api

class SosCaseTransferWizard(models.TransientModel):
    _name = 'sos_case_transfer_wizard'
    _description = 'Transfer Sales Person Wizard'

    case_diary_id = fields.Many2one('sos_case_diary', string="Case Diary", required=True)
    new_sales_person = fields.Many2one('res.users', string="New Sales Person")


    def action_confirm_transfer(self):
        """ Transfers sales person and records history """
        case = self.case_diary_id
        if case.sales_person and case.sales_person.id == self.new_sales_person.id:
            raise ValueError("The new Sales Person should be different from the current Sales Person.")

        # Create Transfer History
        self.env['sos_case_transfer_history'].create({
            'case_diary_id': case.id,
            'previous_sales_person': case.sales_person.id,
            'new_sales_person': self.new_sales_person.id,
            'status':'Waiting For Approval'
        })
        # Email part
        body_html = f"""
        <p>Below <b>Case Diary</b> is waiting for your Approval to Transfer</p>
        """
        subject = f"Case Diary Transfer Approval Request - {case.customer_name.customer_name}"
        send_email = self.env['sos_common_scripts']
        send_email.send_direct_email(self.env,"sos_case_diary",self.case_diary_id.id,"ramachandran.r@sosaley.in",subject,body_html)
        return {
                    'type': 'ir.actions.client',
                    'tag': 'display_notification',
                    'params': {
                        'message': "Request Sent to Management",
                        'type': 'success',
                        'sticky': False
                    }
                }



