<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <!-- Server Action: Generate Historical Reports -->
    <record id="action_server_generate_historical" model="ir.actions.server">
        <field name="name">Generate Historical Reports</field>
        <field name="model_id" ref="model_fusion_clock_report"/>
        <field name="state">code</field>
        <field name="code">action = model.action_generate_historical_reports()</field>
    </record>

    <!-- Report List View -->
    <record id="view_fusion_clock_report_list" model="ir.ui.view">
        <field name="name">fusion.clock.report.list</field>
        <field name="model">fusion.clock.report</field>
        <field name="arch" type="xml">
            <list string="Clock Reports" default_order="date_end desc">
                <header>
                    <button name="%(action_server_generate_historical)d"
                            type="action"
                            string="Generate Historical Reports"
                            class="btn-secondary"
                            icon="fa-history"/>
                </header>
                <field name="name"/>
                <field name="date_start"/>
                <field name="date_end"/>
                <field name="schedule_type"/>
                <field name="employee_id"/>
                <field name="is_batch"/>
                <field name="days_worked"/>
                <field name="total_hours" widget="float_time"/>
                <field name="net_hours" widget="float_time"/>
                <field name="total_penalties"/>
                <field name="state" widget="badge"
                       decoration-info="state == 'draft'"
                       decoration-success="state == 'generated'"
                       decoration-warning="state == 'sent'"/>
            </list>
        </field>
    </record>

    <!-- Report Form View -->
    <record id="view_fusion_clock_report_form" model="ir.ui.view">
        <field name="name">fusion.clock.report.form</field>
        <field name="model">fusion.clock.report</field>
        <field name="arch" type="xml">
            <form string="Clock Report">
                <header>
                    <button name="action_generate_report" type="object"
                            string="Generate Report" class="btn-primary"
                            invisible="state != 'draft'"
                            icon="fa-file-pdf-o"/>
                    <button name="action_send_report" type="object"
                            string="Send Report" class="btn-secondary"
                            invisible="state != 'generated'"
                            icon="fa-envelope"/>
                    <button name="action_export_csv" type="object"
                            string="Export CSV" class="btn-secondary"
                            invisible="state == 'draft'"
                            icon="fa-download"/>
                    <button name="action_reset_draft" type="object"
                            string="Reset to Draft" class="btn-secondary"
                            invisible="state == 'draft'"
                            icon="fa-undo"
                            confirm="This will reset the report to draft. Continue?"/>
                    <field name="state" widget="statusbar" statusbar_visible="draft,generated,sent"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="name"/>
                        </h1>
                    </div>
                    <group>
                        <group string="Period">
                            <field name="date_start"/>
                            <field name="date_end"/>
                            <field name="schedule_type"/>
                            <field name="employee_id"/>
                            <field name="is_batch"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                        <group string="Summary">
                            <field name="days_worked"/>
                            <field name="total_hours" widget="float_time"/>
                            <field name="net_hours" widget="float_time"/>
                            <field name="total_breaks"/>
                            <field name="total_penalties"/>
                        </group>
                    </group>

                    <!-- PDF Download -->
                    <group string="Report File" invisible="not report_pdf">
                        <field name="report_pdf" filename="report_pdf_filename"/>
                        <field name="report_pdf_filename" invisible="1"/>
                    </group>

                    <!-- Attendance Records -->
                    <notebook>
                        <page string="Attendance Records" name="attendances">
                            <field name="attendance_ids" nolabel="1">
                                <list>
                                    <field name="employee_id"/>
                                    <field name="check_in" widget="datetime"/>
                                    <field name="check_out" widget="datetime"/>
                                    <field name="worked_hours" widget="float_time"/>
                                    <field name="x_fclk_break_minutes" string="Break"/>
                                    <field name="x_fclk_net_hours" string="Net" widget="float_time"/>
                                    <field name="x_fclk_location_id" string="Location"/>
                                </list>
                            </field>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Report Search View -->
    <record id="view_fusion_clock_report_search" model="ir.ui.view">
        <field name="name">fusion.clock.report.search</field>
        <field name="model">fusion.clock.report</field>
        <field name="arch" type="xml">
            <search string="Search Reports">
                <field name="employee_id"/>
                <field name="name"/>
                <filter name="individual" string="Individual" domain="[('employee_id', '!=', False)]"/>
                <filter name="batch" string="Batch" domain="[('employee_id', '=', False)]"/>
                <separator/>
                <filter name="draft" string="Draft" domain="[('state', '=', 'draft')]"/>
                <filter name="generated" string="Generated" domain="[('state', '=', 'generated')]"/>
                <filter name="sent" string="Sent" domain="[('state', '=', 'sent')]"/>
                <separator/>
                <filter name="group_employee" string="Employee" context="{'group_by': 'employee_id'}"/>
                <filter name="group_period" string="Schedule Type" context="{'group_by': 'schedule_type'}"/>
                <filter name="group_state" string="Status" context="{'group_by': 'state'}"/>
            </search>
        </field>
    </record>

    <!-- Report Action -->
    <record id="action_fusion_clock_report" model="ir.actions.act_window">
        <field name="name">Reports</field>
        <field name="res_model">fusion.clock.report</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_clock_report_search"/>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No reports generated yet
            </p>
            <p>
                Reports are automatically generated at the end of each pay period.
                You can also create them manually.
            </p>
        </field>
    </record>

</odoo>
