<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'phone',
        'is_active',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'is_active' => 'boolean',
        ];
    }

    /**
     * Check if user has admin role
     */
    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    /**
     * Check if user has receptionist role
     */
    public function isReceptionist(): bool
    {
        return $this->role === 'receptionist';
    }

    /**
     * Check if user has permission for a given action
     */
    public function hasPermission(string $permission): bool
    {
        $permissions = [
            'admin' => [
                'view_dashboard',
                'manage_customers',
                'manage_orders',
                'manage_items',
                'manage_pricing',
                'view_reports',
                'view_advanced_reports',
                'manage_users',
                'manage_settings',
                'delete_customers',
                'cancel_orders',
            ],
            'receptionist' => [
                'view_dashboard',
                'manage_customers',
                'manage_orders',
                'view_reports',
            ],
        ];

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

    /**
     * Customers created by this user
     */
    public function customers()
    {
        return $this->hasMany(Customer::class, 'created_by');
    }

    /**
     * Orders created by this user
     */
    public function orders()
    {
        return $this->hasMany(Order::class, 'created_by');
    }
}
