<?php

namespace App\Models;

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

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

    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'sub_role',
        'branch_id',
        'restaurant_id',
        'is_active',
    ];

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

    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'is_active' => 'boolean',
    ];

    public function inventories()
    {
        return $this->hasMany(Inventory::class, 'created_by');
    }

    public function stockOuts()
    {
        return $this->hasMany(StockOut::class, 'transferred_by');
    }

    public function stockClosings()
    {
        return $this->hasMany(StockClosing::class, 'closed_by');
    }

    public function isAdmin()
    {
        return $this->role === 'admin';
    }

    public function isManager()
    {
        return $this->role === 'manager';
    }

    public function isStaff()
    {
        return $this->role === 'staff';
    }

    public function isPosManager()
    {
        return $this->role === 'branch_manager';
    }

    public function isBranchCashier()
    {
        return $this->role === 'branch_cashier';
    }

    public function isPosStaff()
    {
        return $this->role === 'pos_staff';
    }

    public function isKitchenStaff()
    {
        return $this->role === 'pos_kitchen_staff';
    }

    public function isRestaurantOwner()
    {
        return $this->role === 'restaurant_owner';
    }

    public function isBranchManager()
    {
        return $this->role === 'branch_manager';
    }

    public function isInventoryManager()
    {
        return $this->role === 'inventory_manager';
    }

    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    public function restaurant()
    {
        return $this->belongsTo(Restaurant::class, 'restaurant_id');
    }
}

