<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
use LaravelAndVueJS\Traits\LaravelPermissionToVueJS;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasRoles;
    use LaravelPermissionToVueJS;

    public const ADMIN_ROL = 'Administrador';

    public const RECEPCIONISTA_ROL = 'Recepcionista';

    public const OPERADOR_ROL = 'Operador';

    public const ROLES = [
        self::ADMIN_ROL,
        self::RECEPCIONISTA_ROL,
        self::OPERADOR_ROL,
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'estatus',
        'autorizado',
        'cliente_id',
        'nota',
        'uuid',
        'area_id',
    ];

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

    /**
     * Relación con órdenes de lavandería creadas (para Recepcionista)
     */
    public function ordenesLavanderia(): HasMany
    {
        return $this->hasMany(\App\Models\OrdenLavanderia::class, 'usuario_recepcionista_id');
    }

    public function orderAreaScanEvents(): HasMany
    {
        return $this->hasMany(OrderAreaScanEvent::class, 'user_id');
    }

    public function shippingOrdersMarkedDelivered(): HasMany
    {
        return $this->hasMany(ShippingOrder::class, 'delivered_by_user_id');
    }

    /**
     * Área de trabajo en planta (C5): un usuario operativo escanea solo en esta estación.
     */
    public function area(): BelongsTo
    {
        return $this->belongsTo(Area::class, 'area_id');
    }
}
