<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Project;
use App\Models\Requirement;
use App\Models\UserOperation;
use App\Models\RequirementOperation;
use App\Models\NonFunctionalRequirement;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

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

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

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

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

     /**
     * The projects that belong to the user.
     */
    public function projects()
    {
        return $this->belongsToMany(Project::class, 'user_operations', 'user_id', 'project_id')
            ->withPivot([
                'designation',
                'tech_skills',
                'experience',
                'interpersonal_relationship',
                'power',
                'influence',
            ])
            ->withTimestamps();
    }

    /**
     * Get the user operations pivot records.
     */
    public function userOperations()
    {
        return $this->hasMany(UserOperation::class);
    }

     /**
     * Get the requirement operations for the user.
     */
    public function requirementOperations()
    {
        return $this->hasMany(RequirementOperation::class);
    }

    /**
     * Get the requirements associated with the user through operations.
     */
    public function requirementsThroughOperations()
    {
        return $this->hasManyThrough(Requirement::class, RequirementOperation::class, 'user_id', 'id', 'id', 'r_id');
    }

    /**
     * Get the non-functional requirements associated with the user through operations.
     */
    public function nonFunctionalRequirementsThroughOperations()
    {
        return $this->hasManyThrough(NonFunctionalRequirement::class, RequirementOperation::class, 'user_id', 'id', 'id', 'nfu_id');
    }
}
