<?php

namespace Tests\Feature;

use App\Services\Installer\DatabaseConnectionTester;
use App\Services\Installer\InstallMigrationAction;
use App\Support\InstallationState;
use Tests\TestCase;

class InstallWizardFlowTest extends TestCase
{
    public function test_install_wizard_post_flow_with_mock_driver(): void
    {
        $this->post(route('installer.welcome.store'), [
            'app_name' => 'Test Admin',
            'app_url' => 'http://localhost',
        ])->assertRedirect(route('installer.requirements'));

        $this->post(route('installer.requirements.store'))
            ->assertRedirect(route('installer.database'));

        $this->mock(DatabaseConnectionTester::class, function ($mock): void {
            $mock->shouldReceive('test')->andReturn(['success' => true, 'message' => null]);
        });

        $this->mock(InstallMigrationAction::class, function ($mock): void {
            $mock->shouldReceive('execute')->once();
        });

        $this->post(route('installer.database.store'), [
            'db_connection' => 'mysql',
            'db_host' => '127.0.0.1',
            'db_port' => 3306,
            'db_database' => 'test_admin',
            'db_username' => 'root',
            'db_password' => '',
        ])->assertRedirect(route('installer.mail'));

        $this->post(route('installer.mail.store'), [
            'mail_mailer' => 'log',
            'mail_from_address' => 'admin@example.com',
            'mail_from_name' => 'Test Admin',
        ])->assertRedirect(route('installer.company'));

        $this->post(route('installer.company.store'), [
            'company_name' => 'Test Company',
            'company_email' => 'company@example.com',
            'timezone' => 'UTC',
            'locale' => 'en',
        ])->assertRedirect(route('installer.admin'));

        $this->post(route('installer.admin.store'), [
            'admin_name' => 'Admin User',
            'admin_email' => 'admin@example.com',
            'admin_phone' => '9954770099',
            'admin_password' => 'password123',
            'admin_password_confirmation' => 'password123',
        ])->assertRedirect(route('installer.review'));
    }

    public function test_installed_app_redirects_installer_to_dashboard(): void
    {
        app(InstallationState::class)->markInstalled();

        $this->get(route('installer.welcome'))
            ->assertRedirect(route('login'));
    }
}
