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

    <!-- Pay Period Tree View -->
    <record id="payroll_pay_period_tree" model="ir.ui.view">
        <field name="name">payroll.pay.period.tree</field>
        <field name="model">payroll.pay.period</field>
        <field name="arch" type="xml">
            <list>
                <field name="name" string="Period"/>
                <field name="schedule_type"/>
                <field name="date_start"/>
                <field name="date_end"/>
                <field name="pay_date"/>
                <field name="state" widget="badge" 
                       decoration-info="state == 'draft'"
                       decoration-warning="state == 'in_progress'"
                       decoration-success="state == 'paid'"
                       decoration-muted="state == 'closed'"/>
                <field name="payslip_run_id"/>
            </list>
        </field>
    </record>

    <!-- Pay Period Form View -->
    <record id="payroll_pay_period_form" model="ir.ui.view">
        <field name="name">payroll.pay.period.form</field>
        <field name="model">payroll.pay.period</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <field name="state" widget="statusbar" statusbar_visible="draft,in_progress,paid,closed"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>
                    <group>
                        <group>
                            <field name="company_id" groups="base.group_multi_company"/>
                            <field name="schedule_type"/>
                        </group>
                        <group>
                            <field name="date_start"/>
                            <field name="date_end"/>
                            <field name="pay_date"/>
                        </group>
                    </group>
                    <group>
                        <field name="payslip_run_id" readonly="1"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Pay Period Search View -->
    <record id="payroll_pay_period_search" model="ir.ui.view">
        <field name="name">payroll.pay.period.search</field>
        <field name="model">payroll.pay.period</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="schedule_type"/>
            </search>
        </field>
    </record>

    <!-- Pay Period Settings Form View -->
    <record id="payroll_pay_period_settings_form" model="ir.ui.view">
        <field name="name">payroll.pay.period.settings.form</field>
        <field name="model">payroll.pay.period.settings</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <div class="oe_title mb-3">
                        <h1>Pay Period Configuration</h1>
                    </div>
                    <group>
                        <group string="Schedule Settings">
                            <field name="company_id" invisible="1"/>
                            <field name="schedule_type" widget="radio"/>
                            <field name="first_period_start"/>
                            <field name="pay_day_offset" help="Days after period end until pay date"/>
                        </group>
                        <group string="Auto-Generation">
                            <field name="auto_generate_periods"/>
                            <field name="periods_to_generate" invisible="not auto_generate_periods"/>
                        </group>
                    </group>
                    <div class="mt-4">
                        <button name="action_generate_periods" 
                                type="object" 
                                string="Generate Pay Periods" 
                                class="btn-primary"
                                icon="fa-calendar-plus-o"/>
                    </div>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Pay Period Action -->
    <record id="action_payroll_pay_period" model="ir.actions.act_window">
        <field name="name">Pay Periods</field>
        <field name="res_model">payroll.pay.period</field>
        <field name="view_mode">list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first pay period
            </p>
            <p>
                Pay periods help you track payroll cycles. Configure your pay period settings
                to automatically generate periods based on your schedule.
            </p>
        </field>
    </record>

    <!-- Pay Period Settings Action -->
    <record id="action_payroll_pay_period_settings" model="ir.actions.act_window">
        <field name="name">Pay Period Settings</field>
        <field name="res_model">payroll.pay.period.settings</field>
        <field name="view_mode">form</field>
        <field name="target">current</field>
    </record>

    <!-- Server Action to Open Pay Period Settings (creates if not exists) -->
    <record id="action_open_pay_period_settings" model="ir.actions.server">
        <field name="name">Pay Period Settings</field>
        <field name="model_id" ref="model_payroll_pay_period_settings"/>
        <field name="state">code</field>
        <field name="code">
settings = model.get_or_create_settings()
action = {
    'type': 'ir.actions.act_window',
    'name': 'Pay Period Settings',
    'res_model': 'payroll.pay.period.settings',
    'res_id': settings.id,
    'view_mode': 'form',
    'target': 'current',
}
        </field>
    </record>

</odoo>
