<?php

namespace Tests\Feature;

use App\Http\Controllers\InstallController;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class InstallerTest extends TestCase
{
    use RefreshDatabase;

    protected function tearDown(): void
    {
        // Cleanup installed file after tests if generated
        if (File::exists(storage_path('installed'))) {
            File::delete(storage_path('installed'));
        }
        parent::tearDown();
    }

    public function test_uninstalled_system_redirects_to_install(): void
    {
        if (File::exists(storage_path('installed'))) {
            File::delete(storage_path('installed'));
        }

        $response = $this->get('/');
        $response->assertRedirect(route('install.welcome'));
    }

    public function test_installer_welcome_screen_renders_successfully(): void
    {
        if (File::exists(storage_path('installed'))) {
            File::delete(storage_path('installed'));
        }

        $response = $this->get('/install');
        $response->assertStatus(200);
        $response->assertSee('Welcome to MyOpticals');
    }

    public function test_installer_requirements_check_renders_successfully(): void
    {
        if (File::exists(storage_path('installed'))) {
            File::delete(storage_path('installed'));
        }

        $response = $this->get('/install/requirements');
        $response->assertStatus(200);
        $response->assertSee('PHP Version');
        $response->assertSee('Required PHP Extensions');
    }

    public function test_database_test_connection_endpoint(): void
    {
        $response = $this->postJson('/install/database/test', [
            'db_connection' => 'sqlite',
        ]);

        $response->assertStatus(200);
        $response->assertJson(['success' => true]);
    }

    public function test_full_installation_process_creates_admin_and_lock_file(): void
    {
        if (File::exists(storage_path('installed'))) {
            File::delete(storage_path('installed'));
        }

        $response = $this->post('/install/process', [
            'app_name' => 'MyOpticals Test',
            'app_url' => 'http://localhost:8000',
            'db_connection' => 'sqlite',
            'admin_name' => 'Super Admin Test',
            'admin_email' => 'admin_test@myopticals.com',
            'admin_phone' => '9998887776',
            'admin_password' => 'Secret123',
        ]);

        $response->assertRedirect(route('install.complete'));
        $this->assertTrue(File::exists(storage_path('installed')));
        $this->assertDatabaseHas('users', [
            'email' => 'admin_test@myopticals.com',
            'role' => 'superadmin',
        ]);
    }

    public function test_installed_system_blocks_access_to_installer(): void
    {
        File::put(storage_path('installed'), '{"installed": true}');

        $response = $this->get('/install');
        $response->assertRedirect(route('home'));
    }
}
