<?php

namespace App\Models;

use Laravel\Sanctum\HasApiTokens;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

   protected $fillable = [
    'name', 'staff_code', 'phone', 'email', 'password',
    'type_id', 'shop_id', 'role_id', 'is_active',
];

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

    public function shop()
    {
        return $this->belongsTo(Shop::class);
    }

    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    public function salarySetting()
    {
        return $this->hasOne(StaffSalarySetting::class, 'staff_id');
    }

    public function salaryPayments()
    {
        return $this->hasMany(SalaryPayment::class, 'staff_id');
    }

    public function hasPermission(string $key): bool
    {
        if (in_array($this->type_id, [1, 2])) {
            return true;
        }

        return $this->role?->hasPermission($key) ?? false;
    }

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }
    public function users()
{
    return $this->hasMany(User::class);
}
}
