<?php

namespace App\Models;

use App\Enums\IsActive;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
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 Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements FilamentUser, MustVerifyEmail
{
    use HasFactory, HasRoles, Notifiable, SoftDeletes;

    protected $guarded = ['id'];

    protected $hidden = ['password', 'remember_token'];

    protected function casts(): array
    {
        return [
            'password' => 'hashed',
            'is_active' => IsActive::class,
            'email_verified_at' => 'timestamp',
        ];
    }

    public function canAccessPanel(Panel $panel): bool
    {
        return true;
    }

    #[Scope]
    protected function withoutDeveloper(Builder $query): void
    {
        $query->whereDoesntHave('roles', function ($q) {
            $q->where('name', 'Developer');
        });
    }

    #[Scope]
    protected function active(Builder $query): void
    {
        $query->where('is_active', IsActive::ACTIVE);
    }

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

    public function contacts(): HasMany
    {
        return $this->hasMany(Contact::class, 'replied_by');
    }

    public function news(): HasMany
    {
        return $this->hasMany(News::class, 'author_id');
    }

    public function verificationRequests(): HasMany
    {
        return $this->hasMany(VerificationRequest::class, 'submitted_by');
    }

    public function verificationReviews(): HasMany
    {
        return $this->hasMany(VerificationReview::class, 'reviewer_id');
    }
}
