<?php

namespace App\Models;

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

class User extends Authenticatable
{
    use HasFactory, Notifiable, SoftDeletes;

    protected $fillable = [
        'role_id',
        'shift_id',
        'name',
        'nip_nis',
        'fingerprint_pin',
        'class_name',
        'student_status',
        'email',
        'phone_number',
        'parent_token',
        'password',
        'photo_path',
        'is_active',
        'force_password_change',
    ];

    public function getParentPhoneAttribute(): ?string
    {
        return $this->phone_number;
    }

    public function setParentPhoneAttribute($value): void
    {
        $this->attributes['phone_number'] = $value;
    }

    protected static function booted(): void
    {
        static::creating(function ($user) {
            if (empty($user->parent_token)) {
                $user->parent_token = \Illuminate\Support\Str::random(32);
            }
        });

        static::created(function (User $user) {
            if (empty($user->fingerprint_pin)) {
                $user->fingerprint_pin = substr((string) $user->id, 0, 8);
                $user->saveQuietly();
            }
        });

        static::deleting(function (User $user) {
            if (!$user->isForceDeleting()) {
                $user->email = 'deleted_' . $user->id . '_' . $user->email;
                $user->saveQuietly();
            }
        });
    }

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

    protected function casts(): array
    {
        return [
            'password' => 'hashed',
            'force_password_change' => 'boolean',
            'is_active' => 'boolean',
        ];
    }

    public function role(): BelongsTo
    {
        return $this->belongsTo(Role::class);
    }

    public function shift(): BelongsTo
    {
        return $this->belongsTo(Shift::class);
    }

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

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

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

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

    public function hasRole(string $roleName): bool
    {
        return $this->role && $this->role->name === $roleName;
    }

    public function hasPermissionTo(string $permissionName): bool
    {
        if ($this->isSuperAdmin()) {
            return true;
        }

        return $this->role ? $this->role->hasPermissionTo($permissionName) : false;
    }

    public function isSuperAdmin(): bool
    {
        return $this->hasRole('super_admin');
    }

    public function isAdminTU(): bool
    {
        return $this->hasRole('admin_tu');
    }
}
