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

    <!-- Batch Report Action -->
    <record id="action_report_clock_batch" model="ir.actions.report">
        <field name="name">Batch Attendance Report</field>
        <field name="model">fusion.clock.report</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">fusion_clock.report_clock_batch</field>
        <field name="report_file">fusion_clock.report_clock_batch</field>
    </record>

    <!-- Batch Report Template -->
    <template id="report_clock_batch">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="doc">
                <t t-call="web.external_layout">
                    <div class="page" style="font-family: Arial, sans-serif;">
                        <!-- Header -->
                        <div style="text-align:center; margin-bottom:24px;">
                            <h2 style="color:#1a1d23; margin:0;">Employee Attendance Summary</h2>
                            <p style="color:#6b7280; margin:4px 0 0;">
                                <t t-esc="doc.date_start"/> to <t t-esc="doc.date_end"/>
                                | <t t-esc="doc.company_id.name"/>
                            </p>
                        </div>

                        <!-- Summary Table - Group by Employee -->
                        <t t-set="employees" t-value="doc.attendance_ids.mapped('employee_id')"/>
                        <table style="width:100%; border-collapse:collapse; font-size:11px; margin-bottom:24px;">
                            <thead>
                                <tr style="background:#1a1d23; color:white;">
                                    <th style="padding:8px; text-align:left;">Employee</th>
                                    <th style="padding:8px; text-align:center;">Days</th>
                                    <th style="padding:8px; text-align:center;">Total Hours</th>
                                    <th style="padding:8px; text-align:center;">Net Hours</th>
                                    <th style="padding:8px; text-align:center;">Breaks (min)</th>
                                    <th style="padding:8px; text-align:center;">Late In</th>
                                    <th style="padding:8px; text-align:center;">Early Out</th>
                                    <th style="padding:8px; text-align:center;">Leave</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-foreach="employees.sorted(key=lambda e: e.name)" t-as="emp">
                                    <t t-set="emp_atts" t-value="doc.attendance_ids.filtered(lambda a: a.employee_id == emp)"/>
                                    <t t-set="emp_dates" t-value="set(a.check_in.date() for a in emp_atts if a.check_in)"/>
                                    <t t-set="late_count" t-value="len(doc.env['fusion.clock.penalty'].search([('employee_id', '=', emp.id), ('date', '>=', doc.date_start), ('date', '&lt;=', doc.date_end), ('penalty_type', '=', 'late_in')]))"/>
                                    <t t-set="early_count" t-value="len(doc.env['fusion.clock.penalty'].search([('employee_id', '=', emp.id), ('date', '>=', doc.date_start), ('date', '&lt;=', doc.date_end), ('penalty_type', '=', 'early_out')]))"/>
                                    <t t-set="emp_leaves" t-value="doc.leave_request_ids.filtered(lambda l: l.employee_id == emp)"/>
                                    <tr t-attf-style="background: {{ '#f8f9fa' if emp_index % 2 == 0 else '#ffffff' }};">
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; font-weight:bold;">
                                            <t t-esc="emp.name"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-esc="len(emp_dates)"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-esc="'%.1f' % sum(a.worked_hours or 0 for a in emp_atts)"/>h
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center; font-weight:bold; color:#10B981;">
                                            <t t-esc="'%.1f' % sum(a.x_fclk_net_hours or 0 for a in emp_atts)"/>h
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-esc="'%.0f' % sum(a.x_fclk_break_minutes or 0 for a in emp_atts)"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;"
                                            t-attf-style="color: {{ '#ef4444' if late_count else '#1a1d23' }};">
                                            <t t-esc="late_count"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;"
                                            t-attf-style="color: {{ '#ef4444' if early_count else '#1a1d23' }};">
                                            <t t-esc="early_count"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;"
                                            t-attf-style="color: {{ '#3b82f6' if emp_leaves else '#1a1d23' }};">
                                            <t t-esc="len(emp_leaves)"/>
                                        </td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>

                        <!-- Footer -->
                        <div style="margin-top:24px; padding-top:12px; border-top:1px solid #e0e0e0; text-align:center;">
                            <p style="color:#9ca3af; font-size:10px;">
                                Generated by Fusion Clock |
                                <t t-esc="context_timestamp(datetime.datetime.now()).strftime('%Y-%m-%d %H:%M')"/>
                            </p>
                        </div>
                    </div>
                </t>
            </t>
        </t>
    </template>

</odoo>
