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

    <!-- SIN View/Edit Wizard Form -->
    <record id="hr_employee_sin_wizard_form" model="ir.ui.view">
        <field name="name">hr.employee.sin.wizard.form</field>
        <field name="model">hr.employee.sin.wizard</field>
        <field name="arch" type="xml">
            <form string="View/Edit Social Insurance Number">
                <div class="alert alert-info mb-3" role="alert">
                    <i class="fa fa-lock me-2"/>
                    <strong>Restricted Access:</strong> This information is only visible to HR Managers and Accountants.
                </div>
                <group>
                    <group>
                        <field name="employee_id" readonly="1"/>
                    </group>
                    <group>
                        <label for="sin_number"/>
                        <div class="o_row">
                            <field name="sin_number" nolabel="1" placeholder="XXX-XXX-XXX"/>
                        </div>
                        <field name="sin_number_formatted" readonly="1" invisible="1"/>
                    </group>
                </group>
                <div class="text-muted small mt-2">
                    <i class="fa fa-info-circle me-1"/>
                    Enter the 9-digit SIN number. Format: XXX-XXX-XXX
                </div>
                <footer>
                    <button name="action_save" string="Save" type="object" class="btn-primary"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- Employee Termination Wizard Form -->
    <record id="hr_employee_terminate_wizard_form" model="ir.ui.view">
        <field name="name">hr.employee.terminate.wizard.form</field>
        <field name="model">hr.employee.terminate.wizard</field>
        <field name="arch" type="xml">
            <form string="Terminate Employee">
                <div class="alert alert-warning mb-3" role="alert">
                    <i class="fa fa-warning me-2"/>
                    <strong>Important:</strong> Make sure you send a Record of Employment (ROE) to Service Canada within 5 days of the interruption in earnings.
                </div>
                <group>
                    <group string="Employee">
                        <field name="employee_id" readonly="1"/>
                        <field name="last_day_of_work" required="1"/>
                    </group>
                    <group string="Reason">
                        <field name="roe_reason_code" required="1"/>
                    </group>
                </group>
                <group>
                    <group string="Options">
                        <field name="show_in_employee_lists_only"/>
                        <field name="issue_roe_now"/>
                    </group>
                </group>
                <group>
                    <field name="notes" placeholder="Optional termination notes..."/>
                </group>
                <footer>
                    <button name="action_terminate" string="Terminate Employee" type="object" class="btn-danger"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <!-- Extend Employee Form View -->
    <record id="hr_employee_view_form_inherit_payroll_canada" model="ir.ui.view">
        <field name="name">hr.employee.form.inherit.payroll.canada</field>
        <field name="model">hr.employee</field>
        <field name="inherit_id" ref="hr.view_employee_form"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            
            <!-- Add Terminate button in header -->
            <xpath expr="//header" position="inside">
                <button name="action_terminate_employee" 
                        string="Terminate" 
                        type="object" 
                        class="btn-secondary"
                        invisible="employment_status == 'terminated'"/>
                <button name="action_issue_roe" 
                        string="Issue ROE" 
                        type="object" 
                        class="btn-secondary"
                        invisible="employment_status != 'terminated' or roe_issued"/>
            </xpath>

            <!-- Add Employment Status badge after name -->
            <xpath expr="//div[hasclass('oe_title')]" position="inside">
                <div class="d-flex gap-2 mt-2">
                    <span class="badge rounded-pill text-bg-success" 
                          invisible="employment_status != 'active'">Active</span>
                    <span class="badge rounded-pill text-bg-warning" 
                          invisible="employment_status != 'on_leave'">On Leave</span>
                    <span class="badge rounded-pill text-bg-danger" 
                          invisible="employment_status != 'terminated'">Terminated</span>
                </div>
            </xpath>

            <!-- Add Fusion Payroll Tab -->
            <xpath expr="//notebook" position="inside">
                <page string="Fusion Payroll" name="fusion_payroll">
                    
                    <!-- Employment Details Section -->
                    <group string="Employment Details">
                        <group>
                            <field name="employment_status"/>
                            <field name="hire_date"/>
                            <field name="pay_schedule"/>
                        </group>
                        <group>
                            <field name="work_location_ids" widget="many2many_tags" 
                                   options="{'color_field': 'status'}"
                                   placeholder="Select work locations..."/>
                            <field name="department_id"/>
                            <field name="employee_number"/>
                        </group>
                    </group>
                    <group invisible="employment_status != 'terminated'">
                        <group>
                            <field name="last_day_of_work" required="employment_status == 'terminated'"/>
                            <field name="roe_reason_code" required="employment_status == 'terminated'"/>
                            <field name="show_in_employee_lists_only"/>
                        </group>
                    </group>
                    
                    <!-- Personal Info Section -->
                    <group string="Personal Info">
                        <group>
                            <field name="preferred_name" placeholder="Preferred First Name"/>
                            <field name="private_email" widget="email"/>
                            <field name="private_phone" widget="phone"/>
                        </group>
                        <group>
                            <field name="birthday"/>
                            <field name="gender_identity"/>
                            <!-- SIN with masking -->
                            <label for="sin_number_display"/>
                            <div class="o_row">
                                <field name="sin_number_display" readonly="1" nolabel="1"/>
                                <button name="action_view_sin" type="object" 
                                        string="View/Edit" class="btn-link"
                                        groups="hr.group_hr_manager,account.group_account_manager"/>
                            </div>
                        </group>
                    </group>
                    
                    <group string="Home Address">
                        <group>
                            <field name="home_street" placeholder="Street Address"/>
                            <field name="home_street2" placeholder="Apt, Suite, Unit"/>
                            <field name="home_city"/>
                        </group>
                        <group>
                            <field name="home_province"/>
                            <field name="home_postal_code" placeholder="A1A 1A1"/>
                        </group>
                    </group>
                    
                    <group string="Mailing Address">
                        <group>
                            <field name="mailing_same_as_home"/>
                        </group>
                        <group invisible="mailing_same_as_home">
                            <field name="mailing_street"/>
                            <field name="mailing_city"/>
                            <field name="mailing_province"/>
                            <field name="mailing_postal_code"/>
                        </group>
                    </group>

                    <!-- Tax Withholdings Section -->
                    <group string="Tax Withholdings">
                        <group>
                            <field name="federal_td1_amount" widget="monetary"/>
                            <field name="federal_additional_tax" widget="monetary"/>
                        </group>
                        <group>
                            <field name="provincial_claim_amount" widget="monetary"/>
                        </group>
                    </group>

                    <group string="Tax Exemptions">
                        <group>
                            <field name="exempt_cpp"/>
                            <field name="exempt_ei"/>
                            <field name="exempt_federal_tax"/>
                        </group>
                    </group>

                    <!-- Payment Method Section -->
                    <group string="Payment Method">
                        <group>
                            <field name="payment_method" widget="radio"/>
                        </group>
                    </group>

                    <!-- Base Pay Section -->
                    <field name="currency_id" invisible="1"/>
                    <group string="Base Pay">
                        <group>
                            <field name="pay_type" widget="radio"/>
                            <field name="hourly_rate" widget="monetary" 
                                   invisible="pay_type != 'hourly'"/>
                            <field name="salary_amount" widget="monetary"
                                   invisible="pay_type != 'salary'"/>
                        </group>
                        <group>
                            <field name="default_hours_per_day"/>
                            <field name="default_days_per_week"/>
                            <field name="default_hours_per_week" readonly="1"/>
                            <field name="base_pay_effective_date"/>
                        </group>
                    </group>

                    <!-- Time Off / Vacation Section -->
                    <group string="Time Off">
                        <group>
                            <field name="vacation_policy"/>
                            <field name="vacation_rate" 
                                   invisible="vacation_policy not in ['percent_4', 'percent_6']"/>
                        </group>
                    </group>

                    <!-- Deductions and Contributions -->
                    <group string="Deductions and Contributions">
                        <group>
                            <field name="t4_dental_code"/>
                        </group>
                    </group>

                    <!-- Billing Section -->
                    <group string="Billing">
                        <group>
                            <field name="billing_rate" widget="monetary"/>
                            <field name="billable_by_default"/>
                        </group>
                    </group>

                    <!-- Emergency Contact Section -->
                    <group string="Emergency Contact">
                        <group>
                            <field name="emergency_first_name"/>
                            <field name="emergency_last_name"/>
                            <field name="emergency_relationship"/>
                        </group>
                        <group>
                            <field name="emergency_phone" widget="phone"/>
                            <field name="emergency_email" widget="email"/>
                        </group>
                    </group>

                    <!-- ROE Tracking Section (visible only for terminated) -->
                    <group string="Record of Employment (ROE)" 
                           invisible="employment_status != 'terminated'">
                        <group>
                            <field name="roe_issued"/>
                            <field name="roe_issued_date" invisible="not roe_issued"/>
                        </group>
                        <group>
                            <field name="roe_serial_number" invisible="not roe_issued"/>
                        </group>
                    </group>

                </page>
            </xpath>

        </field>
    </record>

    <!-- Employee Tree View - Add Status Column -->
    <record id="hr_employee_view_tree_inherit_payroll_canada" model="ir.ui.view">
        <field name="name">hr.employee.tree.inherit.payroll.canada</field>
        <field name="model">hr.employee</field>
        <field name="inherit_id" ref="hr.view_employee_tree"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='department_id']" position="after">
                <field name="employment_status" 
                       decoration-success="employment_status == 'active'"
                       decoration-warning="employment_status == 'on_leave'"
                       decoration-danger="employment_status == 'terminated'"/>
                <field name="hire_date"/>
            </xpath>
        </field>
    </record>

    <!-- Employee Search View - Add Filters -->
    <record id="hr_employee_view_search_inherit_payroll_canada" model="ir.ui.view">
        <field name="name">hr.employee.search.inherit.payroll.canada</field>
        <field name="model">hr.employee</field>
        <field name="inherit_id" ref="hr.view_employee_filter"/>
        <field name="arch" type="xml">
            <xpath expr="//filter[@name='my_team']" position="after">
                <separator/>
                <filter name="active_employees" string="Active" 
                        domain="[('employment_status', '=', 'active')]"/>
                <filter name="on_leave_employees" string="On Leave" 
                        domain="[('employment_status', '=', 'on_leave')]"/>
                <filter name="terminated_employees" string="Terminated" 
                        domain="[('employment_status', '=', 'terminated')]"/>
                <filter name="pending_roe" string="Pending ROE" 
                        domain="[('employment_status', '=', 'terminated'), ('roe_issued', '=', False)]"/>
            </xpath>
            <xpath expr="//group" position="inside">
                <filter name="group_by_status" string="Employment Status" 
                        context="{'group_by': 'employment_status'}"/>
                <filter name="group_by_pay_schedule" string="Pay Schedule" 
                        context="{'group_by': 'pay_schedule'}"/>
            </xpath>
        </field>
    </record>

</odoo>
