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

    <!-- ================================================================== -->
    <!-- FACILITY - FORM VIEW                                                -->
    <!-- ================================================================== -->
    <record id="view_ltc_facility_form" model="ir.ui.view">
        <field name="name">fusion.ltc.facility.form</field>
        <field name="model">fusion.ltc.facility</field>
        <field name="arch" type="xml">
            <form string="LTC Facility">
                <header/>
                <sheet>
                    <widget name="web_ribbon" text="Archived" bg_color="text-bg-danger"
                            invisible="active"/>
                    <field name="active" invisible="1"/>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_repairs" type="object"
                                class="oe_stat_button" icon="fa-wrench">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="active_repair_count"/>
                                </span>
                                <span class="o_stat_text">Active Repairs</span>
                            </div>
                        </button>
                        <button name="action_view_repairs" type="object"
                                class="oe_stat_button" icon="fa-list">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="repair_count"/>
                                </span>
                                <span class="o_stat_text">Total Repairs</span>
                            </div>
                        </button>
                        <button name="action_view_cleanups" type="object"
                                class="oe_stat_button" icon="fa-refresh">
                            <div class="o_field_widget o_stat_info">
                                <span class="o_stat_value">
                                    <field name="cleanup_count"/>
                                </span>
                                <span class="o_stat_text">Cleanups</span>
                            </div>
                        </button>
                    </div>
                    <field name="image_1920" widget="image" class="oe_avatar"
                           options="{'preview_image': 'image_128'}"/>
                    <div class="oe_title">
                        <label for="name"/>
                        <h1>
                            <field name="name" placeholder="Facility Name"/>
                        </h1>
                        <field name="code" readonly="1"/>
                    </div>

                    <group>
                        <group string="Facility Details">
                            <field name="partner_id"/>
                            <field name="phone" widget="phone"/>
                            <field name="email" widget="email"/>
                            <field name="website" widget="url"/>
                        </group>
                        <group string="Address">
                            <field name="street"/>
                            <field name="street2"/>
                            <field name="city"/>
                            <field name="state_id"/>
                            <field name="zip"/>
                            <field name="country_id"/>
                        </group>
                    </group>

                    <notebook>
                        <page string="Key Contacts" name="contacts">
                            <group>
                                <group>
                                    <field name="director_of_care_id"/>
                                    <field name="service_supervisor_id"/>
                                </group>
                                <group>
                                    <field name="physiotherapist_ids" widget="many2many_tags"/>
                                </group>
                            </group>
                        </page>

                        <page string="Floors &amp; Stations" name="structure">
                            <group>
                                <field name="number_of_floors"/>
                            </group>
                            <field name="floor_ids">
                                <list editable="bottom">
                                    <field name="sequence" widget="handle"/>
                                    <field name="name"/>
                                    <field name="head_nurse_id"/>
                                    <field name="physiotherapist_id"/>
                                </list>
                            </field>
                        </page>

                        <page string="Nursing Stations" name="stations">
                            <field name="floor_ids" mode="list">
                                <list>
                                    <field name="name" string="Floor"/>
                                    <field name="station_ids" widget="many2many_tags" string="Stations"/>
                                    <field name="head_nurse_id"/>
                                </list>
                            </field>
                        </page>

                        <page string="Contract" name="contract">
                            <group>
                                <group>
                                    <field name="contract_start_date"/>
                                    <field name="contract_end_date"/>
                                </group>
                                <group>
                                    <field name="cleanup_frequency"/>
                                    <field name="cleanup_interval_days"
                                           invisible="cleanup_frequency != 'custom'"/>
                                    <field name="next_cleanup_date"/>
                                </group>
                            </group>

                            <separator string="Contract Document"/>
                            <div class="row mb-3">
                                <div class="col-md-6">
                                    <div class="d-flex align-items-center gap-2">
                                        <field name="contract_file"
                                               filename="contract_file_filename"
                                               widget="binary" nolabel="1"/>
                                        <field name="contract_file_filename" invisible="1"/>
                                        <button name="action_preview_contract" type="object"
                                                class="btn btn-secondary"
                                                icon="fa-eye"
                                                string="Preview"
                                                invisible="not contract_file"/>
                                    </div>
                                </div>
                            </div>

                            <field name="contract_notes" placeholder="Contract details and notes..."/>
                        </page>

                        <page string="Notes" name="notes">
                            <field name="notes"/>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FACILITY - LIST VIEW                                                -->
    <!-- ================================================================== -->
    <record id="view_ltc_facility_list" model="ir.ui.view">
        <field name="name">fusion.ltc.facility.list</field>
        <field name="model">fusion.ltc.facility</field>
        <field name="arch" type="xml">
            <list default_order="name">
                <field name="code" optional="show"/>
                <field name="name"/>
                <field name="city"/>
                <field name="phone" optional="show"/>
                <field name="director_of_care_id" optional="show"/>
                <field name="service_supervisor_id" optional="hide"/>
                <field name="number_of_floors" optional="hide"/>
                <field name="contract_start_date" optional="hide"/>
                <field name="contract_end_date" optional="hide"/>
                <field name="cleanup_frequency" optional="show"/>
                <field name="next_cleanup_date" optional="show"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FACILITY - SEARCH VIEW                                              -->
    <!-- ================================================================== -->
    <record id="view_ltc_facility_search" model="ir.ui.view">
        <field name="name">fusion.ltc.facility.search</field>
        <field name="model">fusion.ltc.facility</field>
        <field name="arch" type="xml">
            <search string="Search Facilities">
                <field name="name" string="Facility"/>
                <field name="code"/>
                <field name="city"/>
                <field name="director_of_care_id"/>
                <separator/>
                <filter string="Active" name="filter_active"
                        domain="[('active', '=', True)]"/>
                <filter string="Archived" name="filter_archived"
                        domain="[('active', '=', False)]"/>
                <separator/>
                <filter string="Has Repairs" name="filter_has_repairs"
                        domain="[('repair_ids', '!=', False)]"/>
                <filter string="Cleanup Due" name="filter_cleanup_due"
                        domain="[('next_cleanup_date', '&lt;=', (context_today() + datetime.timedelta(days=7)).strftime('%Y-%m-%d')),
                                 ('next_cleanup_date', '!=', False)]"/>
                <separator/>
                <filter string="City" name="group_city" context="{'group_by': 'city'}"/>
                <filter string="Cleanup Frequency" name="group_frequency"
                        context="{'group_by': 'cleanup_frequency'}"/>
            </search>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- STATION - FORM VIEW (for inline editing within floors)              -->
    <!-- ================================================================== -->
    <record id="view_ltc_station_form" model="ir.ui.view">
        <field name="name">fusion.ltc.station.form</field>
        <field name="model">fusion.ltc.station</field>
        <field name="arch" type="xml">
            <form string="Nursing Station">
                <sheet>
                    <group>
                        <group>
                            <field name="name"/>
                            <field name="sequence"/>
                        </group>
                        <group>
                            <field name="head_nurse_id"/>
                            <field name="phone" widget="phone"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- STATION - LIST VIEW                                                 -->
    <!-- ================================================================== -->
    <record id="view_ltc_station_list" model="ir.ui.view">
        <field name="name">fusion.ltc.station.list</field>
        <field name="model">fusion.ltc.station</field>
        <field name="arch" type="xml">
            <list string="Nursing Stations" editable="bottom">
                <field name="sequence" widget="handle"/>
                <field name="floor_id"/>
                <field name="name"/>
                <field name="head_nurse_id"/>
                <field name="phone"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FLOOR - FORM VIEW (with embedded stations)                          -->
    <!-- ================================================================== -->
    <record id="view_ltc_floor_form" model="ir.ui.view">
        <field name="name">fusion.ltc.floor.form</field>
        <field name="model">fusion.ltc.floor</field>
        <field name="arch" type="xml">
            <form string="Floor">
                <sheet>
                    <group>
                        <group>
                            <field name="name"/>
                            <field name="sequence"/>
                            <field name="facility_id" invisible="1"/>
                        </group>
                        <group>
                            <field name="head_nurse_id"/>
                            <field name="physiotherapist_id"/>
                        </group>
                    </group>
                    <field name="station_ids">
                        <list editable="bottom">
                            <field name="sequence" widget="handle"/>
                            <field name="name"/>
                            <field name="head_nurse_id"/>
                            <field name="phone"/>
                        </list>
                    </field>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- ACTIONS                                                             -->
    <!-- ================================================================== -->
    <record id="action_ltc_facilities" model="ir.actions.act_window">
        <field name="name">LTC Facilities</field>
        <field name="res_model">fusion.ltc.facility</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_ltc_facility_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Add your first LTC Facility
            </p>
            <p>Create a facility record to start managing repairs and cleanups.</p>
        </field>
    </record>

</odoo>
