<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Models\Concerns\HasIndonesianTimestamps;
use Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

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

    public const CREATED_AT = 'dibuat_pada';
    public const UPDATED_AT = 'diperbarui_pada';

    public const ROLE_ADMIN = 'admin';
    public const ROLE_PETUGAS = 'petugas';

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'nama',
        'email',
        'kata_sandi',
        'peran',
        'aktif',
        'token_api',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'kata_sandi',
        'token_pengingat',
        'token_api',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_diverifikasi_pada' => 'datetime',
            'kata_sandi' => 'hashed',
            'aktif' => 'boolean',
        ];
    }

    public function getAuthPasswordName(): string
    {
        return 'kata_sandi';
    }

    public function getAuthPassword(): string
    {
        return (string) $this->kata_sandi;
    }

    public function getRememberTokenName(): string
    {
        return 'token_pengingat';
    }

    public static function roles(): array
    {
        return [
            self::ROLE_ADMIN => 'Admin Web',
            self::ROLE_PETUGAS => 'Petugas Mobile',
        ];
    }

    public function isAdmin(): bool
    {
        return $this->peran === self::ROLE_ADMIN;
    }

    public function isPetugas(): bool
    {
        return $this->peran === self::ROLE_PETUGAS;
    }

    public function getNameAttribute(): ?string
    {
        return $this->attributes['nama'] ?? null;
    }

    public function getPasswordAttribute(): ?string
    {
        return $this->attributes['kata_sandi'] ?? null;
    }

    public function getRoleAttribute(): ?string
    {
        return $this->attributes['peran'] ?? null;
    }

    public function getIsActiveAttribute(): bool
    {
        return (bool) ($this->attributes['aktif'] ?? false);
    }

    public function getApiTokenAttribute(): ?string
    {
        return $this->attributes['token_api'] ?? null;
    }
}
