<?php

use App\Models\User;
use Livewire\Livewire;
use Modules\Identity\Database\Seeders\RolePermissionSeeder;
use Modules\Identity\Filament\Pages\ManageAppSettings;
use Modules\Shared\Support\EnvFileWriter;

beforeEach(function () {
    $this->seed(RolePermissionSeeder::class);

    // Never let a test write to the real project .env — bind the writer to
    // a throwaway temp file instead.
    $this->envPath = sys_get_temp_dir().'/manage-app-settings-test-'.uniqid().'.env';
    file_put_contents($this->envPath, "APP_NAME=Laravel\n");
    app()->instance(EnvFileWriter::class, new EnvFileWriter($this->envPath));
});

afterEach(function () {
    @unlink($this->envPath);
});

test('an admin without manage_settings is forbidden from the app settings page', function () {
    $support = User::factory()->create();
    $support->assignRole('support');

    $this->actingAs($support)->get('/admin/manage-app-settings')->assertForbidden();
});

test('a super_admin can view and save app settings, writing them to .env', function () {
    $superAdmin = User::factory()->create();
    $superAdmin->assignRole('super_admin');
    $this->actingAs($superAdmin);

    Livewire::test(ManageAppSettings::class)
        ->assertOk()
        ->fillForm([
            'site_name' => 'EV Booking Co',
            'support_email' => 'help@evbooking.test',
            'support_phone' => '+95912345678',
            'timezone' => 'Asia/Yangon',
            'currency' => 'MMK',
            'front_seat_enabled' => false,
            'front_seat_max_per_booking' => 2,
            'back_seat_enabled' => false,
            'back_seat_max_per_booking' => 5,
            'whole_vehicle_enabled' => true,
            'whole_vehicle_max_per_booking' => 6,
            'booking_admin_emails' => ['ops@evbooking.test', 'dispatch@evbooking.test'],
            'sms_enabled' => true,
            'sms_server' => 'https://sms.example.test/send',
            'sms_token' => 'secret-token',
            'sms_sender' => 'EVBooking',
        ])
        ->call('save')
        ->assertHasNoFormErrors();

    $contents = file_get_contents($this->envPath);

    expect($contents)
        ->toContain('APP_NAME="EV Booking Co"')
        ->toContain('SUPPORT_EMAIL=help@evbooking.test')
        ->toContain('APP_TIMEZONE=Asia/Yangon')
        ->toContain('APP_CURRENCY=MMK')
        ->toContain('BOOKING_FRONT_SEAT_ENABLED=false')
        ->toContain('BOOKING_FRONT_SEAT_MAX_PER_BOOKING=2')
        ->toContain('BOOKING_BACK_SEAT_ENABLED=false')
        ->toContain('BOOKING_BACK_SEAT_MAX_PER_BOOKING=5')
        ->toContain('BOOKING_WHOLE_VEHICLE_ENABLED=true')
        ->toContain('BOOKING_WHOLE_VEHICLE_MAX_PER_BOOKING=6')
        ->toContain('BOOKING_ADMIN_EMAILS=ops@evbooking.test,dispatch@evbooking.test')
        ->toContain('SMS_ENABLED=true')
        ->toContain('SMS_SERVER=https://sms.example.test/send')
        ->toContain('SMS_TOKEN=secret-token')
        ->toContain('SMS_SENDER=EVBooking');
});

test('sms server and token are required once sms is enabled', function () {
    $superAdmin = User::factory()->create();
    $superAdmin->assignRole('super_admin');
    $this->actingAs($superAdmin);

    Livewire::test(ManageAppSettings::class)
        ->fillForm(['sms_enabled' => true, 'sms_server' => '', 'sms_token' => ''])
        ->call('save')
        ->assertHasFormErrors(['sms_server', 'sms_token']);
});

test('max per booking fields must be at least 1', function (string $field) {
    $superAdmin = User::factory()->create();
    $superAdmin->assignRole('super_admin');
    $this->actingAs($superAdmin);

    Livewire::test(ManageAppSettings::class)
        ->fillForm([$field => 0])
        ->call('save')
        ->assertHasFormErrors([$field]);
})->with([
    'front_seat_max_per_booking',
    'back_seat_max_per_booking',
    'whole_vehicle_max_per_booking',
]);
