from odoo import models, fields, api
import random

class sos_sales_leads(models.Model):
    _name = 'sos_sales_leads'
    _description = 'Sosaley Customers Management'
    _rec_name="company_name"

    company_name = fields.Char(string="Company Name")
    transferred_on = fields.Date(string="Transferred On")
    location = fields.Char(string="Location")
    website_url = fields.Char(string="Website URL")
    vertical_domain = fields.Many2many('sos_vertical_domain',string="Domain / Industry", ondelete="cascade")
    linkedin_profile = fields.Char(string="Linkedin profile")
    status = fields.Char(string="Status")
    correspondence_address = fields.Text(string="Correspondence Address")
    interested_in = fields.Selection(
            [              
                ('Product', 'Products'), 
                ('Project', 'Projects')                 
            ],
            string="Service Type",required=True,default="Product")
    sales_type = fields.Selection(
            [
                ('Domestic', 'Domestic'), 
                ('International', 'International')               
            ],
            string="Sales Type",default="Domestic")
    project_name= fields.Many2one('sos_projects',string="Project Name")
    products_interested = fields.Many2many('sos_products',string="Product Interested In")
    # 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")
    country = fields.Many2one(
    'res.country',
    string='Country',
    default=lambda self: self.env['res.country'].search([('code', '=', 'IN')], limit=1)
)
    source = fields.Selection(
            [
                ('self', 'Self'),
                ('inside_sales', 'Inside Sales')                
            ],
            string="Source")
    expo_name = fields.Char(string="Expo Name")
    lead_generated_by = fields.Many2one(
    'res.users', 
    string='Lead Generated By', 
    default=lambda self: self.env.user.id
)
    line_ids_contacts = fields.One2many('sos_leads_customer_contact_lines', 'ref_id', string="Contact Details",copy=True)
    convert_to_customer_btn_display = fields.Boolean(default=True)
    def action_convert_to_customer(self):
        self.convert_to_customer_btn_display = False
        self.status = "Moved to Customer"
        customer_model = self.env['sos_customers']
        line_contacts_data = [(0, 0, {
        'ref_id': self.id,  # Replace with actual field names
        'name': line.name,
        'dept': line.dept,
        'email': line.email,
        'mobile_number': line.mobile_number,
        'set_as_primary': line.set_as_primary,
    }) for line in self.line_ids_contacts]
        customer_record = customer_model.create({
                    'customer_name': self.company_name,
                    'customer_city':self.location,
                    'vertical_domain':self.vertical_domain.id,
                    'correspondence_address': self.correspondence_address,
                    'products':self.products,
                    'interested_in':self.interested_in,
                    'project_name':self.project_name,
                    'line_ids_contacts': line_contacts_data,
                    'leads_ref_no':self.id
                    
                })
        return {
                        'type': 'ir.actions.client',
                        'tag': 'display_notification',
                        'params': {
                            'message': "Moved Successfully",
                            'type': 'success',
                            'sticky': False
                        }
                    }
class SOS_Leads_Lines(models.Model):
    _name = 'sos_leads_customer_contact_lines'
    _description = 'Leads Contact Lines'

    ref_id = fields.Many2one('sos_sales_leads', string="Leads")
    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")
    linkedin_profile = fields.Char(string="LinkedIn Profile")
    remarks = fields.Text(string="Remarks")
    # @api.model
    # def create(self, vals):
    #     record = super(SOS_Leads_Lines, self).create(vals)
    #     if not self.env['sos_leads_customer_contact_lines'].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_Leads_Lines, self).write(vals)
        if 'set_as_primary' in vals and vals['set_as_primary']:
            for record in self:
                self.env['sos_leads_customer_contact_lines'].search([
                    ('ref_id', '=', record.ref_id.id),
                    ('id', '!=', record.id)
                ]).write({'set_as_primary': False})
        return res
    
class ProductInterest(models.Model):
    _name = 'sos_products'
    _description = 'Product Interest'

    name = fields.Char(string="Product Name", required=True)
    color = fields.Integer(string="Colour")
    @api.model
    def create(self, vals):
        if 'color' not in vals:
            vals['color'] = random.randint(1, 11) 
        return super(ProductInterest, self).create(vals)
    
class ProjectMaster(models.Model):
    _name = 'sos_projects'
    _description = 'Projects'

    name = fields.Char(string="Project Name", required=True)
