<?php

namespace Tests\Feature\Auth;

use App\Enums\UserType;
use App\Models\User;
use App\Services\Settings\SettingService;
use App\Support\InstallationState;
use App\Support\RegistrationSettings;
use App\Support\UserAccess;
use Database\Seeders\RoleAndPermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class RegistrationTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        File::put(app(InstallationState::class)->lockPath(), now()->toIso8601String());
        $this->seed(RoleAndPermissionSeeder::class);
    }

    public function test_register_page_is_blocked_when_disabled(): void
    {
        $this->get(route('register'))
            ->assertRedirect(route('login'))
            ->assertSessionHas('error');
    }

    public function test_login_page_hides_register_link_when_disabled(): void
    {
        $this->get(route('login'))
            ->assertOk()
            ->assertDontSee('Register as customer', false);
    }

    public function test_customer_can_register_when_enabled(): void
    {
        app(SettingService::class)->updateMany([
            'auth.allow_registration' => ['value' => '1', 'category' => 'auth', 'type' => 'boolean'],
        ]);

        $this->assertTrue(RegistrationSettings::isEnabled());

        $this->get(route('register'))
            ->assertOk()
            ->assertSee('Customer registration', false);

        $this->post(route('register.store'), [
            'name' => 'New Customer',
            'email' => 'customer@example.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
        ])
            ->assertRedirect(route('customer.home'));

        $user = User::query()->where('email', 'customer@example.com')->firstOrFail();

        $this->assertAuthenticatedAs($user);
        $this->assertTrue($user->isCustomer());
        $this->assertTrue($user->hasRole(UserAccess::customerRoleName()));
    }

    public function test_registered_customer_cannot_access_staff_dashboard(): void
    {
        app(SettingService::class)->updateMany([
            'auth.allow_registration' => ['value' => '1', 'category' => 'auth', 'type' => 'boolean'],
        ]);

        $this->post(route('register.store'), [
            'name' => 'Portal Customer',
            'email' => 'portal@example.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
        ]);

        $this->get(route('dashboard'))
            ->assertRedirect(route('customer.home'));
    }

    public function test_login_page_shows_register_link_when_enabled(): void
    {
        app(SettingService::class)->updateMany([
            'auth.allow_registration' => ['value' => '1', 'category' => 'auth', 'type' => 'boolean'],
        ]);

        $this->get(route('login'))
            ->assertOk()
            ->assertSee('Register as customer', false);
    }
}
