<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use App\Models\AttendanceManagement\WorkingHour;
use App\Models\Central\BusinessAccount;
use App\Models\Central\BusinessAccountUser;
use App\Models\Staff\JobInfo;
use App\Models\Staff\Staff;
use App\Models\TimeManagement\TimeLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasPermissions;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasFactory, Notifiable, HasApiTokens, HasRoles, HasPermissions;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'username',
        'full_name',
        'phone_number',
        'email',
        'business_name',
        'user_type_id',
        'referral_code_id',
        'country_id',
        'registration_method_id',
        'password',
        'initial_password',
        'must_change_password'
    ];

    /**
     * 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 businesses under which the user is registered
     * @return BelongsToMany
     */
    public function business_accounts(): BelongsToMany
    {
        return $this->belongsToMany(BusinessAccount::class, BusinessAccountUser::class);
    }

    /**
     * Get user Audit trails
     * @return HasMany
     */
    public function logs(): HasMany
    {
        return $this->hasMany(AuditTrail::class, 'user_id');
    }

    /**
     * Get the departments created by the user
     * @return HasMany
     */
    public function created_departments(): HasMany
    {
        return $this->hasMany(Department::class, 'created_by');
    }

    /**
     * Get the positions created by the user
     * @return HasMany
     */
    public function created_positions(): HasMany
    {
        return $this->hasMany(Position::class, 'created_by');
    }

    /**
     * Get the business locations created by the user
     * @return HasMany
     */
    public function created_business_locations(): HasMany
    {
        return $this->hasMany(BusinessLocation::class, 'created_by');
    }

    public function staff(): HasOne
    {
        return $this->hasOne(Staff::class, 'user_id');
    }


    public function position(): HasOneThrough
    {
        return $this->hasOneThrough(Position::class,JobInfo::class, 'staff_id', 'id', 'id', 'position_id');
    }

    public function department(): HasOneThrough
    {
        return $this->hasOneThrough(Department::class,Position::class, 'id', 'id', 'position_id', 'department_id');
    }

    /**
     * Get the staff created by the user
     * @return HasMany
     */
    public function created_staff(): HasMany
    {
        return $this->hasMany(Staff::class, 'created_by');
    }

    /**
     * Get the time logs created by the user
     * @return HasMany
     */
    public function created_time_logs(): HasMany
    {
        return $this->hasMany(TimeLog::class, 'created_by');
    }


    /**
     * Get the time sheets created by the user
     * @return HasMany
     */
    public function created_time_sheets(): HasMany
    {
        return $this->hasMany(TimeLog::class, 'created_by');
    }

    /**
     * Get the work hours created by the user
     * @return HasMany
     */
    public function created_working_hours(): HasMany
    {
        return $this->hasMany(WorkingHour::class, 'created_by');
    }
}
