<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    // ✅ No email, use phone + role
    protected $fillable = [
        'name',
        'phone',
        'role',
        'password',
    ];

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

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

    // ✅ Role helpers
    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    public function isCandidate(): bool
    {
        return $this->role === 'candidate';
    }

    public function isUser(): bool
    {
        return $this->role === 'user';
    }

    // One user can be one candidate
    public function candidate(): HasOne
    {
        return $this->hasOne(Candidate::class);
    }

    // Team manager
    public function managedTeams(): HasMany
    {
        return $this->hasMany(Team::class, 'manager_user_id');
    }

    // Team member (membership)
    public function teamMemberships(): HasMany
    {
        return $this->hasMany(TeamMember::class);
    }

    // Tasks created by this user (manager usually)
    public function createdTasks(): HasMany
    {
        return $this->hasMany(Task::class, 'created_by_user_id');
    }
}
