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

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

    // Default email domain for school admins without email
    public const DEFAULT_SCHOOL_EMAIL_DOMAIN = '@sekolah.local';

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'role_id',
        'kcd_id',
        'school_id',
        'employee_id',
        'npsn',
        'name',
        'email',
        'password',
        'phone',
        'nip',
        'pangkat_golongan',
        'alamat',
        'is_active',
    ];

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

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

    public function kcd()
    {
        return $this->belongsTo(Kcd::class);
    }

    public function school()
    {
        return $this->belongsTo(School::class);
    }

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

    public function kcdAssignments()
    {
        return $this->hasMany(SupervisorKcdAssignment::class);
    }

    public function schoolAssignments()
    {
        return $this->hasMany(SupervisorSchoolAssignment::class);
    }

    public function sptjmReports()
    {
        return $this->hasMany(SptjmReport::class, 'submitted_by');
    }

    public function formResponses()
    {
        return $this->hasMany(FormResponse::class);
    }

    // Helper methods
    public function isSuperadmin()
    {
        return $this->role->slug === 'superadmin';
    }

    public function isAdminDinas()
    {
        return $this->role->slug === 'admin_dinas';
    }

    public function isAdminKcd()
    {
        return $this->role->slug === 'admin_kcd';
    }

    public function isPengawas()
    {
        return $this->role->slug === 'pengawas';
    }

    public function isAdminSekolah()
    {
        return $this->role->slug === 'admin_sekolah';
    }

    public function isGuru()
    {
        return $this->role->slug === 'guru';
    }
}
