# -*- coding: utf-8 -*-
# Copyright 2024-2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)

from . import models


def _fusion_tasks_post_init(env):
    """Post-install hook for fusion_tasks.

    1. Sets default ICP values (upsert - safe if keys already exist).
    2. Adds all active internal users to group_field_technician so
       the Field Service menus are visible immediately after install.
    """
    ICP = env['ir.config_parameter'].sudo()
    defaults = {
        'fusion_claims.google_maps_api_key': '',
        'fusion_claims.store_open_hour': '9.0',
        'fusion_claims.store_close_hour': '18.0',
        'fusion_claims.push_enabled': 'False',
        'fusion_claims.push_advance_minutes': '30',
        'fusion_claims.sync_instance_id': '',
        'fusion_claims.technician_start_address': '',
    }
    for key, default_value in defaults.items():
        if not ICP.get_param(key):
            ICP.set_param(key, default_value)

    # Add all active internal users to Field Technician group
    ft_group = env.ref('fusion_tasks.group_field_technician', raise_if_not_found=False)
    if ft_group:
        internal_users = env['res.users'].search([
            ('active', '=', True),
            ('share', '=', False),
        ])
        ft_group.write({'user_ids': [(4, u.id) for u in internal_users]})
