
from odoo import models, fields, api


class SOS_Customers(models.Model):
    _name = 'sos_customers'
    _description = 'Sosaley Customers Management'
    _rec_name="customer_name"
    _sql_constraints = [
        ('unique_customer_product', 'unique(customer_name, vertical_domain,customer_city)', 
         'A customer with the same domain & city already exists!')
    ]
    customer_name = fields.Char(string="Primary Customer Name")
    customer_city = fields.Char(string="City")
    leads_ref_no = fields.Many2one('sos_sales_leads',string="Lead Ref No")
    vertical_domain = fields.Many2one('sos_vertical_domain',string="Domain / Industry", ondelete="cascade")
    correspondence_address = fields.Text(string="Correspondence Address")
    interested_in = fields.Selection(
            [              
                ('products', 'Products'),                                
                ('projects', 'Projects')                   
            ],
            string="Interested In",required=True,default="products")
    project_name = fields.Char(string="Project Name")
    products = fields.Selection(
            [
               ('BHMS 1.2V', 'BHMS 1.2V'), 
                ('BHMS 2V', 'BHMS 2V'), 
                ('BHMS 12V', 'BHMS 12V'),  
                ('BHMS 48V', 'BHMS 48V'),
                ('BMS-HV', 'BMS-HV'),
                ('BMS-LV 100A', 'BMS-LV 100A'), 
                ('BMS-LV 40A', 'BMS-LV 40A'), 
                ('SBMS 55A', 'SBMS 55A'),              
                ('MC 250W', 'MC 250W'),                                
                ('HeartTarang', 'HeartTarang')                  
            ],
            string="Products")
    line_ids_contacts = fields.One2many('sos_customers_line', 'ref_id', string="Contact Details",copy=True)
    reporting_to = fields.Many2one('res.users', string='Reporting To')
    responsible = fields.Many2one('res.users', string='Sales Executive',default=lambda self: self.env.user)
    @api.model
    def create(self, vals):
        create_uid = vals.get('create_uid', self.env.uid)
        create_user = self.env['res.users'].browse(create_uid)
        vals['reporting_to'] = create_user.reporting_to.id 
        return super(SOS_Customers, self).create(vals)
class SOS_Customers_Line(models.Model):
    _name = 'sos_customers_line'
    _description = 'Customer Contact Lines'

    ref_id = fields.Many2one('sos_customers', string="Customers", ondelete="cascade")
    name = fields.Char(string="Name")
    dept = fields.Char(string="Designation")
    email = fields.Char(string="Email Id")
    mobile_number = fields.Char(string="Contact Number")
    set_as_primary = fields.Boolean(string="Set Primary")
    @api.model
    def create(self, vals):
        record = super(SOS_Customers_Line, self).create(vals)
        if not self.env['sos_customers_line'].search([
            ('ref_id', '=', record.ref_id.id),
            ('set_as_primary', '=', True)
        ]):
            # Set the created record as primary
            record.set_as_primary = True
        return record
    def write(self, vals):
        res = super(SOS_Customers_Line, self).write(vals)
        if 'set_as_primary' in vals and vals['set_as_primary']:
            for record in self:
                self.env['sos_customers_line'].search([
                    ('ref_id', '=', record.ref_id.id),
                    ('id', '!=', record.id)
                ]).write({'set_as_primary': False})
        return res
        

    