<?php

namespace App\Models;

use App\enum\UserLevel;
use Database\Factories\UserFactory;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements FilamentUser, MustVerifyEmail
{
    /** @use HasFactory<UserFactory> */
    use Billable, HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'level',
    ];

    /**
     * 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',
            'level' => UserLevel::class,
        ];
    }

    public function initials(): string
    {
        return Str::of($this->name)
            ->explode(' ')
            ->map(fn (string $name) => Str::of($name)->substr(0, 1))
            ->implode('');
    }

    public function canAccessPanel(Panel $panel): bool
    {
        return str_ends_with($this->email, '@zemail.me') && $this->level === UserLevel::SUPERADMIN && $this->hasVerifiedEmail();
    }

    /**
     * Scope to query only super admin users.
     */
    public function scopeIsSuperAdmin($query)
    {
        return $query->where('level', UserLevel::SUPERADMIN->value);
    }

    /**
     * Scope to query only normal users.
     */
    public function scopeIsNormalUser($query)
    {
        return $query->where('level', UserLevel::NORMALUSER->value);
    }

    /**
     * Scope to query only banner users.
     */
    public function scopeIsBannerUser($query)
    {
        return $query->where('level', UserLevel::BANNEDUSER->value);
    }

    /**
     * Check if user is a super admin.
     */
    public function isSuperAdmin(): bool
    {
        return $this->level === UserLevel::SUPERADMIN;
    }

    /**
     * Check if user is a normal user.
     */
    public function isNormalUser(): bool
    {
        return $this->level === UserLevel::NORMALUSER;
    }

    /**
     * Check if user is a banner user.
     */
    public function isBannerUser(): bool
    {
        return $this->level === UserLevel::BANNEDUSER;
    }

    /**
     * Get user level name.
     */
    public function getLevelName(): string
    {
        return $this->level->name;
    }

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

    public function logs()
    {
        return $this->hasMany(Log::class);
    }

    public function usageLogs()
    {
        return $this->hasMany(UsageLog::class);
    }

    public function impersonationLogs()
    {
        return $this->hasMany(ImpersonationLog::class, 'admin_id');
    }

    public function impersonationTargets()
    {
        return $this->hasMany(ImpersonationLog::class, 'target_user_id');
    }
}
