<?php

namespace Tests\Unit\Models;

use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;
use Filament\Models\Contracts\FilamentUser;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Laravel\Sanctum\NewAccessToken;
use App\Models\Log;
use App\Models\Ticket;
use App\Models\UsageLog;
use App\Models\User;
use Filament\Panel;
use Tests\TestCase;

class UserTest extends TestCase
{
    public $user;
    protected function setUp(): void
    {
        parent::setUp();

        $this->user = User::factory()->create();
    }

    /** @test */
    public function it_can_create_a_user_with_factory(): void
    {
        $this->assertInstanceOf(User::class, $this->user);
        $this->assertIsString($this->user->name);
        $this->assertIsString($this->user->email);
        $this->assertIsString($this->user->password);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $userData = [
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => 'password',
        ];

        $user = User::query()->create($userData);

        $this->assertEquals('Test User', $user->name);
        $this->assertEquals('test@example.com', $user->email);
        $this->assertNotEquals('password', $user->password); // Should be hashed
    }

    /** @test */
    public function it_hides_sensitive_attributes(): void
    {
        $userArray = $this->user->toArray();

        $this->assertArrayNotHasKey('password', $userArray);
        $this->assertArrayNotHasKey('remember_token', $userArray);
    }

    /** @test */
    public function it_casts_email_verified_at_to_datetime(): void
    {
        $this->user->email_verified_at = now();
        $this->user->save();

        $this->assertInstanceOf(Carbon::class, $this->user->email_verified_at);
    }

    /** @test */
    public function it_hashes_password(): void
    {
        $plainPassword = 'password123';
        $user = User::query()->create([
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => $plainPassword,
        ]);

        $this->assertNotEquals($plainPassword, $user->password);
        $this->assertTrue(Hash::check($plainPassword, $user->password));
    }

    /** @test */
    public function it_generates_initials_correctly(): void
    {
        $user = User::factory()->create(['name' => 'John Doe']);
        $this->assertEquals('JD', $user->initials());

        $user = User::factory()->create(['name' => 'John']);
        $this->assertEquals('J', $user->initials());

        $user = User::factory()->create(['name' => 'John Michael Smith']);
        $this->assertEquals('JMS', $user->initials());
    }

    /** @test */
    public function it_can_access_filament_panel_when_conditions_are_met(): void
    {
        $adminUser = User::factory()->create([
            'email' => 'admin1@zemail.me',
            'level' => 9,
            'email_verified_at' => now(),
        ]);

        $panel = $this->mock(Panel::class);

        $this->assertTrue($adminUser->canAccessPanel($panel));
    }

    /** @test */
    public function it_cannot_access_filament_panel_when_email_does_not_end_with_zemail_me(): void
    {
        $user = User::factory()->create([
            'email' => 'user@gmail.com',
            'level' => 9,
            'email_verified_at' => now(),
        ]);

        $panel = $this->mock(Panel::class);

        $this->assertFalse($user->canAccessPanel($panel));
    }

    /** @test */
    public function it_cannot_access_filament_panel_when_level_is_not_9(): void
    {
        $user = User::factory()->create([
            'email' => 'admin2@zemail.me',
            'level' => 1,
            'email_verified_at' => now(),
        ]);

        $panel = $this->mock(Panel::class);

        $this->assertFalse($user->canAccessPanel($panel));
    }

    /** @test */
    public function it_cannot_access_filament_panel_when_email_is_not_verified(): void
    {
        $user = User::factory()->create([
            'email' => 'admin3@zemail.me',
            'level' => 9,
            'email_verified_at' => null,
        ]);

        $panel = $this->mock(Panel::class);

        $this->assertFalse($user->canAccessPanel($panel));
    }

    /** @test */
    public function it_has_many_tickets_relationship(): void
    {
        $ticket = Ticket::factory()->create(['user_id' => $this->user->id]);

        $this->assertCount(1, $this->user->tickets);
        $this->assertEquals($ticket->id, $this->user->tickets->first()->id);
    }

    /** @test */
    public function it_has_many_logs_relationship(): void
    {
        $log = Log::factory()->create(['user_id' => $this->user->id]);

        $this->assertCount(1, $this->user->logs);
        $this->assertEquals($log->id, $this->user->logs->first()->id);
    }

    /** @test */
    public function it_has_many_usage_logs_relationship(): void
    {
        $usageLog = UsageLog::factory()->create(['user_id' => $this->user->id]);

        $this->assertCount(1, $this->user->usageLogs);
        $this->assertEquals($usageLog->id, $this->user->usageLogs->first()->id);
    }

    /** @test */
    public function it_uses_required_traits(): void
    {
        $traits = class_uses(User::class);

        $this->assertArrayHasKey(HasFactory::class, $traits);
        $this->assertArrayHasKey(Notifiable::class, $traits);
        $this->assertArrayHasKey(Billable::class, $traits);
        $this->assertArrayHasKey(HasApiTokens::class, $traits);
    }

    /** @test */
    public function it_implements_required_interfaces(): void
    {
        $user = new User;

        $this->assertInstanceOf(FilamentUser::class, $user);
        $this->assertInstanceOf(MustVerifyEmail::class, $user);
    }

    /** @test */
    public function it_extends_authenticatable(): void
    {
        $this->assertInstanceOf(\Illuminate\Foundation\Auth\User::class, $this->user);
    }

    /** @test */
    public function it_can_create_api_token(): void
    {
        $token = $this->user->createToken('test-token');

        $this->assertInstanceOf(NewAccessToken::class, $token);
        $this->assertCount(1, $this->user->tokens);
    }

    /** @test */
    public function it_can_delete_tokens(): void
    {
        $this->user->createToken('test-token');
        $this->user->tokens()->delete();

        $this->assertCount(0, $this->user->fresh()->tokens);
    }
}
