<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    protected $fillable = [
        'name','email','password','role','permissions','status','branch_id','two_factor_enabled',
    ];

    protected $hidden = ['password'];
    protected $casts = ['permissions' => 'array', 'two_factor_enabled' => 'boolean'];
    public $timestamps = false;

    public function permissionsList(): array
    {
        $all = [
            'dashboard','products','purchases','sales','returns','expenses',
            'customers','accounts','reports','inventory','users','settings','branches','service','installments','dealers','hr','security','automation'
        ];

        return match ($this->role) {
            'Super Admin' => $all,
            'Manager' => ['dashboard','products','purchases','sales','returns','expenses','customers','reports','inventory','branches','service','installments','dealers','hr','automation'],
            'Accountant' => ['dashboard','customers','accounts','reports','expenses','installments','dealers'],
            'Cashier' => ['dashboard','sales','customers','returns','service'],
            default => array_values(array_intersect($all, $this->permissions ?? [])),
        };
    }

    public function hasPermission(string $permission): bool
    {
        return in_array($permission, $this->permissionsList(), true);
    }
}
