<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Jetstream\HasProfilePhoto;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens;

    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
    use HasRoles;

    protected $fillable = [
        'name',
        'email',
        'telefono',
        'direccion',
        'plantel',
        'password',
        'code',
        'status',
    ];


    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
        'status',
    ];

    protected $appends = [
        'profile_photo_url',
    ];

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


    // public function note() : HasOne
    // {
    //     //esta funcion es para retornar la relacion de un usario que contenga notas
    //     return $this->hasOne(Note::class);
    // }

    public function notes() : HasMany
    {
        return $this->hasMany(Note::class);
    }

    public function codes() : HasMany
    {
        return $this->hasMany(Code::class);
    }

    public function pagos() : HasMany
    {
        return $this->hasMany(Pago::class);
    }

    public function plantelUsuarios() : BelongsToMany
    {
        return $this->belongsToMany(Plantel::class)->withPivot('plantel_id');
    }

    public function existe($request = null)
    {
        if (!$request) {
            return false;
        }

        return $this->plantelUsuarios()
            ->wherePivot('plantel_id', $request)
            ->exists();
    }


    public function prospectos() : BelongsToMany
    {
        return $this->belongsToMany(Prospecto::class);
    }

}
