# from odoo import http
# from odoo.http import request
# from datetime import date, timedelta

# class SpencoController(http.Controller):

#     @http.route('/spenco/week_summary_report', type='json', auth='user')
#     def week_summary_report(self, **kwargs):
#         sales_person_id = kwargs.get('sales_person_id')
#         if not sales_person_id:
#             return {'error': 'No sales person provided'}

#         return {
#             'status': 'ok',
#             'redirect_url': f"/spenco/render_week_summary?sales_person_id={sales_person_id}"
#         }

#     @http.route('/spenco/render_week_summary', type='http', auth='user', website=True)
#     def render_week_summary(self, sales_person_id=None, **kwargs):
#         sales_person_id = int(sales_person_id) if sales_person_id else None
#         current_date = date.today()
#         start_of_week = current_date - timedelta(days=current_date.weekday())
#         end_of_week = start_of_week + timedelta(days=5)
#         week_number = f"{start_of_week.strftime('%b %d')} - {end_of_week.strftime('%b %d')}"
#         domain = [('week_number', '=', week_number)]
#         sales_users = request.env['res.users'].search([
#             ('groups_id', 'in', [request.env.ref('sos_inventory.sos_sales_user').id])
#         ]).read(['id', 'name'])
#         if sales_person_id:
#             domain.append(('sales_executive', '=', sales_person_id))

#         records = request.env['sos_spenco_summary_week_wise'].search(domain)
#         if not records:
#             return request.render("sos_sales.spenco_week_summary_report_template", {
#                 'error': f"No records found for week {week_number}.",
#                 'sales_users': sales_users,
#                 'sales_person_id': sales_person_id,
#                 'sales_person_name': "All Sales Persons",
#                 'aggregated_data': {},
#             })

#         # Reuse aggregation logic
#         status_map = {
#             'Suspects': 'S',
#             'Prospects': 'P',
#             'Engaged': 'E',
#             'Negotiation': 'N',
#             'Commercial Order': 'CO'
#         }
#         aggregated_data = {
#             'Opening': {'P': {'count': 0, 'value': 0.0}, 'E': {'count': 0, 'value': 0.0}, 'N': {'count': 0, 'value': 0.0}, 'CO': {'count': 0, 'value': 0.0}, 'EOV': {'value': 0.0}},
#             'Downgrade': {'P': {'count': 0, 'value': 0.0}, 'E': {'count': 0, 'value': 0.0}, 'N': {'count': 0, 'value': 0.0}, 'CO': {'count': 0, 'value': 0.0}, 'EOV': {'value': 0.0}},
#             'Upgrade': {'P': {'count': 0, 'value': 0.0}, 'E': {'count': 0, 'value': 0.0}, 'N': {'count': 0, 'value': 0.0}, 'CO': {'count': 0, 'value': 0.0}, 'EOV': {'value': 0.0}},
#             'New': {'P': {'count': 0, 'value': 0.0}, 'E': {'count': 0, 'value': 0.0}, 'N': {'count': 0, 'value': 0.0}, 'CO': {'count': 0, 'value': 0.0}, 'EOV': {'value': 0.0}},
#             'Closing': {'P': {'count': 0, 'value': 0.0}, 'E': {'count': 0, 'value': 0.0}, 'N': {'count': 0, 'value': 0.0}, 'CO': {'count': 0, 'value': 0.0}, 'EOV': {'value': 0.0}}
#         }
#         for record in records:
#             status = record.spenco_status
#             if not status or status not in status_map:
#                 continue
#             status_key = status_map[status]
#             if status in ['Prospects', 'Engaged', 'Negotiation', 'Commercial Order']:
#                 for stage in ['Opening', 'Downgrade', 'Upgrade', 'New']:
#                     aggregated_data[stage][status_key]['count'] += getattr(record, f'{stage.lower()}_cases_count', 0)
#                     aggregated_data[stage][status_key]['value'] += getattr(record, f'{stage.lower()}_cases_value', 0.0)
#                     aggregated_data[stage]['EOV']['value'] += getattr(record, f'{stage.lower()}_cases_value', 0.0)

#         for status_key in ['P', 'E', 'N', 'CO']:
#             aggregated_data['Closing'][status_key]['count'] = (
#                 aggregated_data['Opening'][status_key]['count'] -
#                 aggregated_data['Downgrade'][status_key]['count'] -
#                 aggregated_data['Upgrade'][status_key]['count'] +
#                 aggregated_data['New'][status_key]['count']
#             )
#             aggregated_data['Closing'][status_key]['value'] = (
#                 aggregated_data['Opening'][status_key]['value'] -
#                 aggregated_data['Downgrade'][status_key]['value'] -
#                 aggregated_data['Upgrade'][status_key]['value'] +
#                 aggregated_data['New'][status_key]['value']
#             )

        
#         return request.render("sos_sales.spenco_week_summary_report_template", {
#             'aggregated_data': aggregated_data,
#             'sales_users': sales_users,
#             'sales_person_id': sales_person_id,
#             'sales_person_name': request.env['res.users'].browse(sales_person_id).name if sales_person_id else "All Sales Persons"
#         })
