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

    <!-- Assessment Tree View -->
    <record id="view_fusion_assessment_tree" model="ir.ui.view">
        <field name="name">fusion.assessment.tree</field>
        <field name="model">fusion.assessment</field>
        <field name="arch" type="xml">
            <list string="Assessments" default_order="assessment_date desc, id desc"
                  decoration-info="state == 'draft'"
                  decoration-warning="state == 'pending_signature'"
                  decoration-success="state == 'completed'"
                  decoration-muted="state == 'cancelled'">
                <field name="reference"/>
                <field name="client_name"/>
                <field name="equipment_type" optional="show"/>
                <field name="client_type" optional="show"/>
                <field name="assessment_date"/>
                <field name="sales_rep_id"/>
                <field name="authorizer_id"/>
                <field name="reason_for_application" optional="hide"/>
                <field name="state" widget="badge"
                       decoration-info="state == 'draft'"
                       decoration-warning="state == 'pending_signature'"
                       decoration-success="state == 'completed'"
                       decoration-danger="state == 'cancelled'"/>
                <field name="signatures_complete" widget="boolean" optional="show"/>
                <field name="sale_order_id" optional="show"/>
            </list>
        </field>
    </record>

    <!-- Assessment Form View -->
    <record id="view_fusion_assessment_form" model="ir.ui.view">
        <field name="name">fusion.assessment.form</field>
        <field name="model">fusion.assessment</field>
        <field name="arch" type="xml">
            <form string="Assessment">
                <header>
                    <button name="action_mark_pending_signature" type="object"
                            string="Mark Pending Signature" class="btn-primary"
                            invisible="state != 'draft'"/>
                    <button name="action_complete" type="object"
                            string="Complete Assessment" class="btn-success"
                            invisible="state not in ['draft', 'pending_signature']"/>
                    <button name="action_complete_express" type="object"
                            string="Express Complete" class="btn-warning"
                            invisible="state not in ['draft', 'pending_signature']"
                            confirm="This will complete the assessment without requiring signatures. Continue?"/>
                    <button name="action_cancel" type="object"
                            string="Cancel"
                            invisible="state in ['completed', 'cancelled']"/>
                    <button name="action_reset_draft" type="object"
                            string="Reset to Draft"
                            invisible="state != 'cancelled'"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,pending_signature,completed"/>
                </header>
                <sheet>
                    <div class="oe_button_box" name="button_box">
                        <button name="action_view_documents" type="object"
                                class="oe_stat_button" icon="fa-file-pdf-o">
                            <field name="document_count" string="Documents" widget="statinfo"/>
                        </button>
                        <button name="action_view_sale_order" type="object"
                                class="oe_stat_button" icon="fa-shopping-cart"
                                invisible="not sale_order_id">
                            <span class="o_stat_text">Sale Order</span>
                        </button>
                    </div>

                    <widget name="web_ribbon" title="Completed" bg_color="text-bg-success"
                            invisible="state != 'completed'"/>
                    <widget name="web_ribbon" title="Cancelled" bg_color="text-bg-danger"
                            invisible="state != 'cancelled'"/>

                    <div class="oe_title mb-3">
                        <h1>
                            <field name="reference" readonly="1" class="me-3"/>
                        </h1>
                        <h2 class="text-muted" invisible="not client_name">
                            <field name="client_name" readonly="1"/>
                        </h2>
                    </div>

                    <!-- ============ TOP SUMMARY ============ -->
                    <group>
                        <group string="Equipment">
                            <field name="equipment_type"/>
                            <field name="rollator_type" invisible="equipment_type != 'rollator'"/>
                            <field name="wheelchair_type" invisible="equipment_type != 'wheelchair'"/>
                            <field name="powerchair_type" invisible="equipment_type != 'powerchair'"/>
                            <field name="client_type"/>
                            <field name="reason_for_application"/>
                            <field name="previous_funding_date"
                                   invisible="reason_for_application not in ['replace_status','replace_size','replace_worn','replace_lost','replace_stolen','replace_damaged','replace_no_longer_meets']"/>
                        </group>
                        <group string="Assessment Info">
                            <field name="assessment_date"/>
                            <field name="assessment_location"/>
                            <field name="assessment_location_notes"/>
                            <field name="sales_rep_id"/>
                            <field name="authorizer_id"/>
                            <field name="sale_order_id" readonly="1"/>
                        </group>
                    </group>

                    <notebook>

                        <!-- ============ CLIENT INFORMATION ============ -->
                        <page string="Client" name="client_info">
                            <group>
                                <group string="Personal Details">
                                    <field name="client_first_name"/>
                                    <field name="client_middle_name"/>
                                    <field name="client_last_name"/>
                                    <field name="client_dob"/>
                                    <field name="client_phone"/>
                                    <field name="client_mobile"/>
                                    <field name="client_email" widget="email"/>
                                </group>
                                <group string="Health Card">
                                    <field name="client_health_card"/>
                                    <field name="client_health_card_version"/>
                                    <field name="client_weight"/>
                                    <field name="client_height"/>
                                </group>
                            </group>
                            <group>
                                <group string="Address">
                                    <field name="client_street"/>
                                    <field name="client_unit"/>
                                    <field name="client_city"/>
                                    <field name="client_state"/>
                                    <field name="client_postal_code"/>
                                    <field name="client_country_id"/>
                                </group>
                                <group string="References &amp; Linking">
                                    <field name="client_reference_1"/>
                                    <field name="client_reference_2"/>
                                    <field name="partner_id"/>
                                    <field name="create_new_partner"/>
                                </group>
                            </group>
                        </page>

                        <!-- ============ MEASUREMENTS & SPECS ============ -->
                        <page string="Measurements" name="measurements">

                            <!-- Rollator Measurements -->
                            <group string="Rollator Measurements"
                                   invisible="equipment_type != 'rollator'">
                                <group>
                                    <field name="rollator_handle_height"/>
                                    <field name="rollator_seat_height"/>
                                </group>
                                <group>
                                    <field name="rollator_addons" placeholder="e.g. Basket, Tray, Backrest pad..."/>
                                </group>
                            </group>

                            <!-- Wheelchair / Powerchair Measurements -->
                            <group string="Seat Measurements"
                                   invisible="equipment_type not in ['wheelchair', 'powerchair']">
                                <group>
                                    <field name="seat_width"/>
                                    <field name="seat_depth"/>
                                    <field name="seat_to_floor_height"/>
                                    <field name="seat_angle"/>
                                </group>
                                <group>
                                    <field name="back_height"/>
                                    <field name="back_angle"/>
                                    <field name="armrest_height"/>
                                </group>
                            </group>

                            <group string="Leg &amp; Foot"
                                   invisible="equipment_type not in ['wheelchair', 'powerchair']">
                                <group>
                                    <field name="footrest_length"/>
                                    <field name="legrest_length"/>
                                    <field name="cane_height"/>
                                </group>
                            </group>

                            <group string="Overall Dimensions"
                                   invisible="equipment_type not in ['wheelchair', 'powerchair']">
                                <group>
                                    <field name="overall_width"/>
                                    <field name="overall_length"/>
                                    <field name="overall_height"/>
                                </group>
                            </group>

                        </page>

                        <!-- ============ OPTIONS & ACCESSORIES ============ -->
                        <page string="Options" name="options"                              invisible="equipment_type not in ['wheelchair', 'powerchair']">

                            <group invisible="equipment_type != 'wheelchair'">
                                <group string="Frame Options">
                                    <field name="frame_options" nolabel="1"
                                           placeholder="e.g. Recliner Option, Dynamic Tilt Frame, Titanium Frame"/>
                                </group>
                                <group string="Wheel Options">
                                    <field name="wheel_options" nolabel="1"
                                           placeholder="e.g. Quick Release Axle, Mag Wheels, Anti-Tip..."/>
                                </group>
                            </group>

                            <group invisible="equipment_type != 'wheelchair'">
                                <group string="Legrest Accessories">
                                    <field name="legrest_options" nolabel="1"
                                           placeholder="e.g. Elevating Legrest, Swing Away..."/>
                                </group>
                                <group string="Additional ADP Options">
                                    <field name="additional_adp_options" nolabel="1"/>
                                </group>
                            </group>

                            <group invisible="equipment_type != 'powerchair'">
                                <group string="Powerchair Options">
                                    <field name="powerchair_options" nolabel="1"/>
                                </group>
                                <group string="Specialty Controls">
                                    <field name="specialty_controls" nolabel="1"
                                           placeholder="Rationale required for specialty components"/>
                                </group>
                            </group>

                            <group>
                                <group string="Seating">
                                    <field name="seatbelt_type"/>
                                    <field name="cushion_info"/>
                                    <field name="backrest_info"/>
                                </group>
                                <group string="Additional Customization">
                                    <field name="additional_customization" nolabel="1"
                                           placeholder="Free-form notes for any customization..."/>
                                </group>
                            </group>
                        </page>

                        <!-- ============ PRODUCT TYPES ============ -->
                        <page string="Product Types" name="products">
                            <group>
                                <group string="Cushion">
                                    <field name="cushion_type"/>
                                    <field name="cushion_notes" placeholder="Cushion details..."
                                           invisible="not cushion_type"/>
                                </group>
                                <group string="Backrest">
                                    <field name="backrest_type"/>
                                    <field name="backrest_notes" placeholder="Backrest details..."
                                           invisible="not backrest_type"/>
                                </group>
                            </group>
                            <group>
                                <group string="Frame">
                                    <field name="frame_type"/>
                                    <field name="frame_notes" placeholder="Frame details..."
                                           invisible="not frame_type"/>
                                </group>
                                <group string="Wheels">
                                    <field name="wheel_type"/>
                                    <field name="wheel_notes" placeholder="Wheel details..."
                                           invisible="not wheel_type"/>
                                </group>
                            </group>
                        </page>

                        <!-- ============ CLINICAL NOTES ============ -->
                        <page string="Clinical Notes" name="needs">
                            <group>
                                <group>
                                    <field name="diagnosis" placeholder="Relevant medical diagnosis or conditions..."/>
                                </group>
                                <group>
                                    <field name="mobility_notes" placeholder="Document mobility needs and challenges..."/>
                                </group>
                            </group>
                            <group>
                                <group>
                                    <field name="accessibility_notes" placeholder="Accessibility requirements and home environment..."/>
                                </group>
                                <group>
                                    <field name="special_requirements" placeholder="Any special requirements or customizations..."/>
                                </group>
                            </group>
                        </page>

                        <!-- ============ KEY DATES ============ -->
                        <page string="Dates" name="dates">
                            <group>
                                <group string="Assessment Period">
                                    <field name="assessment_start_date"/>
                                    <field name="assessment_end_date"/>
                                </group>
                                <group string="Authorization">
                                    <field name="claim_authorization_date"/>
                                </group>
                            </group>
                        </page>

                        <!-- ============ CONSENT & DECLARATION (PAGE 11) ============ -->
                        <page string="Consent &amp; Declaration" name="consent">
                            <group>
                                <group string="Consent Details">
                                    <field name="consent_signed_by"/>
                                    <field name="consent_declaration_accepted"/>
                                    <field name="consent_date"/>
                                </group>
                            </group>

                            <group string="Agent Details"
                                   invisible="consent_signed_by != 'agent'">
                                <group>
                                    <field name="agent_relationship"/>
                                    <field name="agent_first_name"/>
                                    <field name="agent_middle_initial"/>
                                    <field name="agent_last_name"/>
                                </group>
                                <group>
                                    <field name="agent_street_number"/>
                                    <field name="agent_street_name"/>
                                    <field name="agent_unit"/>
                                    <field name="agent_city"/>
                                    <field name="agent_province"/>
                                    <field name="agent_postal_code"/>
                                </group>
                            </group>
                            <group string="Agent Contact"
                                   invisible="consent_signed_by != 'agent'">
                                <group>
                                    <field name="agent_home_phone"/>
                                    <field name="agent_business_phone"/>
                                    <field name="agent_phone_ext"/>
                                </group>
                            </group>
                        </page>

                        <!-- ============ SIGNATURES ============ -->
                        <page string="Signatures" name="signatures">
                            <group>
                                <group string="Page 11 - Authorizer Signature">
                                    <field name="signature_page_11" widget="image" class="oe_avatar"/>
                                    <field name="signature_page_11_name"/>
                                    <field name="signature_page_11_date"/>
                                </group>
                                <group string="Page 12 - Client Signature">
                                    <field name="signature_page_12" widget="image" class="oe_avatar"/>
                                    <field name="signature_page_12_name"/>
                                    <field name="signature_page_12_date"/>
                                </group>
                            </group>
                            <group>
                                <field name="signatures_complete" readonly="1"/>
                                <field name="signed_page_11_pdf" filename="signed_page_11_pdf_filename"
                                       invisible="not signed_page_11_pdf"/>
                                <field name="signed_page_11_pdf_filename" invisible="1"/>
                            </group>
                        </page>

                        <!-- ============ DOCUMENTS ============ -->
                        <page string="Documents" name="documents">
                            <field name="document_ids">
                                <list string="Documents" editable="bottom">
                                    <field name="document_type"/>
                                    <field name="filename"/>
                                    <field name="revision"/>
                                    <field name="upload_date"/>
                                    <field name="uploaded_by"/>
                                </list>
                            </field>
                        </page>

                        <!-- ============ COMMENTS ============ -->
                        <page string="Comments" name="comments">
                            <field name="comment_ids">
                                <list string="Comments" editable="bottom">
                                    <field name="create_date" string="Date"/>
                                    <field name="author_id"/>
                                    <field name="comment"/>
                                </list>
                            </field>
                        </page>

                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- Assessment Search View -->
    <record id="view_fusion_assessment_search" model="ir.ui.view">
        <field name="name">fusion.assessment.search</field>
        <field name="model">fusion.assessment</field>
        <field name="arch" type="xml">
            <search string="Search Assessments">
                <field name="reference"/>
                <field name="client_name"/>
                <field name="client_email"/>
                <field name="client_health_card"/>
                <field name="sales_rep_id"/>
                <field name="authorizer_id"/>
                <field name="sale_order_id"/>
                <separator/>
                <filter string="In Progress" name="draft" domain="[('state', '=', 'draft')]"/>
                <filter string="Pending Signature" name="pending" domain="[('state', '=', 'pending_signature')]"/>
                <filter string="Completed" name="completed" domain="[('state', '=', 'completed')]"/>
                <filter string="Cancelled" name="cancelled" domain="[('state', '=', 'cancelled')]"/>
                <separator/>
                <filter string="My Assessments" name="my_assessments" domain="[('sales_rep_id', '=', uid)]"/>
                <filter string="Has Sale Order" name="has_so" domain="[('sale_order_id', '!=', False)]"/>
                <filter string="Signatures Pending" name="sigs_pending" domain="[('signatures_complete', '=', False), ('state', '!=', 'cancelled')]"/>
                <separator/>
                <filter string="Wheelchair" name="filter_wheelchair" domain="[('equipment_type', '=', 'wheelchair')]"/>
                <filter string="Powerchair" name="filter_powerchair" domain="[('equipment_type', '=', 'powerchair')]"/>
                <filter string="Rollator" name="filter_rollator" domain="[('equipment_type', '=', 'rollator')]"/>
                <separator/>
                <filter string="Status" name="group_state" context="{'group_by': 'state'}"/>
                <filter string="Equipment Type" name="group_equipment" context="{'group_by': 'equipment_type'}"/>
                <filter string="Client Type" name="group_client_type" context="{'group_by': 'client_type'}"/>
                <filter string="Sales Rep" name="group_sales_rep" context="{'group_by': 'sales_rep_id'}"/>
                <filter string="Authorizer" name="group_authorizer" context="{'group_by': 'authorizer_id'}"/>
                <filter string="Month" name="group_date" context="{'group_by': 'assessment_date:month'}"/>
            </search>
        </field>
    </record>

    <!-- Assessment Action -->
    <record id="action_fusion_assessment" model="ir.actions.act_window">
        <field name="name">Assessments</field>
        <field name="res_model">fusion.assessment</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_fusion_assessment_search"/>
        <field name="context">{'search_default_my_assessments': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first assessment
            </p>
            <p>
                Assessments record wheelchair, powerchair, and rollator specifications
                along with client needs. Once completed, a draft sale order is created.
            </p>
        </field>
    </record>

    <!-- Menu Items -->
    <menuitem id="menu_fusion_assessment_root"
              name="Assessments"
              parent="fusion_claims.menu_adp_claims_root"
              sequence="42"/>

    <menuitem id="menu_fusion_assessment_list"
              name="All Assessments"
              parent="menu_fusion_assessment_root"
              action="action_fusion_assessment"
              sequence="10"/>

</odoo>
