<?php

namespace Tests\Feature\Update;

use App\Models\User;
use App\Support\InstallationState;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

class AdminUpdatePageTest extends TestCase
{
    use RefreshDatabase;

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

        File::put(app(InstallationState::class)->lockPath(), now()->toIso8601String());

        config([
            'update.current_version' => '1.0.0',
            'update.current_build' => '1.0.0-release.1',
            'product_owner.installation_id' => '00000000-0000-0000-0000-000000000001',
            'product_owner.license_key' => 'FREE-TEST-0000-0000',
        ]);

        $this->seed(\Database\Seeders\RoleAndPermissionSeeder::class);
    }

    public function test_updates_page_shows_version_comparison(): void
    {
        $user = User::factory()->create();
        $user->assignRole(config('permissions.super_admin_role', 'Super Admin'));

        $this->actingAs($user)
            ->get(route('updates.index'))
            ->assertOk()
            ->assertSee('Update Admin', false)
            ->assertSee('Your Version', false)
            ->assertSee('Latest Version', false)
            ->assertSee('1.0.0', false)
            ->assertSee('An update is available', false);
    }

    public function test_check_now_refreshes_update_status(): void
    {
        $user = User::factory()->create();
        $user->assignRole(config('permissions.super_admin_role', 'Super Admin'));

        $this->actingAs($user)
            ->post(route('updates.check'))
            ->assertRedirect(route('updates.index'))
            ->assertSessionHas('status');
    }
}
