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

    <!-- ================================================================== -->
    <!-- SEQUENCE                                                            -->
    <!-- ================================================================== -->
    <record id="seq_technician_task" model="ir.sequence">
        <field name="name">Technician Task</field>
        <field name="code">fusion.technician.task</field>
        <field name="prefix">TASK-</field>
        <field name="padding">5</field>
        <field name="number_increment">1</field>
    </record>

    <!-- ================================================================== -->
    <!-- RES.USERS FORM EXTENSION - Field Staff toggle                       -->
    <!-- ================================================================== -->
    <record id="view_users_form_field_staff" model="ir.ui.view">
        <field name="name">res.users.form.field.staff</field>
        <field name="model">res.users</field>
        <field name="inherit_id" ref="base.view_users_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='login']" position="after">
                <field name="x_fc_is_field_staff"/>
                <field name="x_fc_start_address"
                       invisible="not x_fc_is_field_staff"
                       placeholder="e.g. 123 Main St, Brampton, ON"/>
                <field name="x_fc_tech_sync_id"
                       invisible="not x_fc_is_field_staff"
                       placeholder="e.g. gordy, manpreet"/>
            </xpath>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- SEARCH VIEW                                                         -->
    <!-- ================================================================== -->
    <record id="view_technician_task_search" model="ir.ui.view">
        <field name="name">fusion.technician.task.search</field>
        <field name="model">fusion.technician.task</field>
        <field name="arch" type="xml">
            <search string="Search Tasks">
                <field name="technician_id" string="Technician"/>
                <field name="partner_id" string="Client"/>
                <field name="name" string="Task"/>
                <separator/>
                <!-- Quick Filters -->
                <filter string="Today" name="filter_today"
                        domain="[('scheduled_date', '=', context_today().strftime('%Y-%m-%d'))]"/>
                <filter string="Tomorrow" name="filter_tomorrow"
                        domain="[('scheduled_date', '=', (context_today() + datetime.timedelta(days=1)).strftime('%Y-%m-%d'))]"/>
                <filter string="This Week" name="filter_this_week"
                        domain="[('scheduled_date', '>=', (context_today() - datetime.timedelta(days=context_today().weekday())).strftime('%Y-%m-%d')),
                                 ('scheduled_date', '&lt;=', (context_today() + datetime.timedelta(days=6-context_today().weekday())).strftime('%Y-%m-%d'))]"/>
                <separator/>
                <filter string="Pending" name="filter_pending" domain="[('status', '=', 'pending')]"/>
                <filter string="Scheduled" name="filter_scheduled" domain="[('status', '=', 'scheduled')]"/>
                <filter string="En Route" name="filter_en_route" domain="[('status', '=', 'en_route')]"/>
                <filter string="In Progress" name="filter_in_progress" domain="[('status', '=', 'in_progress')]"/>
                <filter string="Completed" name="filter_completed" domain="[('status', '=', 'completed')]"/>
                <filter string="Active" name="filter_active" domain="[('status', 'not in', ['cancelled', 'completed'])]"/>
                <separator/>
                <filter string="My Tasks" name="filter_my_tasks"
                        domain="['|', ('technician_id', '=', uid), ('additional_technician_ids', 'in', [uid])]"/>
                <filter string="Deliveries" name="filter_deliveries" domain="[('task_type', '=', 'delivery')]"/>
                <filter string="Repairs" name="filter_repairs" domain="[('task_type', '=', 'repair')]"/>
                <filter string="POD Required" name="filter_pod" domain="[('pod_required', '=', True)]"/>
                <separator/>
                <filter string="Local Tasks" name="filter_local"
                        domain="[('x_fc_sync_source', '=', False)]"/>
                <filter string="Synced Tasks" name="filter_synced"
                        domain="[('x_fc_sync_source', '!=', False)]"/>
                <separator/>
                <!-- Group By -->
                <filter string="Technician" name="group_technician" context="{'group_by': 'technician_id'}"/>
                <filter string="Date" name="group_date" context="{'group_by': 'scheduled_date'}"/>
                <filter string="Status" name="group_status" context="{'group_by': 'status'}"/>
                <filter string="Task Type" name="group_type" context="{'group_by': 'task_type'}"/>
                <filter string="Client" name="group_client" context="{'group_by': 'partner_id'}"/>
            </search>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FORM VIEW                                                           -->
    <!-- ================================================================== -->
    <record id="view_technician_task_form" model="ir.ui.view">
        <field name="name">fusion.technician.task.form</field>
        <field name="model">fusion.technician.task</field>
        <field name="arch" type="xml">
            <form string="Technician Task">
                <field name="x_fc_is_shadow" invisible="1"/>
                <field name="x_fc_sync_source" invisible="1"/>
                <header>
                    <button name="action_start_en_route" type="object" string="En Route"
                            class="btn-primary" invisible="status != 'scheduled' or x_fc_is_shadow"/>
                    <button name="action_start_task" type="object" string="Start Task"
                            class="btn-primary" invisible="status not in ('scheduled', 'en_route') or x_fc_is_shadow"/>
                    <button name="action_complete_task" type="object" string="Complete"
                            class="btn-success" invisible="status not in ('in_progress', 'en_route') or x_fc_is_shadow"/>
                    <button name="action_reschedule" type="object" string="Reschedule"
                            class="btn-warning" invisible="status not in ('scheduled', 'en_route') or x_fc_is_shadow"/>
                    <button name="action_cancel_task" type="object" string="Cancel"
                            class="btn-danger" invisible="status in ('completed', 'cancelled') or x_fc_is_shadow"
                            confirm="Are you sure you want to cancel this task?"/>
                    <button name="action_reset_to_scheduled" type="object" string="Reset to Scheduled"
                            invisible="status not in ('cancelled', 'rescheduled') or x_fc_is_shadow"/>
                    <button string="Calculate Travel"
                            class="btn-secondary o_fc_calculate_travel" icon="fa-car"
                            invisible="x_fc_is_shadow"/>
                    <field name="status" widget="statusbar"
                           statusbar_visible="pending,scheduled,en_route,in_progress,completed"/>
                </header>
                <sheet>
                    <!-- Shadow task banner -->
                    <div class="alert alert-info text-center" role="alert"
                         invisible="not x_fc_is_shadow">
                        <strong><i class="fa fa-link"/> This task is synced from
                        <field name="x_fc_sync_source" readonly="1" nolabel="1" class="d-inline"/>
                        — view only.</strong>
                    </div>
                    <div class="oe_button_box" name="button_box">
                    </div>
                    <widget name="web_ribbon" title="Completed" bg_color="text-bg-success"
                            invisible="status != 'completed'"/>
                    <widget name="web_ribbon" title="Cancelled" bg_color="text-bg-danger"
                            invisible="status != 'cancelled'"/>
                    <widget name="web_ribbon" title="Synced" bg_color="text-bg-info"
                            invisible="not x_fc_is_shadow or status in ('completed', 'cancelled')"/>
                    <div class="oe_title">
                        <h1>
                            <field name="name" readonly="1"/>
                        </h1>
                    </div>

                    <!-- Schedule Info Banner -->
                    <field name="schedule_info_html" nolabel="1" colspan="2"
                           invisible="not technician_id or not scheduled_date"/>

                    <!-- Previous Task / Travel Warning Banner -->
                    <field name="prev_task_summary_html" nolabel="1" colspan="2"
                           invisible="not technician_id or not scheduled_date"/>

                    <!-- Hidden fields for calendar sync and legacy -->
                    <field name="datetime_start" invisible="1"/>
                    <field name="datetime_end" invisible="1"/>
                    <field name="time_start_12h" invisible="1"/>
                    <field name="time_end_12h" invisible="1"/>

                    <group>
                        <group string="Assignment">
                            <field name="technician_id"
                                   domain="[('x_fc_is_field_staff', '=', True)]"/>
                            <field name="additional_technician_ids"
                                   widget="many2many_tags_avatar"
                                   domain="[('x_fc_is_field_staff', '=', True), ('id', '!=', technician_id)]"
                                   options="{'color_field': 'color'}"/>
                            <field name="task_type"/>
                            <field name="priority" widget="priority"/>
                        </group>
                        <group string="Schedule">
                            <field name="scheduled_date"/>
                            <field name="time_start" widget="float_time"
                                   string="Start Time"/>
                            <field name="duration_hours" widget="float_time"
                                   string="Duration"/>
                            <field name="time_end" widget="float_time"
                                   string="End Time" readonly="1"
                                   force_save="1"/>
                        </group>
                    </group>

                    <group>
                        <group string="Client">
                            <field name="partner_id"/>
                            <field name="partner_phone" widget="phone"/>
                        </group>
                        <group string="Location">
                            <field name="is_in_store"/>
                            <field name="address_partner_id" invisible="is_in_store"/>
                            <field name="address_street" readonly="is_in_store"/>
                            <field name="address_street2" string="Unit/Suite #" invisible="is_in_store"/>
                            <field name="address_buzz_code" invisible="is_in_store"/>
                            <field name="address_city" invisible="1"/>
                            <field name="address_state_id" invisible="1"/>
                            <field name="address_zip" invisible="1"/>
                            <field name="address_lat" invisible="1"/>
                            <field name="address_lng" invisible="1"/>
                        </group>
                    </group>

                    <group>
                        <group string="Travel (Auto-Calculated)">
                            <field name="travel_time_minutes" readonly="1"/>
                            <field name="travel_distance_km" readonly="1"/>
                            <field name="travel_origin" readonly="1"/>
                            <field name="previous_task_id" readonly="1"/>
                        </group>
                        <group string="Options">
                            <field name="pod_required"/>
                            <field name="x_fc_send_client_updates"/>
                            <field name="x_fc_ask_google_review"/>
                            <field name="active" invisible="1"/>
                        </group>
                    </group>

                    <notebook>
                        <page string="Description" name="description">
                            <group>
                                <field name="description" placeholder="What needs to be done..."/>
                            </group>
                            <group>
                                <field name="equipment_needed" placeholder="Tools, parts, materials..."/>
                            </group>
                        </page>
                        <page string="Completion" name="completion">
                            <group>
                                <field name="completion_datetime"/>
                                <field name="completion_notes"/>
                            </group>
                            <group>
                                <field name="voice_note_transcription"/>
                            </group>
                        </page>
                    </notebook>
                </sheet>
                <chatter/>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- LIST VIEW                                                           -->
    <!-- ================================================================== -->
    <record id="view_technician_task_list" model="ir.ui.view">
        <field name="name">fusion.technician.task.list</field>
        <field name="model">fusion.technician.task</field>
        <field name="arch" type="xml">
            <list string="Technician Tasks" decoration-success="status == 'completed'"
                  decoration-warning="status == 'in_progress'"
                  decoration-info="status == 'en_route'"
                  decoration-danger="status == 'cancelled'"
                  decoration-muted="status == 'rescheduled'"
                  default_order="scheduled_date, sequence, time_start">
                <field name="name"/>
                <field name="technician_id" widget="many2one_avatar_user"/>
                <field name="additional_technician_ids" widget="many2many_tags_avatar"
                       optional="show" string="+ Techs"/>
                <field name="task_type" decoration-bf="1"/>
                <field name="scheduled_date"/>
                <field name="time_start_display" string="Start"/>
                <field name="time_end_display" string="End"/>
                <field name="partner_id"/>
                <field name="address_city"/>
                <field name="travel_time_minutes" string="Travel (min)" optional="show"/>
                <field name="status" widget="badge"
                       decoration-success="status == 'completed'"
                       decoration-warning="status == 'in_progress'"
                       decoration-info="status in ('scheduled', 'en_route')"
                       decoration-danger="status == 'cancelled'"/>
                <field name="priority" widget="priority" optional="hide"/>
                <field name="pod_required" optional="hide"/>
                <field name="x_fc_source_label" string="Source" optional="show"
                       widget="badge" decoration-info="x_fc_is_shadow"
                       decoration-success="not x_fc_is_shadow"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- KANBAN VIEW                                                         -->
    <!-- ================================================================== -->
    <record id="view_technician_task_kanban" model="ir.ui.view">
        <field name="name">fusion.technician.task.kanban</field>
        <field name="model">fusion.technician.task</field>
        <field name="arch" type="xml">
            <kanban default_group_by="status" class="o_kanban_small_column"
                    records_draggable="1" group_create="0">
                <field name="color"/>
                <field name="priority"/>
                <field name="technician_id"/>
                <field name="additional_technician_ids"/>
                <field name="additional_tech_count"/>
                <field name="partner_id"/>
                <field name="task_type"/>
                <field name="scheduled_date"/>
                <field name="time_start_display"/>
                <field name="address_city"/>
                <field name="travel_time_minutes"/>
                <field name="status"/>
                <field name="x_fc_is_shadow"/>
                <field name="x_fc_sync_client_name"/>
                <templates>
                    <t t-name="card">
                        <div t-attf-class="oe_kanban_color_#{record.color.raw_value} oe_kanban_card oe_kanban_global_click">
                            <div class="oe_kanban_content">
                                <div class="o_kanban_record_top mb-1">
                                    <div class="o_kanban_record_headings">
                                        <strong class="o_kanban_record_title">
                                            <field name="name"/>
                                        </strong>
                                    </div>
                                    <field name="priority" widget="priority"/>
                                </div>
                                <div class="mb-1">
                                    <span class="badge bg-primary me-1"><field name="task_type"/></span>
                                    <span class="text-muted"><field name="scheduled_date"/> - <field name="time_start_display"/></span>
                                </div>
                                <div class="mb-1">
                                    <i class="fa fa-user me-1"/>
                                    <t t-if="record.x_fc_is_shadow.raw_value">
                                        <span t-out="record.x_fc_sync_client_name.value"/>
                                    </t>
                                    <t t-else="">
                                        <field name="partner_id"/>
                                    </t>
                                </div>
                                <div class="text-muted small" t-if="record.address_city.raw_value">
                                    <i class="fa fa-map-marker me-1"/><field name="address_city"/>
                                    <t t-if="record.travel_time_minutes.raw_value">
                                        <span class="ms-2"><i class="fa fa-car me-1"/><field name="travel_time_minutes"/> min</span>
                                    </t>
                                </div>
                                <div t-if="record.additional_tech_count.raw_value > 0" class="text-muted small mb-1">
                                    <i class="fa fa-users me-1"/>
                                    <span>+<field name="additional_tech_count"/> technician(s)</span>
                                </div>
                                <div class="o_kanban_record_bottom mt-2">
                                    <div class="oe_kanban_bottom_left">
                                        <field name="activity_ids" widget="kanban_activity"/>
                                    </div>
                                    <div class="oe_kanban_bottom_right">
                                        <field name="technician_id" widget="many2one_avatar_user"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </t>
                </templates>
            </kanban>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- CALENDAR VIEW                                                       -->
    <!-- ================================================================== -->
    <record id="view_technician_task_calendar" model="ir.ui.view">
        <field name="name">fusion.technician.task.calendar</field>
        <field name="model">fusion.technician.task</field>
        <field name="arch" type="xml">
            <calendar string="Technician Schedule"
                      date_start="datetime_start" date_stop="datetime_end"
                      color="technician_id" mode="week" event_open_popup="1"
                      quick_create="0">
                <!-- Displayed on the calendar card -->
                <field name="partner_id"/>
                <field name="x_fc_sync_client_name"/>
                <field name="task_type"/>
                <field name="time_start_display" string="Start"/>
                <field name="time_end_display" string="End"/>
                <!-- Popover (hover/click) details -->
                <field name="name"/>
                <field name="technician_id" avatar_field="image_128"/>
                <field name="address_display" string="Address"/>
                <field name="travel_time_minutes" string="Travel (min)"/>
                <field name="status"/>
                <field name="duration_hours" widget="float_time" string="Duration"/>
            </calendar>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- MAP VIEW (Enterprise web_map)                                       -->
    <!-- ================================================================== -->
    <record id="view_technician_task_map" model="ir.ui.view">
        <field name="name">fusion.technician.task.map</field>
        <field name="model">fusion.technician.task</field>
        <field name="arch" type="xml">
            <map res_partner="address_partner_id" default_order="time_start"
                 routing="1" js_class="fusion_task_map">
                <field name="partner_id" string="Client"/>
                <field name="task_type" string="Type"/>
                <field name="technician_id" string="Technician"/>
                <field name="time_start_display" string="Start"/>
                <field name="time_end_display" string="End"/>
                <field name="status" string="Status"/>
                <field name="travel_time_minutes" string="Travel (min)"/>
            </map>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- ACTIONS                                                             -->
    <!-- ================================================================== -->

    <!-- Main Tasks Action (List/Kanban) -->
    <record id="action_technician_tasks" model="ir.actions.act_window">
        <field name="name">Technician Tasks</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">list,kanban,form,calendar,map</field>
        <field name="search_view_id" ref="view_technician_task_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 technician task
            </p>
            <p>Schedule deliveries, repairs, and other field tasks for your technicians.</p>
        </field>
    </record>

    <!-- Schedule Action (Map default) -->
    <record id="action_technician_schedule" model="ir.actions.act_window">
        <field name="name">Schedule</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">map,calendar,list,kanban,form</field>
        <field name="search_view_id" ref="view_technician_task_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
    </record>

    <!-- Map View Action (for app landing page) -->
    <record id="action_technician_map_view" model="ir.actions.act_window">
        <field name="name">Task Map</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">map,list,kanban,form,calendar</field>
        <field name="search_view_id" ref="view_technician_task_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
    </record>

    <!-- Today's Tasks Action -->
    <record id="action_technician_tasks_today" model="ir.actions.act_window">
        <field name="name">Today's Tasks</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">kanban,list,form,map</field>
        <field name="search_view_id" ref="view_technician_task_search"/>
        <field name="context">{'search_default_filter_today': 1, 'search_default_filter_active': 1}</field>
    </record>

    <!-- My Tasks Action -->
    <record id="action_technician_my_tasks" model="ir.actions.act_window">
        <field name="name">My Tasks</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">list,kanban,form,calendar,map</field>
        <field name="search_view_id" ref="view_technician_task_search"/>
        <field name="context">{'search_default_filter_my_tasks': 1, 'search_default_filter_active': 1}</field>
    </record>

    <!-- Pending Tasks Action -->
    <record id="action_technician_tasks_pending" model="ir.actions.act_window">
        <field name="name">Pending Tasks</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">list,kanban,form</field>
        <field name="search_view_id" ref="view_technician_task_search"/>
        <field name="context">{'search_default_filter_pending': 1}</field>
    </record>

    <!-- Calendar Action -->
    <record id="action_technician_calendar" model="ir.actions.act_window">
        <field name="name">Task Calendar</field>
        <field name="res_model">fusion.technician.task</field>
        <field name="view_mode">calendar,list,kanban,form,map</field>
        <field name="search_view_id" ref="view_technician_task_search"/>
        <field name="context">{'search_default_filter_active': 1}</field>
    </record>

    <!-- ================================================================== -->
    <!-- MENU ITEMS - Standalone Field Service App                           -->
    <!-- ================================================================== -->

    <!-- Root app menu -->
    <menuitem id="menu_field_service_root"
              name="Field Service"
              web_icon="fusion_tasks,static/description/icon.png"
              groups="fusion_tasks.group_field_technician"
              sequence="45"/>

    <!-- Map View - first item = default landing view -->
    <menuitem id="menu_technician_map"
              name="Map View"
              parent="menu_field_service_root"
              action="action_technician_map_view"
              sequence="5"
              groups="fusion_tasks.group_field_technician"/>

    <!-- Tasks -->
    <menuitem id="menu_technician_tasks"
              name="Tasks"
              parent="menu_field_service_root"
              action="action_technician_tasks"
              sequence="10"
              groups="fusion_tasks.group_field_technician"/>

    <!-- Calendar -->
    <menuitem id="menu_technician_calendar"
              name="Calendar"
              parent="menu_field_service_root"
              action="action_technician_calendar"
              sequence="30"
              groups="fusion_tasks.group_field_technician"/>

    <!-- Task Sync (submenu) -->
    <menuitem id="menu_technician_config"
              name="Configuration"
              parent="menu_field_service_root"
              sequence="90"
              groups="fusion_tasks.group_field_technician"/>

</odoo>
