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

    <!-- SEO Content Image List View -->
    <record id="otk_seo_content_image_view_tree" model="ir.ui.view">
        <field name="name">otk.seo.content.image.list</field>
        <field name="model">otk.seo.content.image</field>
        <field name="arch" type="xml">
            <list string="Generated Images"
                decoration-info="state == 'draft'"
                decoration-warning="state in ('pending', 'processing')"
                decoration-success="state == 'done'"
                decoration-danger="state == 'error'">
                <field name="sequence" widget="handle"/>
                <field name="image" widget="image" options="{'size': [64, 64]}"/>
                <field name="name"/>
                <field name="content_id"/>
                <field name="style"/>
                <field name="aspect_ratio"/>
                <field name="state" widget="badge"/>
                <field name="is_cover"/>
                <field name="is_optimized" widget="boolean"/>
                <field name="size_reduction" optional="hide"/>
                <field name="alt_text"/>
                <field name="token_cost" optional="hide"/>
            </list>
        </field>
    </record>

    <!-- SEO Content Image Form View -->
    <record id="otk_seo_content_image_view_form" model="ir.ui.view">
        <field name="name">otk.seo.content.image.form</field>
        <field name="model">otk.seo.content.image</field>
        <field name="arch" type="xml">
            <form string="Generated Image">
                <header>
                    <button name="action_generate" type="object" string="Generate"
                        class="oe_highlight" invisible="state not in ('draft', 'pending')"/>
                    <button name="action_regenerate" type="object" string="Regenerate"
                        class="oe_highlight" icon="fa-refresh"
                        invisible="state not in ('done', 'error')"
                        confirm="This will generate a new image with the current settings. The previous image will be saved as backup. Continue?"/>
                    <button name="action_poll_status" type="object" string="Check Status"
                        invisible="state != 'processing'"/>
                    <button name="action_retry" type="object" string="Retry"
                        invisible="state != 'error'"/>
                    <button name="action_optimize" type="object" string="Optimize for Web"
                        class="btn-secondary" invisible="state != 'done' or is_optimized"/>
                    <button name="action_restore_original" type="object" string="Restore Original"
                        class="btn-secondary" invisible="not image_original"/>
                    <field name="state" widget="statusbar"
                        statusbar_visible="draft,pending,processing,done"/>
                </header>
                <sheet>
                    <widget name="web_ribbon" title="Error"
                        bg_color="text-bg-danger" invisible="state != 'error'"/>
                    <widget name="web_ribbon" title="Optimized"
                            bg_color="text-bg-success" invisible="not is_optimized"/>

                    <group>
                        <group string="Image">
                            <field name="image" widget="image"
                                options="{'size': [400, 400]}"
                                invisible="state != 'done'"/>
                            <field name="image_original" widget="image"
                                options="{'size': [200, 200]}" readonly="1"
                                invisible="not image_original"
                                string="Previous Image (Backup)"/>
                            <field name="name"/>
                            <field name="content_id" readonly="state != 'draft'"/>
                        </group>
                        <group string="Generation Settings">
                            <field name="style" readonly="state == 'processing'"/>
                            <field name="aspect_ratio" readonly="state == 'processing'"/>
                            <field name="quality" readonly="state == 'processing'"/>
                            <field name="is_cover"/>
                            <field name="is_og_image"/>
                        </group>
                    </group>

                    <group string="Generation Prompt">
                        <field name="prompt" placeholder="Describe the image you want to generate..."
                            readonly="state == 'processing'"/>
                        <field name="negative_prompt" placeholder="Things to avoid in the image..."
                            readonly="state == 'processing'"/>
                    </group>

                    <div class="alert alert-info" role="alert" title="Regenerate" invisible="state not in ('done', 'error')">
                        <i class="fa fa-info-circle me-2"/>
                        <strong>Want a different image?</strong>
                        Modify the prompt, style, or settings above, then click <strong>Regenerate</strong>.
                        Your current image will be saved as backup.
                    </div>

                    <group invisible="state != 'done'">
                        <group string="Result">
                            <field name="revised_prompt" readonly="1"/>
                            <field name="width"/>
                            <field name="height"/>
                        </group>
                        <group string="SEO">
                            <field name="alt_text" placeholder="SEO-friendly alt text"/>
                            <field name="inserted_in_content"/>
                        </group>
                    </group>

                    <group string="Optimization" invisible="state != 'done'">
                        <group>
                            <field name="is_optimized" widget="boolean_toggle" readonly="1"/>
                            <field name="optimization_quality" invisible="is_optimized"/>
                            <field name="original_size" readonly="1" widget="integer"
                                invisible="not original_size"/>
                            <field name="optimized_size" readonly="1" widget="integer"
                                invisible="not is_optimized"/>
                        </group>
                        <group>
                            <field name="size_reduction" readonly="1" widget="progressbar"
                                invisible="not is_optimized"/>
                            <field name="webp_size" readonly="1" widget="integer"
                                invisible="not is_optimized"/>
                        </group>
                    </group>

                    <group string="Tracking">
                        <group>
                            <field name="task_id" readonly="1"/>
                            <field name="token_cost" readonly="1"/>
                        </group>
                        <group>
                            <field name="error_message" readonly="1"
                                invisible="not error_message"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- SEO Content Image Search View -->
    <record id="otk_seo_content_image_view_search" model="ir.ui.view">
        <field name="name">otk.seo.content.image.search</field>
        <field name="model">otk.seo.content.image</field>
        <field name="arch" type="xml">
            <search string="Search Images">
                <field name="name"/>
                <field name="content_id"/>
                <field name="prompt"/>
                <separator/>
                <filter name="filter_done" string="Generated"
                    domain="[('state', '=', 'done')]"/>
                <filter name="filter_pending" string="Pending"
                    domain="[('state', 'in', ('pending', 'processing'))]"/>
                <filter name="filter_error" string="Failed"
                    domain="[('state', '=', 'error')]"/>
                <separator/>
                <filter name="filter_cover" string="Cover Images"
                    domain="[('is_cover', '=', True)]"/>
                <filter name="filter_optimized" string="Optimized"
                    domain="[('is_optimized', '=', True)]"/>
                <filter name="filter_not_optimized" string="Not Optimized"
                    domain="[('is_optimized', '=', False), ('state', '=', 'done')]"/>
                <group>
                    <filter name="group_state" string="Status"
                        context="{'group_by': 'state'}"/>
                    <filter name="group_style" string="Style"
                        context="{'group_by': 'style'}"/>
                    <filter name="group_content" string="Content"
                        context="{'group_by': 'content_id'}"/>
                </group>
            </search>
        </field>
    </record>

    <!-- SEO Content Image Kanban View -->
    <record id="otk_seo_content_image_view_kanban" model="ir.ui.view">
        <field name="name">otk.seo.content.image.kanban</field>
        <field name="model">otk.seo.content.image</field>
        <field name="arch" type="xml">
            <kanban>
                <field name="name"/>
                <field name="image"/>
                <field name="state"/>
                <field name="is_cover"/>
                <templates>
                    <t t-name="card">
                        <div class="oe_kanban_card oe_kanban_global_click">
                            <div class="o_kanban_image">
                                <field name="image" widget="image"
                                    options="{'size': [100, 100]}"/>
                            </div>
                            <div class="oe_kanban_content">
                                <div class="o_kanban_record_title">
                                    <field name="name"/>
                                </div>
                                <div>
                                    <field name="state" widget="badge"/>
                                    <span t-if="record.is_cover.raw_value"
                                        class="badge text-bg-info ms-1">Cover</span>
                                </div>
                            </div>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <!-- Action -->
    <record id="action_otk_seo_content_image" model="ir.actions.act_window">
        <field name="name">Generated Images</field>
        <field name="res_model">otk.seo.content.image</field>
        <field name="view_mode">kanban,list,form</field>
        <field name="search_view_id" ref="otk_seo_content_image_view_search"/>
        <field name="context">{}</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No images generated yet
            </p>
            <p>
                Images are automatically generated when you create SEO content
                with the image generation option enabled.
            </p>
        </field>
    </record>

</odoo>
