# -*- coding: utf-8 -*-
# Copyright 2024-2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)

"""
Fusion Technician Task - LTC Extension
Adds LTC facility field and onchange behavior
to the base fusion.technician.task model.
"""

from odoo import models, fields, api, _


class FusionTechnicianTaskLTC(models.Model):
    _inherit = 'fusion.technician.task'

    facility_id = fields.Many2one(
        'fusion.ltc.facility',
        string='LTC Facility',
        tracking=True,
        help='LTC Home for this visit',
    )

    @api.onchange('facility_id')
    def _onchange_facility_id(self):
        """Auto-fill address from the LTC facility."""
        if self.facility_id and self.task_type == 'ltc_visit':
            fac = self.facility_id
            self.address_street = fac.street or ''
            self.address_street2 = fac.street2 or ''
            self.address_city = fac.city or ''
            self.address_state_id = fac.state_id.id if fac.state_id else False
            self.address_zip = fac.zip or ''
            self.description = self.description or _(
                'LTC Visit at %s', fac.name
            )

    @api.onchange('task_type')
    def _onchange_task_type_ltc(self):
        if self.task_type == 'ltc_visit':
            self.sale_order_id = False
            self.purchase_order_id = False
