from odoo import models, fields, api
import io
from datetime import date, timedelta

class ActionPlanSummaryWizard(models.TransientModel):
    _name = 'action_plan_summary_wizard'
    _description = 'Action Plan Summary Wizard'

    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)]
    )
    select_week = fields.Selection(
        selection='_get_week_selection',
        string='Select Week'
    )
    def _get_week_selection(self):
        """Generate a list of weeks for the dropdown."""
        weeks = []
        current_date = date.today()
        for i in [-1, 0, 1]:
            week_start = current_date + timedelta(days=(i * 7 - current_date.weekday()))
            week_end = week_start + timedelta(days=5)
            week_label = f"{week_start.strftime('%b %d')} - {week_end.strftime('%b %d')}"
            weeks.append((week_label, week_label))
        return weeks
    def generate_report(self):
        domain = [('week_number', '=', self.select_week)]
        if self.sales_person_id:
            domain.append(('sales_executive', '=', self.sales_person_id.id))

        records = self.env['sos_action_plan_summary_week_wise'].search(domain)
        if not records:
            return {
                'type': 'ir.actions.client',
                'tag': 'display_notification',
                'params': {
                    'title': 'No Records Found',
                    'message': f'No records found for week {self.select_week}.',
                    'type': 'warning',
                    'sticky': False,
                }
            }

        # Trigger report only if data exists
        return self.env.ref('sos_sales.action_plan_summary_report').report_action(
            [],
            data = {
            'week_number': self.select_week if self.select_week else False,
            'sales_person_id': self.sales_person_id.id if self.sales_person_id else False
        }
        )
