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

    <!-- ================================================================== -->
    <!-- LIST VIEW                                                           -->
    <!-- ================================================================== -->
    <record id="view_technician_location_list" model="ir.ui.view">
        <field name="name">fusion.technician.location.list</field>
        <field name="model">fusion.technician.location</field>
        <field name="arch" type="xml">
            <list string="Technician Locations" create="0" edit="0"
                  default_order="logged_at desc">
                <field name="user_id" widget="many2one_avatar_user"/>
                <field name="logged_at" string="Time"/>
                <field name="latitude" optional="hide"/>
                <field name="longitude" optional="hide"/>
                <field name="accuracy" string="Accuracy (m)" optional="hide"/>
                <field name="source"/>
            </list>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- FORM VIEW (read-only)                                               -->
    <!-- ================================================================== -->
    <record id="view_technician_location_form" model="ir.ui.view">
        <field name="name">fusion.technician.location.form</field>
        <field name="model">fusion.technician.location</field>
        <field name="arch" type="xml">
            <form string="Location Log" create="0" edit="0">
                <sheet>
                    <group>
                        <group>
                            <field name="user_id"/>
                            <field name="logged_at"/>
                            <field name="source"/>
                        </group>
                        <group>
                            <field name="latitude"/>
                            <field name="longitude"/>
                            <field name="accuracy"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- SEARCH VIEW                                                         -->
    <!-- ================================================================== -->
    <record id="view_technician_location_search" model="ir.ui.view">
        <field name="name">fusion.technician.location.search</field>
        <field name="model">fusion.technician.location</field>
        <field name="arch" type="xml">
            <search string="Search Location Logs">
                <field name="user_id" string="Technician"/>
                <separator/>
                <filter string="Today" name="filter_today"
                        domain="[('logged_at', '>=', context_today().strftime('%Y-%m-%d'))]"/>
                <filter string="Last 7 Days" name="filter_7d"
                        domain="[('logged_at', '>=', (context_today() - datetime.timedelta(days=7)).strftime('%Y-%m-%d'))]"/>
                <filter string="Last 30 Days" name="filter_30d"
                        domain="[('logged_at', '>=', (context_today() - datetime.timedelta(days=30)).strftime('%Y-%m-%d'))]"/>
                <separator/>
                <filter string="Technician" name="group_user" context="{'group_by': 'user_id'}"/>
                <filter string="Date" name="group_date" context="{'group_by': 'logged_at:day'}"/>
                <filter string="Source" name="group_source" context="{'group_by': 'source'}"/>
            </search>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- ACTION                                                              -->
    <!-- ================================================================== -->
    <record id="action_technician_locations" model="ir.actions.act_window">
        <field name="name">Location History</field>
        <field name="res_model">fusion.technician.location</field>
        <field name="view_mode">list,form</field>
        <field name="search_view_id" ref="view_technician_location_search"/>
        <field name="context">{
            'search_default_filter_today': 1,
            'search_default_group_user': 1,
        }</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                No location data logged yet.
            </p>
            <p>Technician locations are automatically logged when they use the portal.</p>
        </field>
    </record>

    <!-- ================================================================== -->
    <!-- MENU ITEMS (under Configuration)                                    -->
    <!-- ================================================================== -->
    <menuitem id="menu_technician_locations"
              name="Location History"
              parent="menu_technician_config"
              action="action_technician_locations"
              sequence="20"/>

</odoo>
