<?php

namespace App\Models;

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

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

    protected $fillable = [
        'restaurant_id',
        'branch_id',
        'name',
        'email',
        'password',
        'pin',
        'phone',
        'role',
        'permissions',
        'is_active',
        'last_login_at',
        'employee_id',
        'department',
        'shift_schedule',
        'emergency_contact',
        'hire_date',
        'hourly_rate',
        'notes',
    ];

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

    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'permissions' => 'array',
        'is_active' => 'boolean',
        'last_login_at' => 'datetime',
        'shift_schedule' => 'array',
        'hire_date' => 'date',
        'hourly_rate' => 'decimal:2',
    ];

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

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

    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function stockMovements(): HasMany
    {
        return $this->hasMany(StockMovement::class);
    }

    public function acknowledgedTickets(): HasMany
    {
        return $this->hasMany(KitchenTicket::class, 'acknowledged_by');
    }

    public function preparedTickets(): HasMany
    {
        return $this->hasMany(KitchenTicket::class, 'prepared_by');
    }

    public function getRoleDisplayAttribute(): string
    {
        return match($this->role) {
            'admin' => 'Administrator',
            'manager' => 'Manager',
            'cashier' => 'Cashier',
            'waiter' => 'Waiter/Server',
            'kitchen' => 'Kitchen Staff',
            'inventory_manager' => 'Inventory Manager',
            'accountant' => 'Accountant',
            default => ucfirst($this->role),
        };
    }

    public function hasPermission(string $permission): bool
    {
        if ($this->role === 'admin') {
            return true;
        }

        return in_array($permission, $this->permissions ?? []);
    }

    public function canViewAllOrders(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'cashier']) || $this->hasPermission('view_all_orders');
    }

    public function canAccessKitchen(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'kitchen']) || $this->hasPermission('access_kitchen');
    }

    public function canManageInventory(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'inventory_manager']) || $this->hasPermission('manage_inventory');
    }

    public function canViewReports(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'accountant']) || $this->hasPermission('view_reports');
    }

    public function canProcessPayments(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'cashier']) || $this->hasPermission('process_payments');
    }

    public function canAccessPOS(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'cashier', 'waiter']) || $this->hasPermission('access_pos');
    }

    public function canManageUsers(): bool
    {
        return in_array($this->role, ['admin', 'manager']) || $this->hasPermission('manage_users');
    }

    public function canManageMenu(): bool
    {
        return in_array($this->role, ['admin', 'manager']) || $this->hasPermission('manage_menu');
    }

    public function canViewFinancialReports(): bool
    {
        return in_array($this->role, ['admin', 'manager', 'accountant']) || $this->hasPermission('view_financial_reports');
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeByRole($query, string $role)
    {
        return $query->where('role', $role);
    }

    public function scopeByBranch($query, int $branchId)
    {
        return $query->where('branch_id', $branchId);
    }
}
