<?php

namespace Tests\Feature\Admin;

use App\Models\User;
use Database\Seeders\DefaultAdminSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class DefaultAdminSeederTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_creates_the_default_admin_user(): void
    {
        config([
            'admin.default_name' => 'Default Admin',
            'admin.default_email' => 'admin@example.test',
            'admin.default_password' => 'SecurePassword#2026',
        ]);

        $this->seed(DefaultAdminSeeder::class);

        $admin = User::query()->where('email', 'admin@example.test')->firstOrFail();

        $this->assertSame('Default Admin', $admin->name);
        $this->assertSame('admin', $admin->role);
        $this->assertNotNull($admin->email_verified_at);
        $this->assertTrue(Hash::check('SecurePassword#2026', $admin->password));
        $this->assertTrue($admin->canAccessPanel(filament()->getPanel('admin')));
    }

    public function test_it_does_not_reset_an_existing_admin_password(): void
    {
        config([
            'admin.default_name' => 'Default Admin',
            'admin.default_email' => 'admin@example.test',
            'admin.default_password' => 'SecurePassword#2026',
        ]);

        $this->seed(DefaultAdminSeeder::class);

        $admin = User::query()->where('email', 'admin@example.test')->firstOrFail();
        $admin->forceFill([
            'password' => Hash::make('ChangedPassword#2026'),
        ])->save();

        $this->seed(DefaultAdminSeeder::class);

        $admin->refresh();

        $this->assertTrue(Hash::check('ChangedPassword#2026', $admin->password));
        $this->assertFalse(Hash::check('SecurePassword#2026', $admin->password));
    }
}
