<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Skill;
use App\Models\Profile;
use App\Models\UserSkill;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, SoftDeletes;

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

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

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

    /**
     * Get the user associated with the User
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function profile(): HasOne
    {
        return $this->hasOne(Profile::class, 'user_id', 'id')->withDefault();
    }

    public function skills(): BelongsToMany
    {
        return $this->belongsToMany(Skill::class, 'user_skills', 'user_id', 'skill_id');
    }

    public function getFormattedSkillsAttribute()
    {
        return $this->skills->map(function ($skill) {
            return [
                'skill_id' => $skill->id,
                'name' => $skill->name,
            ];
        });
    }

    public function getFormattedprofileAttribute() {
        return [
            'user_id' => $this->profile->user_id,
            'image' => $this->profile->image,
            'kyc' => $this->profile->kyc,
            'image_verif' => $this->profile->image_verif,
            'ktp' => $this->profile->ktp,
            'phone' => $this->profile->phone,
            'address' => $this->profile->address,
            'location' => $this->profile->location,
            'birthdate' => $this->profile->birthdate,
            'gender' => $this->profile->gender,
            'marital_status' => $this->profile->marital_status,
            'last_education' => $this->profile->last_education,
            'additional_skills' => $this->profile->additional_skills,
            'desired_salary' => $this->profile->desired_salary,
        ];
    }
}
