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

from odoo import models, fields, api, _


class FusionLTCFormSubmission(models.Model):
    _name = 'fusion.ltc.form.submission'
    _description = 'LTC Form Submission'
    _order = 'submitted_date desc, id desc'

    name = fields.Char(
        string='Reference',
        required=True,
        copy=False,
        readonly=True,
        default=lambda self: _('New'),
    )
    form_type = fields.Selection([
        ('repair', 'Repair Request'),
    ], string='Form Type', default='repair', required=True, index=True)
    repair_id = fields.Many2one(
        'fusion.ltc.repair',
        string='Repair Request',
        ondelete='set null',
        index=True,
    )
    facility_id = fields.Many2one(
        'fusion.ltc.facility',
        string='Facility',
        index=True,
    )
    client_name = fields.Char(string='Client Name')
    room_number = fields.Char(string='Room Number')
    product_serial = fields.Char(string='Product Serial #')
    is_emergency = fields.Boolean(string='Emergency')
    submitted_date = fields.Datetime(
        string='Submitted Date',
        default=fields.Datetime.now,
        readonly=True,
    )
    ip_address = fields.Char(string='IP Address', readonly=True)
    status = fields.Selection([
        ('submitted', 'Submitted'),
        ('processed', 'Processed'),
        ('rejected', 'Rejected'),
    ], string='Status', default='submitted', tracking=True)

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            if vals.get('name', _('New')) == _('New'):
                vals['name'] = self.env['ir.sequence'].next_by_code(
                    'fusion.ltc.form.submission'
                ) or _('New')
        return super().create(vals_list)

    def action_view_repair(self):
        self.ensure_one()
        if not self.repair_id:
            return
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'fusion.ltc.repair',
            'view_mode': 'form',
            'res_id': self.repair_id.id,
        }
