<?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;

    public const ROLE_ADMIN = 'admin';
    public const ROLE_EDITOR = 'editor';
    public const ROLE_VIEWER = 'viewer';

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

    /**
     * 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',
        ];
    }

    public function isAdmin(): bool
    {
        return $this->role === self::ROLE_ADMIN || $this->is_admin;
    }

    public function isEditor(): bool
    {
        return $this->role === self::ROLE_EDITOR || $this->isAdmin();
    }

    public function isViewer(): bool
    {
        return $this->role === self::ROLE_VIEWER;
    }

    public function canManageUsers(): bool
    {
        return $this->isAdmin();
    }

    public function canEditSettings(): bool
    {
        return $this->isAdmin();
    }

    public function canAccessMarketplace(): bool
    {
        return $this->isAdmin();
    }

    public function canEdit(): bool
    {
        return $this->isEditor() || $this->isAdmin();
    }

    public function getRoleLabel(): string
    {
        return match ($this->role ?? '') {
            self::ROLE_ADMIN => 'Administrateur',
            self::ROLE_EDITOR => 'Éditeur',
            self::ROLE_VIEWER => 'Lecteur',
            default => 'Lecteur',
        };
    }
}
