<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- ================================================================== -->
    <!-- MODULE CATEGORY                                                     -->
    <!-- ================================================================== -->
    <record id="module_category_fusion_tasks" model="ir.module.category">
        <field name="name">Fusion Tasks</field>
        <field name="sequence">46</field>
    </record>

    <!-- ================================================================== -->
    <!-- FUSION TASKS PRIVILEGE (Odoo 19 pattern)                            -->
    <!-- ================================================================== -->
    <record id="res_groups_privilege_fusion_tasks" model="res.groups.privilege">
        <field name="name">Fusion Tasks</field>
        <field name="sequence">46</field>
        <field name="category_id" ref="module_category_fusion_tasks"/>
    </record>

    <!-- ================================================================== -->
    <!-- FIELD TECHNICIAN GROUP                                              -->
    <!-- Standalone group safe for both portal and internal users.           -->
    <!-- Do NOT imply base.group_user — that chain conflicts with portal     -->
    <!-- users (share=True).                                                 -->
    <!-- ================================================================== -->
    <record id="group_field_technician" model="res.groups">
        <field name="name">Field Technician</field>
        <field name="privilege_id" ref="res_groups_privilege_fusion_tasks"/>
    </record>

    <!-- ================================================================== -->
    <!-- TECHNICIAN TASK RECORD RULES                                        -->
    <!-- ================================================================== -->

    <!-- Managers: full access to all tasks -->
    <record id="rule_technician_task_manager" model="ir.rule">
        <field name="name">Technician Task: Manager Full Access</field>
        <field name="model_id" ref="model_fusion_technician_task"/>
        <field name="domain_force">[(1, '=', 1)]</field>
        <field name="groups" eval="[(4, ref('sales_team.group_sale_manager'))]"/>
        <field name="perm_read" eval="True"/>
        <field name="perm_write" eval="True"/>
        <field name="perm_create" eval="True"/>
        <field name="perm_unlink" eval="True"/>
    </record>

    <!-- Sales users: read/write all tasks, create tasks -->
    <record id="rule_technician_task_sales_user" model="ir.rule">
        <field name="name">Technician Task: Sales User Access</field>
        <field name="model_id" ref="model_fusion_technician_task"/>
        <field name="domain_force">[(1, '=', 1)]</field>
        <field name="groups" eval="[(4, ref('sales_team.group_sale_salesman'))]"/>
        <field name="perm_read" eval="True"/>
        <field name="perm_write" eval="True"/>
        <field name="perm_create" eval="True"/>
        <field name="perm_unlink" eval="False"/>
    </record>

    <!-- Field Technicians (internal): own tasks only -->
    <record id="rule_technician_task_technician" model="ir.rule">
        <field name="name">Technician Task: Technician Own Tasks</field>
        <field name="model_id" ref="model_fusion_technician_task"/>
        <field name="domain_force">[('technician_id', '=', user.id)]</field>
        <field name="groups" eval="[(4, ref('group_field_technician'))]"/>
        <field name="perm_read" eval="True"/>
        <field name="perm_write" eval="True"/>
        <field name="perm_create" eval="False"/>
        <field name="perm_unlink" eval="False"/>
    </record>

    <!-- Portal technicians: own tasks only, read + limited write -->
    <record id="rule_technician_task_portal" model="ir.rule">
        <field name="name">Technician Task: Portal Technician Access</field>
        <field name="model_id" ref="model_fusion_technician_task"/>
        <field name="domain_force">[('technician_id', '=', user.id)]</field>
        <field name="groups" eval="[(4, ref('base.group_portal'))]"/>
        <field name="perm_read" eval="True"/>
        <field name="perm_write" eval="False"/>
        <field name="perm_create" eval="False"/>
        <field name="perm_unlink" eval="False"/>
    </record>

    <!-- ================================================================== -->
    <!-- PUSH SUBSCRIPTION RECORD RULES                                      -->
    <!-- ================================================================== -->

    <!-- Users: own subscriptions only -->
    <record id="rule_push_subscription_user" model="ir.rule">
        <field name="name">Push Subscription: Own Only</field>
        <field name="model_id" ref="model_fusion_push_subscription"/>
        <field name="domain_force">[('user_id', '=', user.id)]</field>
        <field name="groups" eval="[(4, ref('base.group_user'))]"/>
    </record>

    <!-- Portal: own subscriptions only -->
    <record id="rule_push_subscription_portal" model="ir.rule">
        <field name="name">Push Subscription: Portal Own Only</field>
        <field name="model_id" ref="model_fusion_push_subscription"/>
        <field name="domain_force">[('user_id', '=', user.id)]</field>
        <field name="groups" eval="[(4, ref('base.group_portal'))]"/>
    </record>

</odoo>
