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

    <!-- Employee Report Action -->
    <record id="action_report_clock_employee" model="ir.actions.report">
        <field name="name">Employee 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_employee</field>
        <field name="report_file">fusion_clock.report_clock_employee</field>
        <field name="binding_model_id" ref="model_fusion_clock_report"/>
        <field name="binding_type">report</field>
    </record>

    <!-- Employee Report Template -->
    <template id="report_clock_employee">
        <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;">Attendance Report</h2>
                            <p style="color:#6b7280; margin:4px 0 0;">
                                <t t-esc="doc.date_start"/> to <t t-esc="doc.date_end"/>
                            </p>
                        </div>

                        <!-- Employee Info -->
                        <table style="width:100%; margin-bottom:16px; border-collapse:collapse;">
                            <tr>
                                <td style="width:50%; padding:8px 12px; border:1px solid #e0e0e0;">
                                    <strong>Employee:</strong> <t t-esc="doc.employee_id.name"/>
                                </td>
                                <td style="width:50%; padding:8px 12px; border:1px solid #e0e0e0; text-align:right;">
                                    <strong>Company:</strong> <t t-esc="doc.company_id.name"/>
                                </td>
                            </tr>
                            <tr>
                                <td style="padding:8px 12px; border:1px solid #e0e0e0;">
                                    <strong>Department:</strong> <t t-esc="doc.employee_id.department_id.name or 'N/A'"/>
                                </td>
                                <td style="padding:8px 12px; border:1px solid #e0e0e0; text-align:right;">
                                    <strong>Schedule:</strong> <t t-esc="dict(doc._fields['schedule_type'].selection).get(doc.schedule_type, '')"/>
                                </td>
                            </tr>
                        </table>

                        <!-- Pay Period -->
                        <table style="width:100%; margin-bottom:16px; border-collapse:collapse;">
                            <tr>
                                <td style="padding:8px 12px; border:1px solid #e0e0e0; background:#f8f9fa;">
                                    <strong>Pay Period:</strong>
                                    <t t-esc="doc.date_start" t-options="{'widget': 'date', 'format': 'MMM d, yyyy'}"/>
                                    &#8212;
                                    <t t-esc="doc.date_end" t-options="{'widget': 'date', 'format': 'MMM d, yyyy'}"/>
                                </td>
                            </tr>
                        </table>

                        <!-- Summary Box -->
                        <table style="width:100%; border-collapse:collapse; margin-bottom:24px; background:#f8f9fa; border-radius:8px;">
                            <tr>
                                <td style="padding:12px; text-align:center; border:1px solid #e0e0e0;">
                                    <div style="font-size:20px; font-weight:bold; color:#1a1d23;">
                                        <t t-esc="doc.days_worked"/>
                                    </div>
                                    <div style="font-size:11px; color:#6b7280;">Days Worked</div>
                                </td>
                                <td style="padding:12px; text-align:center; border:1px solid #e0e0e0;">
                                    <div style="font-size:20px; font-weight:bold; color:#1a1d23;">
                                        <t t-esc="'%.1f' % doc.total_hours"/>h
                                    </div>
                                    <div style="font-size:11px; color:#6b7280;">Total Hours</div>
                                </td>
                                <td style="padding:12px; text-align:center; border:1px solid #e0e0e0;">
                                    <div style="font-size:20px; font-weight:bold; color:#10B981;">
                                        <t t-esc="'%.1f' % doc.net_hours"/>h
                                    </div>
                                    <div style="font-size:11px; color:#6b7280;">Net Hours</div>
                                </td>
                                <td style="padding:12px; text-align:center; border:1px solid #e0e0e0;">
                                    <div style="font-size:20px; font-weight:bold; color:#1a1d23;">
                                        <t t-esc="'%.0f' % doc.total_breaks"/> min
                                    </div>
                                    <div style="font-size:11px; color:#6b7280;">Total Breaks</div>
                                </td>
                                <td style="padding:12px; text-align:center; border:1px solid #e0e0e0;">
                                    <div style="font-size:20px; font-weight:bold;"
                                         t-attf-style="color: {{ '#ef4444' if doc.total_penalties > 0 else '#1a1d23' }};">
                                        <t t-esc="doc.total_penalties"/>
                                    </div>
                                    <div style="font-size:11px; color:#6b7280;">Penalties</div>
                                </td>
                                <td style="padding:12px; text-align:center; border:1px solid #e0e0e0;">
                                    <div style="font-size:20px; font-weight:bold;"
                                         t-attf-style="color: {{ '#3b82f6' if doc.leave_days > 0 else '#1a1d23' }};">
                                        <t t-esc="doc.leave_days"/>
                                    </div>
                                    <div style="font-size:11px; color:#6b7280;">Leave Days</div>
                                </td>
                            </tr>
                        </table>

                        <!-- Daily Breakdown -->
                        <h4 style="color:#1a1d23; margin-bottom:8px;">Daily Breakdown</h4>
                        <table style="width:100%; border-collapse:collapse; font-size:11px;">
                            <thead>
                                <tr style="background:#1a1d23; color:white;">
                                    <th style="padding:8px; text-align:left;">Date</th>
                                    <th style="padding:8px; text-align:center;">Clock In</th>
                                    <th style="padding:8px; text-align:center;">Clock Out</th>
                                    <th style="padding:8px; text-align:center;">Worked</th>
                                    <th style="padding:8px; text-align:center;">Break</th>
                                    <th style="padding:8px; text-align:center;">Net Hours</th>
                                    <th style="padding:8px; text-align:left;">Location</th>
                                </tr>
                            </thead>
                            <tbody>
                                <t t-foreach="doc.attendance_ids.sorted(key=lambda a: a.check_in)" t-as="att">
                                    <tr t-attf-style="background: {{ '#f8f9fa' if att_index % 2 == 0 else '#ffffff' }};">
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0;">
                                            <t t-esc="att.check_in" t-options="{'widget': 'date'}"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-esc="att.check_in" t-options="{'widget': 'datetime', 'format': 'HH:mm'}"/>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-if="att.check_out">
                                                <t t-esc="att.check_out" t-options="{'widget': 'datetime', 'format': 'HH:mm'}"/>
                                            </t>
                                            <t t-else="">--</t>
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-esc="'%.1f' % (att.worked_hours or 0)"/>h
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                            <t t-esc="'%.0f' % (att.x_fclk_break_minutes or 0)"/> min
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center; font-weight:bold;">
                                            <t t-esc="'%.1f' % (att.x_fclk_net_hours or 0)"/>h
                                        </td>
                                        <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0;">
                                            <t t-esc="att.x_fclk_location_id.name or ''"/>
                                        </td>
                                    </tr>
                                </t>
                            </tbody>
                        </table>

                        <!-- Leave Days -->
                        <t t-if="doc.leave_request_ids">
                            <h4 style="color:#1a1d23; margin:24px 0 8px;">Leave Days</h4>
                            <table style="width:100%; border-collapse:collapse; font-size:11px;">
                                <thead>
                                    <tr style="background:#3b82f6; color:white;">
                                        <th style="padding:8px; text-align:left;">Date</th>
                                        <th style="padding:8px; text-align:left;">Reason</th>
                                        <th style="padding:8px; text-align:center;">Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <t t-foreach="doc.leave_request_ids.sorted(key=lambda l: l.leave_date)" t-as="lv">
                                        <tr t-attf-style="background: {{ '#f0f7ff' if lv_index % 2 == 0 else '#ffffff' }};">
                                            <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0;">
                                                <t t-esc="lv.leave_date" t-options="{'widget': 'date'}"/>
                                            </td>
                                            <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0;">
                                                <t t-esc="lv.reason"/>
                                            </td>
                                            <td style="padding:6px 8px; border-bottom:1px solid #e0e0e0; text-align:center;">
                                                <t t-if="lv.state == 'reviewed'">
                                                    <span style="color:#10B981; font-weight:bold;">Reviewed</span>
                                                </t>
                                                <t t-else="">
                                                    <span style="color:#3b82f6;">Auto-Approved</span>
                                                </t>
                                            </td>
                                        </tr>
                                    </t>
                                </tbody>
                            </table>
                        </t>

                        <!-- 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>
