<?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;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Sanctum\HasApiTokens;


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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'nick_name',
        'email',
        'password',
        'name' ,
        'dial_code',
        'phone',
        'country_id',
        'nationality_id',
        'emirates_id',
        'status',
        'first_name',
        'last_name',
        'role_id',
        'gender',
        'dob',
        'profile_image',
        'is_verified',
        'whatsapp_number',
        'user_number',
        'profile_setup_completed',
        'preference_setup_completed',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
        'user_phone_otp',
        'user_email_otp',
        'user_device_type',
        'user_device_token',
        'deleted_at',
        'city_id',
        'user_access_token',
        'password_reset_code',
        'password_reset_time',
        'password_reset_otp',
    ];

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

    public $appends = ['format_created_at','profile_image_url','age'];

    public function getFormatCreatedAtAttribute(){
        return \Carbon\Carbon::parse($this->created_at)->format('d M, Y, h:i A');
    }

    public function getProfileImageUrlAttribute()
    {
        if($this->gender == GENDER_MALE){
        return get_uploaded_image_url($this->profile_image, 'user_image_upload_dir', 'male-placeholder.png');
        }else{
            return get_uploaded_image_url($this->profile_image, 'user_image_upload_dir', 'female-placeholder.png');
        }
    }
    
    public function getBirthCertificateAttribute($value)
    {
         $id = $this->details['birth_certificate'] ?? null;
        return get_uploaded_image_url($value, 'user_image_upload_dir', 'male-placeholder.png');
        
    }
    
     public function getMothersEmiratesIdAttribute($value)
    {
      
        $id = $this->details['mothers_emirates_id'] ?? null;
        return get_uploaded_image_url($value, 'user_image_upload_dir', 'male-placeholder.png');

        
    }
    public function getAgeAttribute()
    {
        return \Carbon\Carbon::parse($this->dob)->age;
    }

    public function nationality()
    {
        return $this->belongsTo(Nationality::class, 'nationality_id');
    }

    public function country()
    {
        return $this->belongsTo(Country::class, 'country_id');
    }

    public function emirate()
    {
        return $this->belongsTo(Emirate::class, 'emirates_id');
    }

    public function plan()
    {
        return $this->hasOne(UserPlan::class, 'user_id')->orderBy('id','desc');
    }

    public function details()
    {
        return $this->hasOne(UserDetail::class, 'user_id');
    }

    public function activities()
    {
        return $this->hasMany(UserActivity::class, 'user_id')->whereHas('activity');
    }

    public function sports()
    {
        return $this->hasMany(UserSport::class, 'user_id')->whereHas('sport');
    }

    
    public function known_languages()
    {
        return $this->hasMany(KnownLanguage::class, 'user_id')->whereHas('language');
    }

    public function geneticDiseases()
    {
        return $this->hasMany(UserDisease::class, 'user_id')->whereHas('geneticDisease');
    }
    public function chronicDiseases()
    {
        return $this->hasMany(UserChronicDisease::class, 'user_id')->whereHas('chronicDisease');
    }

     public function dressCodes()
    {
        return $this->hasMany(UserDressCode::class, 'user_id')->whereHas('dressCode');
    }

     public function maritalStatuses()
    {
        return $this->hasMany(UserMaritalStatus::class, 'user_id')->whereHas('maritalStatus');
    }

      public function educations()
    {
        return $this->hasMany(UserEducation::class, 'user_id')->whereHas('education');
    }

     public function sects()
    {
        return $this->hasMany(UserSect::class, 'user_id')->whereHas('sect');
    }

    public function professions()
    {
        return $this->hasMany(UserProfession::class, 'user_id')->whereHas('profession');
    }
    
    public function syncactivities()
    {
        return $this->belongsToMany(Activity::class, 'user_activities');
    }

    public function syncsports()
    {
        return $this->belongsToMany(Sport::class, 'user_sports');
    }

    
    public function syncknown_languages()
    {
        return $this->belongsToMany(Language::class, 'known_languages');
    }

    public function syncgeneticDiseases()
    {
        return $this->belongsToMany(GeneticDisease::class, 'user_diseases');
    }

      public function syncDressCodes()
    {
        return $this->belongsToMany(GeneticDisease::class, 'user_dress_codes','user_id','dress_code_id');
    }

      public function syncMaritalStatuses()
    {
        return $this->belongsToMany(MaritalStatus::class, 'user_marital_statuses','user_id','marital_status_id');
    }

       public function syncEducation()
    {
        return $this->belongsToMany(Education::class, 'user_educations','user_id','education_id');
    }

       public function syncSect()
    {
        return $this->belongsToMany(Sect::class, 'user_sects','user_id','sect_id');
    }


       public function syncProfession()
    {
        return $this->belongsToMany(Profession::class, 'user_professions','user_id','profession_id');
    }

    public function preference()
    {
        return $this->hasOne(UserPreference::class, 'user_id');
    }


    public function children()
    {
        return $this->hasMany(UserChild::class, 'user_id');
    }

    public function images()
    {
        return $this->hasMany(UserImage::class, 'user_id');
    }

      public function notification_settings()
    {
        return $this->hasMany(NotificationSetting::class, 'user_id');
    }

     public function sendRequest()
    {
        return $this->hasMany(UserRequest::class, 'sender_id');
    }
     public function receiveRequest()
    {
        return $this->hasMany(UserRequest::class, 'receiver_id');
    }


    // Users that the current user has blocked
public function blockedUsers()
{
    return $this->hasMany(UserConnection::class, 'user_id')->where('is_blocked', 1);
}

// Users that have blocked the current user
public function blockedByUsers()
{
    return $this->hasMany(UserConnection::class, 'connected_user_id')->where('is_blocked', 1);
}


    public function matches()
    {
        return $this->hasManyThrough(
            User::class,
            UserMatch::class,
            'user_id', // Foreign key on UserMatch table...
            'id', // Foreign key on User table...
            'id', // Local key on User table...
            'matched_user_id' // Local key on UserMatch table...
        );
    }

}
