/** @odoo-module **/

import { registry } from "@web/core/registry";
import { Component } from "@odoo/owl";
import { useService } from "@web/core/utils/hooks";

export class FusionPayrollReportHub extends Component {
    static template = "fusion_payroll.ReportHub";

    setup() {
        this.actionService = useService("action");
    }

    async openReport(actionXmlId) {
        try {
            await this.actionService.doAction(actionXmlId);
        } catch (error) {
            console.error("Failed to open report:", actionXmlId, error);
            // Fallback: try to open directly with context
            const reportModels = {
                'fusion_payroll.action_payroll_report_paycheque_history': 'payroll.report.paycheque.history',
                'fusion_payroll.action_payroll_report_payroll_details': 'payroll.report.payroll.details',
                'fusion_payroll.action_payroll_report_tax_liability': 'payroll.report.tax.liability',
                'fusion_payroll.action_payroll_report_tax_payments': 'payroll.report.tax.payments',
                'fusion_payroll.action_payroll_report_tax_wage_summary': 'payroll.report.tax.wage.summary',
                'fusion_payroll.action_payroll_report_summary': 'payroll.report.summary',
                'fusion_payroll.action_payroll_report_summary_employee': 'payroll.report.summary.by.employee',
                'fusion_payroll.action_payroll_report_employee_directory': 'payroll.report.employee.directory',
                'fusion_payroll.action_payroll_report_time_off': 'payroll.report.time.off',
                'fusion_payroll.action_payroll_report_total_pay': 'payroll.report.total.pay',
                'fusion_payroll.action_payroll_report_total_cost': 'payroll.report.total.cost',
                'fusion_payroll.action_payroll_report_deductions': 'payroll.report.deductions',
                'fusion_payroll.action_payroll_report_workers_comp': 'payroll.report.workers.comp',
            };
            const reportModel = reportModels[actionXmlId];
            if (reportModel) {
                await this.actionService.doAction({
                    type: 'ir.actions.client',
                    tag: 'fusion_payroll.payroll_report_action',
                    name: 'Report',
                    context: { report_model: reportModel },
                });
            }
        }
    }
}

registry.category("actions").add("fusion_payroll.report_hub", FusionPayrollReportHub);
