from odoo import models, fields, api


class SOSCaseDiaryReportWizard(models.TransientModel):
    _name = 'sos_suspects_wizard'
    _description = 'Suspects Wizard'

    from_date = fields.Date(
        string="From Date",
        default=fields.Date.today  # Default to today's date
    )
    to_date = fields.Date(
        string="To Date",
        default=fields.Date.today  # Default to today's date
    )
    sales_person_id = fields.Many2one(
        'res.users', 
        string='Sales Executive', 
        default=lambda self: self.env.user,
        domain=lambda self: [('groups_id', 'in', self.env.ref('sos_inventory.sos_sales_user').ids + self.env.ref('sos_inventory.sos_ce_head').ids)]
    )
    def action_generate_report(self):
        """Fetch and return a list of 'Suspects' based on the selected date range."""
        domain = [('spenco_status', '=', 'Suspects')]
        if self.sales_person_id:
            domain.append(('sales_person', '=', self.sales_person_id.id))
        if self.from_date:
            domain.append(('line_ids.status_changed_on', '>=', self.from_date))
        if self.to_date:
            domain.append(('line_ids.status_changed_on', '<=', self.to_date))

        suspects = self.env['sos_case_diary'].search(domain)
        filtered_records = suspects.filtered(lambda record: record.line_ids and record.line_ids[-1].spenco_status == 'Suspects')
        return {
            'type': 'ir.actions.act_window',
            'name': 'Suspects Report',
            'res_model': 'sos_case_diary',
            'view_mode': 'tree,form',
            'domain': [('id', 'in', suspects.ids)],
            'target': 'current',
        }

