<?php

namespace App\Models;

use App\Enums\AccountType;
use App\Enums\UserAccountStatus;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
 * Règle 1 (cahier des charges) : agence et agent indépendant ont les mêmes droits applicatifs ;
 * {@see AccountType} sert uniquement à l’affichage / au profil.
 */
class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasApiTokens, HasFactory, Notifiable;

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

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

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

    /**
     * Professionnels (agence / agent) en attente de validation admin.
     *
     * @param  Builder<User>  $query
     *
     * @return Builder<User>
     */
    public function scopePendingProfessionals(Builder $query): Builder
    {
        return $query
            ->where('status', UserAccountStatus::Pending)
            ->whereIn('account_type', AccountType::professionalValues());
    }

    public function professionalProfile(): HasOne
    {
        return $this->hasOne(ProfessionalProfile::class);
    }

    public function propertyRequestsCreated(): HasMany
    {
        return $this->hasMany(PropertyRequest::class, 'created_by_user_id');
    }

    public function propertyProposals(): HasMany
    {
        return $this->hasMany(PropertyProposal::class, 'proposed_by_user_id');
    }

    public function conversationsAsRequester(): HasMany
    {
        return $this->hasMany(Conversation::class, 'requester_user_id');
    }

    public function conversationsAsProposer(): HasMany
    {
        return $this->hasMany(Conversation::class, 'proposer_user_id');
    }

    public function messagesSent(): HasMany
    {
        return $this->hasMany(Message::class, 'sender_id');
    }

    /**
     * Règle 5 : compte validé par l’administration (demandes, propositions, chat métier).
     */
    public function hasApprovedAccount(): bool
    {
        return $this->status === UserAccountStatus::Approved;
    }

    public function isAdministrator(): bool
    {
        return $this->is_admin === true;
    }
}
