<?php
namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name','email','password','role','division','phone',
        'is_active','google2fa_secret','two_factor_enabled',
        'telegram_chat_id','whatsapp_number','notification_prefs','permissions','api_token'
    ];

    protected $hidden = ['password','remember_token','google2fa_secret','api_token'];

    protected $casts = [
        'notification_prefs'    => 'array',
        'permissions'           => 'array',
        'two_factor_enabled'    => 'boolean',
        'is_active'             => 'boolean',
        'email_verified_at'     => 'datetime',
    ];

    public static function permissionMap(): array
    {
        return [
            'ticket.*'     => 'Tiket',
            'client.*'     => 'Klien',
            'visit.*'      => 'Kunjungan',
            'invoice.*'    => 'Invoice',
            'workorder.*'  => 'Work Order',
            'project.*'    => 'Proyek',
            'report.*'     => 'Laporan',
            'settings.*'   => 'Pengaturan',
            'user.*'       => 'Manajemen User',
        ];
    }

    public function getPermissions(): array
    {
        return array_filter(array_values((array) $this->permissions));
    }

    public function canDo(string $permission): bool
    {
        $userPermissions = array_filter((array) $this->permissions);
        if (!empty($userPermissions)) {
            foreach ($userPermissions as $p) {
                if ($p === '*' || $p === $permission || fnmatch($p, $permission)) {
                    return true;
                }
            }
            return false;
        }

        $permissions = [
            'admin' => ['*'],
            'cs' => [
                'ticket.*',
                'client.*',
                'visit.*',
                'invoice.*',
                'csat.view',
                'workorder.*',
            ],
            'technician' => [
                'ticket.view',
                'ticket.update',
                'ticket.comment',
                'visit.view',
                'visit.update',
                'workorder.*',
            ],
        ];

        $role = $this->role;
        if (!isset($permissions[$role])) {
            return false;
        }
        if (in_array('*', $permissions[$role], true)) {
            return true;
        }
        foreach ($permissions[$role] as $p) {
            if ($p === $permission || fnmatch($p, $permission)) {
                return true;
            }
        }
        return false;
    }

    public function tickets()
    {
        return $this->hasMany(Ticket::class, 'assigned_to');
    }

    public function isAdmin(): bool { return $this->role === 'admin'; }
    public function isCs(): bool    { return $this->role === 'cs'; }
    public function isTechnician(): bool { return $this->role === 'technician'; }
}
