from odoo import models, fields, api

class MON_MissingComponentWizard(models.TransientModel):
    _name = 'mon_missing_component_wizard'
    _description = 'Wizard for Handling Missing Components'

    missing_component_line_ids = fields.One2many(
        'mon_missing_component_line', 'wizard_id', string="Missing Components"
    )
    mon_id = fields.Many2one('sos_mon', string="Related SFG Record")
    
    def confirm_selections(self):
        mon_record = self.mon_id.id
        line_model = self.env['sos_mon_line_material']
        min_line_model = self.env['sos_min_line_material']
        min_record = self.env['sos_min'].search([('mon_no', '=', mon_record)], limit=1)
        for line in self.missing_component_line_ids:
            if line.alternative_component_id.id:
                line_model.create({'mon_id': mon_record,'com_type':'exits',
                    'component_id':line.alternative_component_id.id,
                    'quantity':line.missing_component_quantity
                    })
                min_line_model.create({'min_id': min_record.id,'com_type':'exits',
                    'component_id':line.alternative_component_id.id,
                    'quantity':line.missing_component_quantity
                    })
            else:
                line_model.create({'mon_id': mon_record,'com_type':'new',
                    'new_component_id':line.missing_component_name,
                    'quantity':line.missing_component_quantity
                    })
                min_line_model.create({'min_id': min_record.id,'com_type':'new',
                    'new_component_id':line.missing_component_name,
                    'quantity':line.missing_component_quantity
                    })

class MON_MissingComponentLine(models.TransientModel):
    _name = 'mon_missing_component_line'
    _description = 'Line for each missing component'

    wizard_id = fields.Many2one('mon_missing_component_wizard', string="Wizard")
    missing_component_name = fields.Char("Missing Component", readonly=True)
    missing_component_quantity=fields.Float(string="Qty", default=1)
    alternative_component_id = fields.Many2one('sos_material', string="Select Alternative")