<?php

namespace Tests\Feature;

use App\Models\Business;
use App\Models\Setting;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;

class AdditionalModuleToggleTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
    }

    public function test_system_settings_lists_optional_operational_modules(): void
    {
        $admin = User::where('username', 'admin')->firstOrFail();

        $this->actingAs($admin)
            ->get(route('admin.system-settings.edit'))
            ->assertOk()
            ->assertSee('Tasks')
            ->assertSee('Calendar')
            ->assertSee('Loan Write-off')
            ->assertSee('Payroll')
            ->assertSee('Remote Payments');
    }

    public function test_disabled_modules_block_routes_and_are_hidden_from_navigation(): void
    {
        $admin = User::where('username', 'admin')->firstOrFail();
        foreach (['tasks', 'calendar', 'loan_writeoffs', 'payroll', 'remote_payments'] as $module) {
            $this->setModule($module, false);
        }

        $this->actingAs($admin)->get(route('tasks.index'))->assertNotFound();
        $this->actingAs($admin)->get(route('calendar.index'))->assertNotFound();
        $this->actingAs($admin)->get(route('loan-write-offs.index'))->assertNotFound();
        $this->actingAs($admin)->get(route('payroll.index'))->assertNotFound();
        $this->actingAs($admin)->get(route('remote-payments.index'))->assertNotFound();
        $this->get(route('remote-payments.public.create'))->assertNotFound();

        $dashboard = $this->actingAs($admin)->get(route('dashboard'))->assertOk();
        $dashboard->assertDontSee('href="'.route('tasks.index').'"', false);
        $dashboard->assertDontSee('href="'.route('calendar.index').'"', false);
        $dashboard->assertDontSee('href="'.route('loan-write-offs.index').'"', false);
        $dashboard->assertDontSee('href="'.route('payroll.index').'"', false);
        $dashboard->assertDontSee('href="'.route('remote-payments.index').'"', false);
    }

    public function test_modules_are_enabled_by_default_for_backward_compatibility(): void
    {
        $admin = User::where('username', 'admin')->firstOrFail();

        $this->actingAs($admin)->get(route('tasks.index'))->assertOk();
        $this->actingAs($admin)->get(route('calendar.index'))->assertOk();
        $this->actingAs($admin)->get(route('loan-write-offs.index'))->assertOk();
        $this->actingAs($admin)->get(route('payroll.index'))->assertOk();
        $this->actingAs($admin)->get(route('remote-payments.index'))->assertOk();
        $this->get(route('remote-payments.public.create'))->assertOk();
    }

    public function test_task_automation_is_skipped_when_tasks_module_is_disabled(): void
    {
        $this->setModule('tasks', false);

        $exit = Artisan::call('management:process-work', ['--date' => '2026-08-13']);

        $this->assertSame(0, $exit);
        $this->assertStringContainsString('Tasks module disabled; task generation skipped', Artisan::output());
    }

    private function setModule(string $key, bool $enabled): void
    {
        $business = Business::firstOrFail();
        Setting::updateOrCreate(
            ['business_id' => $business->id, 'group' => 'modules', 'key' => $key],
            ['value' => $enabled ? '1' : '0', 'type' => 'boolean'],
        );
    }
}
