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

    <!-- ============================================================ -->
    <!-- PDF Template - Form View                                      -->
    <!-- ============================================================ -->
    <record id="fusion_pdf_template_form" model="ir.ui.view">
        <field name="name">fusion.pdf.template.form</field>
        <field name="model">fusion.pdf.template</field>
        <field name="arch" type="xml">
            <form string="PDF Template">
                <header>
                    <button name="action_activate" string="Activate" type="object"
                            class="btn-primary"
                            invisible="state != 'draft'"/>
                    <button name="action_archive" string="Archive" type="object"
                            class="btn-secondary"
                            invisible="state != 'active'"/>
                    <button name="action_reset_draft" string="Reset to Draft" type="object"
                            class="btn-secondary"
                            invisible="state != 'archived'"/>
                    <field name="state" widget="statusbar" statusbar_visible="draft,active"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="name" placeholder="e.g., ADP Page 11 - Consent"/>
                        </h1>
                    </div>
                    <group>
                        <group>
                            <field name="category"/>
                            <field name="version"/>
                            <field name="page_count"/>
                        </group>
                        <group>
                            <field name="pdf_file" filename="pdf_filename"/>
                            <field name="pdf_filename" invisible="1"/>
                            <field name="field_count"/>
                        </group>
                    </group>

                    <div class="alert alert-info" role="alert" style="margin: 10px 0;">
                        <strong>How to use:</strong>
                        1. Upload the agency's PDF form above.
                        2. Upload page preview images (screenshots/photos of each page) in the Previews tab.
                        3. Click "Open Field Editor" to visually position fields on the PDF.
                        4. Activate the template when ready.
                    </div>

                    <div class="d-flex gap-2 mb-3">
                        <button name="action_generate_previews" string="Generate Page Previews"
                                type="object" class="btn-secondary"
                                icon="fa-image"
                                invisible="not pdf_file"/>
                        <button name="action_open_field_editor" string="Open Field Editor"
                                type="object" class="btn-primary"
                                icon="fa-pencil-square-o"
                                invisible="not pdf_file"/>
                    </div>

                    <notebook>
                        <page string="Fields" name="fields">
                            <field name="field_ids">
                                <list editable="bottom">
                                    <field name="sequence" widget="handle"/>
                                    <field name="page"/>
                                    <field name="name"/>
                                    <field name="label"/>
                                    <field name="field_type"/>
                                    <field name="field_key"/>
                                    <field name="pos_x"/>
                                    <field name="pos_y"/>
                                    <field name="width"/>
                                    <field name="height"/>
                                    <field name="font_size"/>
                                    <field name="font_name"/>
                                    <field name="default_value"/>
                                    <field name="is_active"/>
                                </list>
                            </field>
                        </page>
                        <page string="Page Previews" name="previews">
                            <field name="preview_ids">
                                <list editable="bottom">
                                    <field name="page"/>
                                    <field name="image" widget="image" options="{'size': [200, 200]}"/>
                                    <field name="image_filename"/>
                                </list>
                            </field>
                        </page>
                        <page string="Notes" name="notes">
                            <field name="notes" placeholder="Usage notes, which assessments use this template..."/>
                        </page>
                    </notebook>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- PDF Template - List View                                      -->
    <!-- ============================================================ -->
    <record id="fusion_pdf_template_list" model="ir.ui.view">
        <field name="name">fusion.pdf.template.list</field>
        <field name="model">fusion.pdf.template</field>
        <field name="arch" type="xml">
            <list>
                <field name="name"/>
                <field name="category"/>
                <field name="version"/>
                <field name="page_count"/>
                <field name="field_count"/>
                <field name="state" widget="badge"
                       decoration-success="state == 'active'"
                       decoration-info="state == 'draft'"
                       decoration-muted="state == 'archived'"/>
            </list>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- PDF Template - Search View                                    -->
    <!-- ============================================================ -->
    <record id="fusion_pdf_template_search" model="ir.ui.view">
        <field name="name">fusion.pdf.template.search</field>
        <field name="model">fusion.pdf.template</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"/>
                <field name="category"/>
                <filter name="active_templates" string="Active" domain="[('state', '=', 'active')]"/>
                <filter name="draft_templates" string="Draft" domain="[('state', '=', 'draft')]"/>
                <separator/>
                <filter name="group_category" string="Agency" context="{'group_by': 'category'}"/>
                <filter name="group_state" string="Status" context="{'group_by': 'state'}"/>
            </search>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- PDF Template - Action                                         -->
    <!-- ============================================================ -->
    <record id="action_fusion_pdf_template" model="ir.actions.act_window">
        <field name="name">PDF Templates</field>
        <field name="res_model">fusion.pdf.template</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="fusion_pdf_template_search"/>
        <field name="context">{'search_default_active_templates': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first PDF template
            </p>
            <p>
                Upload a funding agency's PDF form, position fields on it using the
                visual editor, and generate filled PDFs automatically from assessment data.
            </p>
        </field>
    </record>

    <!-- ============================================================ -->
    <!-- Menu Item (under Fusion Claims > Configuration)               -->
    <!-- ============================================================ -->
    <!-- Direct under Fusion Claims > Configuration -->
    <menuitem id="menu_fusion_pdf_templates"
              name="PDF Templates"
              parent="fusion_claims.menu_adp_config"
              action="action_fusion_pdf_template"
              sequence="40"/>

</odoo>
