<?php

namespace App\Models;

use App\Enum\UserType;
use Illuminate\Contracts\Auth\MustVerifyEmail;
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\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens, HasFactory, Notifiable, Billable;

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'email',
        'username',
        'password',
        'name',
        'profile_picture',
        'type',
    ];

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

    /**
     * The attributes that are not mass assignable.
     * @var string[]
     */
    protected $guarded = [
        'last_login_at',
        'last_login_ip',
        'user_agent',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function isOnboardingCompleted()
    {
        return true;
    }

    public function role(): ?HasOne
    {
        return match ($this->type) {
            UserType::ORGANIZER->value => $this->hasOne(Organizer::class),
            UserType::MANAGER->value => $this->hasOne(Manager::class),
            UserType::ARTIST->value => $this->hasOne(Artist::class),
        };
    }

    public function offer_comments(): HasMany
    {
        return $this->hasMany(OfferComment::class, 'author_id');
    }
}
