<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Passkeys\PasskeyAuthenticatable;
use Spatie\Permission\Traits\HasRoles;

#[Hidden(['password'])]
#[Guarded(['id', 'last_login_at'])]
#[Appends(['full_name'])]
class User extends Authenticatable
{
    use HasFactory, HasRoles, Notifiable, PasskeyAuthenticatable, SoftDeletes, TwoFactorAuthenticatable;

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

    protected function fullName(): Attribute
    {
        return Attribute::get(fn () => $this->profile?->full_name ?? $this->username);
    }

    public function lecturer(): HasOne
    {
        return $this->hasOne(Lecturer::class);
    }

    public function profile(): HasOne
    {
        return $this->hasOne(UserProfile::class);
    }

    public function student(): HasOne
    {
        return $this->hasOne(Student::class);
    }

    public function courseRegistrationSubmissionLogs(): HasMany
    {
        return $this->hasMany(CourseRegistrationSubmissionLog::class, 'actor_id');
    }

    public function createdAnnouncements(): HasMany
    {
        return $this->hasMany(Announcement::class, 'created_by');
    }

    public function createdNotifications(): HasMany
    {
        return $this->hasMany(Notification::class, 'created_by');
    }

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

    public function handledFeedbacks(): HasMany
    {
        return $this->hasMany(Feedback::class, 'handled_by');
    }

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

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

    public function processedLetterRequests(): HasMany
    {
        return $this->hasMany(LetterRequest::class, 'processed_by');
    }

    public function recordedTuitionPayments(): HasMany
    {
        return $this->hasMany(TuitionPayment::class, 'recorded_by');
    }

    public function reviewedCourseRegistrationSubmissions(): HasMany
    {
        return $this->hasMany(CourseRegistrationSubmission::class, 'reviewed_by');
    }

    public function uploads(): HasMany
    {
        return $this->hasMany(EditorUpload::class, 'uploaded_by');
    }

    public function isAdvisorOf(Student $student): bool
    {
        return $this->hasRole('dosen') && $this->lecturer && $student->academic_advisor_id === $this->lecturer->id;
    }

    public function isKaprodiOf(Student $student): bool
    {
        return $this->hasRole('kaprodi') && $this->lecturer && $student->department?->currentLeader?->lecturer_id === $this->lecturer->id;
    }

    /**
     * Gates page access: the student's academic advisor, the kaprodi of
     * their department, or staff roles may view a student's KRS.
     */
    public function canAccessCourseRegistrationOf(Student $student): bool
    {
        if ($this->hasRole('dosen') || $this->hasRole('kaprodi')) {
            return $this->isAdvisorOf($student) || $this->isKaprodiOf($student);
        }

        return true;
    }

    /**
     * Only the student's academic advisor may approve/reject; the kaprodi
     * signs but does not decide. Other staff roles retain override access.
     */
    public function canReviewCourseRegistrationOf(Student $student): bool
    {
        if ($this->hasRole('dosen')) {
            return $this->isAdvisorOf($student);
        }

        if ($this->hasRole('kaprodi')) {
            return false;
        }

        return true;
    }

    /**
     * @return array<int, int>
     */
    public function ledDepartmentIds(): array
    {
        return $this->lecturer
            ? $this->lecturer->leaderships()->whereNull('ended_at')->pluck('department_id')->all()
            : [];
    }
}
