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

    <!-- Reference Tag List View -->
    <record id="otk_seo_reference_tag_view_tree" model="ir.ui.view">
        <field name="name">otk.seo.reference.tag.list</field>
        <field name="model">otk.seo.reference.tag</field>
        <field name="arch" type="xml">
            <list string="Reference Tags" editable="bottom">
                <field name="name"/>
                <field name="color" widget="color_picker"/>
            </list>
        </field>
    </record>

    <!-- Reference List View -->
    <record id="otk_seo_reference_view_tree" model="ir.ui.view">
        <field name="name">otk.seo.reference.list</field>
        <field name="model">otk.seo.reference</field>
        <field name="arch" type="xml">
            <list string="Reference Library"
                  decoration-danger="reference_type == 'url' and url_fetch_status == 'error'">
                <field name="sequence" widget="handle"/>
                <field name="name"/>
                <field name="reference_type" widget="badge"
                    decoration-info="reference_type == 'internal_content'"
                    decoration-success="reference_type == 'url'"
                    decoration-warning="reference_type == 'text'"
                    decoration-muted="reference_type == 'file'"/>
                <field name="url_fetch_status" string="URL Status" widget="badge"
                    decoration-info="url_fetch_status == 'pending'"
                    decoration-warning="url_fetch_status == 'fetching'"
                    decoration-success="url_fetch_status == 'success'"
                    decoration-danger="url_fetch_status == 'error'"
                    optional="show"/>
                <field name="content_type"/>
                <field name="tag_ids" widget="many2many_tags"
                    options="{'color_field': 'color'}"/>
                <field name="content_length"/>
                <field name="active" column_invisible="1"/>
            </list>
        </field>
    </record>

    <!-- Reference Form View -->
    <record id="otk_seo_reference_view_form" model="ir.ui.view">
        <field name="name">otk.seo.reference.form</field>
        <field name="model">otk.seo.reference</field>
        <field name="arch" type="xml">
            <form string="Reference">
                <sheet>
                    <widget name="web_ribbon" title="Archived"
                        bg_color="text-bg-danger" invisible="active"/>

                    <div class="oe_title">
                        <h1>
                            <field name="name" placeholder="Reference Name"/>
                        </h1>
                    </div>

                    <group>
                        <group string="Reference Settings">
                            <field name="reference_type" widget="radio"
                                options="{'horizontal': true}"/>
                            <field name="content_type"/>
                            <field name="sequence"/>
                            <field name="active" invisible="1"/>
                        </group>
                        <group string="Organization">
                            <field name="tag_ids" widget="many2many_tags"
                                options="{'color_field': 'color', 'no_create_edit': False}"/>
                        </group>
                    </group>

                    <group string="Reference Content">
                        <!-- Internal Content -->
                        <field name="internal_content_id"
                            invisible="reference_type != 'internal_content'"
                            required="reference_type == 'internal_content'"
                            options="{'no_create': True}"/>

                        <!-- URL -->
                        <field name="url" widget="url"
                            invisible="reference_type != 'url'"
                            required="reference_type == 'url'"
                            placeholder="https://example.com/article"/>

                        <!-- URL Fetch Status -->
                        <div invisible="reference_type != 'url'" class="d-flex align-items-center gap-2 mb-2" colspan="2">
                            <field name="url_fetch_status" widget="badge"
                                decoration-info="url_fetch_status == 'pending'"
                                decoration-warning="url_fetch_status == 'fetching'"
                                decoration-success="url_fetch_status == 'success'"
                                decoration-danger="url_fetch_status == 'error'"/>
                            <field name="url_fetch_date" readonly="1" class="text-muted"/>
                            <button name="action_fetch_url_content" type="object"
                                string="Refresh Content" icon="fa-refresh"
                                class="btn-sm btn-secondary"
                                invisible="not url"/>
                        </div>
                        <field name="url_fetch_error" readonly="1"
                            invisible="reference_type != 'url' or url_fetch_status != 'error'"
                            class="text-danger"/>
                        <field name="url_content_type" readonly="1"
                            invisible="reference_type != 'url' or url_fetch_status != 'success'"/>
                        <field name="url_fetched_content" readonly="1"
                            invisible="reference_type != 'url' or url_fetch_status != 'success'"
                            placeholder="Content will be fetched from URL automatically..."/>

                        <!-- Text -->
                        <field name="text_content"
                            invisible="reference_type != 'text'"
                            required="reference_type == 'text'"
                            placeholder="Paste reference text content here..."/>

                        <!-- File -->
                        <field name="file" filename="file_name"
                            invisible="reference_type != 'file'"
                            required="reference_type == 'file'"/>
                        <field name="file_name" invisible="1"/>
                    </group>

                    <group string="Description">
                        <field name="description" nolabel="1" colspan="2"
                            placeholder="Brief description of this reference and when to use it..."/>
                    </group>

                    <group string="Content Preview" invisible="not content_preview">
                        <field name="content_length" string="Content Length (chars)"/>
                        <field name="content_preview" nolabel="1" readonly="1"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- Reference Search View -->
    <record id="otk_seo_reference_view_search" model="ir.ui.view">
        <field name="name">otk.seo.reference.search</field>
        <field name="model">otk.seo.reference</field>
        <field name="arch" type="xml">
            <search string="Search References">
                <field name="name"/>
                <field name="description"/>
                <field name="tag_ids"/>
                <separator/>
                <filter name="filter_internal" string="Internal Content"
                    domain="[('reference_type', '=', 'internal_content')]"/>
                <filter name="filter_url" string="URLs"
                    domain="[('reference_type', '=', 'url')]"/>
                <filter name="filter_url_error" string="URL Fetch Errors"
                    domain="[('reference_type', '=', 'url'), ('url_fetch_status', '=', 'error')]"/>
                <filter name="filter_text" string="Text"
                    domain="[('reference_type', '=', 'text')]"/>
                <filter name="filter_file" string="Files"
                    domain="[('reference_type', '=', 'file')]"/>
                <separator/>
                <filter name="filter_blog" string="For Blog Posts"
                    domain="['|', ('content_type', '=', 'blog_post'), ('content_type', '=', 'all')]"/>
                <filter name="filter_product" string="For Products"
                    domain="['|', ('content_type', '=', 'product_desc'), ('content_type', '=', 'all')]"/>
                <separator/>
                <filter name="filter_active" string="Active"
                    domain="[('active', '=', True)]"/>
                <filter name="filter_archived" string="Archived"
                    domain="[('active', '=', False)]"/>
                <group>
                    <filter name="group_type" string="Type"
                        context="{'group_by': 'reference_type'}"/>
                    <filter name="group_content_type" string="Content Type"
                        context="{'group_by': 'content_type'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- Actions -->
    <record id="action_otk_seo_reference" model="ir.actions.act_window">
        <field name="name">Reference Library</field>
        <field name="res_model">otk.seo.reference</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="otk_seo_reference_view_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create your first reference!
            </p>
            <p>
                Build a library of reference materials that AI can use
                when generating content. Include URLs, text snippets,
                existing content, or uploaded documents.
            </p>
        </field>
    </record>

    <record id="action_otk_seo_reference_tag" model="ir.actions.act_window">
        <field name="name">Reference Tags</field>
        <field name="res_model">otk.seo.reference.tag</field>
        <field name="view_mode">list</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Create tags to organize references!
            </p>
        </field>
    </record>

</odoo>
