<?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;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'business_id',
        'name',
        'email',
        'password',
        'parent_user_id', 
        'is_admin',
        'team_name',
        'reporting_manager_id',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, 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',
        ];
    }

    /**
     * Get the business that the user belongs to.
     */
    public function business()
    {
        return $this->belongsTo(Business::class);
    }

    public function teams()
    {
        return $this->belongsToMany(Team::class)->withTimestamps();
    }

    public function managedTeams()
    {
        return $this->hasMany(Team::class, 'manager_id');
    }

    public function employee()
    {
        return $this->hasOne(Employee::class);
    }

    public function managedDepartments()
    {
        return $this->hasMany(Department::class, 'manager_user_id');
    }

    /**
     * Get the businesses owned by the user.
     */
    public function ownedBusinesses()
    {
        return $this->hasMany(Business::class, 'owner_id');
    }

    /**
     * Check if user has verified email
     */
    public function hasVerifiedEmail()
    {
        return !is_null($this->email_verified_at);
    }

    /**
     * Mark the given user's email as verified.
     */
    public function markEmailAsVerified()
    {
        return $this->forceFill([
            'email_verified_at' => $this->freshTimestamp(),
        ])->save();
    }

    /**
     * Get the email address that should be used for verification.
     */
    public function getEmailForVerification()
    {
        return $this->email;
    }

    /**
     * Get the parent user (who created this user)
     */
    public function parentUser()
    {
        return $this->belongsTo(User::class, 'parent_user_id');
    }

    /**
     * Get all users created by this user
     */
    public function childUsers()
    {
        return $this->hasMany(User::class, 'parent_user_id');
    }

    public function reportingManager()
    {
        return $this->belongsTo(User::class, 'reporting_manager_id');
    }

    public function teamMembers()
    {
        return $this->hasMany(User::class, 'reporting_manager_id');
    }

    /**
     * Get all access configurations for this user
     */
    // public function userAccesses()
    // {
    //     return $this->hasMany(UserAccess::class);
    // }

    public function userAccesses()
    {
        return $this->hasMany(UserAccess::class, 'user_id', 'id');
    }

    public function accessTypes()
    {
        return $this->hasManyThrough(
            AccessType::class,     // final model
            UserAccess::class,     // intermediate model
            'user_id',             // FK on user_accesses table
            'id',                  // PK on access_types table
            'id',                  // PK on users table
            'access_type_id'       // FK on user_accesses table
        );
}

}
