<?php

namespace App\Models;

use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasApiTokens, HasFactory, Notifiable, HasRoles;

    protected $fillable = [
        'its_number',
        'name',
        'email',
        'phone',
        'dob',
        'profile_photo',
        'city',
        'country',
        'password',
        'fcm_token',
    ];

    protected $hidden = [
        'password',
        'remember_token',
        'fcm_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'dob'               => 'date',
            'password'          => 'hashed',
        ];
    }

    public function medicalDetail()
    {
        return $this->hasOne(UserMedicalDetail::class);
    }

    public function attachments()
    {
        return $this->hasMany(UserAttachment::class);
    }

    public function registrations()
    {
        return $this->hasMany(EventRegistration::class);
    }

    public function familyLinks()
    {
        return $this->hasMany(FamilyLink::class);
    }

    public function linkedByFamily()
    {
        return $this->hasMany(FamilyLink::class, 'linked_user_id');
    }

    public function travelPreferences()
    {
        return $this->hasOne(TravelPreference::class);
    }

    public function wishlisted()
    {
        return $this->hasMany(EventWishlist::class);
    }

    public function chatGroups()
    {
        return $this->belongsToMany(ChatGroup::class, 'chat_group_members')
            ->withPivot('role', 'joined_at')
            ->withTimestamps();
    }
}
