<?php

namespace Tests\Feature\Web;

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

class SchoolYearAdminPageTest extends TestCase
{
    use RefreshDatabase;

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

        $compiledPath = storage_path('framework/views');
        if (!is_dir($compiledPath)) {
            mkdir($compiledPath, 0777, true);
        }

        config([
            'app.key' => 'base64:' . base64_encode(random_bytes(32)),
            'app.cipher' => 'AES-256-CBC',
            'jwt.secret' => bin2hex(random_bytes(32)),
            'view.compiled' => $compiledPath,
        ]);
    }

    public function test_admin_can_view_school_year_page(): void
    {
        $this->seedAdminContext();

        $response = $this->actingAs(User::query()->findOrFail(1))
            ->get('/administrator/school-years');

        $response->assertOk();
        $response->assertSee('School Years Management');
        $response->assertSee('JWT Authorization: Bearer');
        $response->assertSee('/api/v1/school-years');
    }

    public function test_admin_can_create_draft_school_year_from_page(): void
    {
        $this->seedAdminContext();

        $response = $this->actingAs(User::query()->findOrFail(1))
            ->post('/administrator/school-years', [
                'name' => '2026-2027',
                'start_date' => '2026-09-01',
                'end_date' => '2027-06-30',
            ]);

        $response->assertRedirect();
        $this->assertDatabaseHas('school_years', [
            'name' => '2026-2027',
            'status' => 'draft',
            'is_current' => 0,
        ]);
    }

    private function seedAdminContext(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        DB::table('roles')->insert([
            'id' => 1,
            'name' => 'administrator',
            'slug' => 'administrator',
            'is_active' => 1,
        ]);

        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Admin',
            'lastname' => 'User',
            'gender' => 'Female',
            'cellphone' => '5555555555',
            'email' => 'admin@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('user_roles')->insert([
            'user_id' => 1,
            'role_id' => 1,
        ]);

        DB::table('school_years')->insert([
            'id' => 1,
            'name' => '2025-2026',
            'start_date' => '2025-09-01',
            'end_date' => '2026-06-30',
            'status' => 'active',
            'is_current' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
