<?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;
use Laravel\Sanctum\HasApiTokens;
use Carbon\Carbon;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'gambar',
        'logo',
        'kampus',
        'masa_aktif'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'masa_aktif' => 'date',
    ];

    /**
     * Cek apakah user masih aktif (masa_aktif belum lewat)
     * 
     * @return bool
     */
    public function isActive()
    {
        // Jika masa_aktif null, dianggap aktif (admin utama/default)
        if (is_null($this->masa_aktif)) {
            return true;
        }
        
        // Cek apakah tanggal sekarang <= masa_aktif
        return Carbon::now()->lessThanOrEqualTo($this->masa_aktif);
    }

    /**
     * Cek apakah user sudah melewati masa aktif
     * 
     * @return bool
     */
    public function isExpired()
    {
        return !$this->isActive();
    }

    /**
     * Mendapatkan sisa hari masa aktif
     * 
     * @return int|null
     */
    public function getSisaHariAktif()
    {
        if (is_null($this->masa_aktif)) {
            return null; // Unlimited
        }
        
        $now = Carbon::now();
        if ($now->greaterThan($this->masa_aktif)) {
            return 0;
        }
        
        return $now->diffInDays($this->masa_aktif);
    }

    /**
     * Cek apakah user adalah admin terbatas (id 13)
     * 
     * @return bool
     */
    public function isRestrictedAdmin()
    {
        return $this->id === 13;
    }

    /**
     * Mendapatkan menu yang diizinkan untuk user terbatas
     * 
     * @return array
     */
    public function getAllowedMenus()
    {
        if ($this->isRestrictedAdmin()) {
            return [
                'dashboard',
                'daftar',
                'tracer',
                'gallery',
                'berita',
                'brosur'
            ];
        }
        
        // Untuk admin biasa, semua menu diizinkan
        return [
            'dashboard',
            'daftar',
            'tracer',
            'menu',
            'pages',
            'gallery',
            'settings',
            'user',
            'berita',
            'biaya',
            'brosur',
            'kurikulum'
        ];
    }

    /**
     * Cek apakah user bisa mengakses menu tertentu
     * 
     * @param string $menuRoute
     * @return bool
     */
    public function canAccessMenu($menuRoute)
    {
        $allowedMenus = $this->getAllowedMenus();
        return in_array($menuRoute, $allowedMenus);
    }
}