<?php

namespace Tests\Feature\Api\V1\Roles;

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

class RoleSwitcherControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_roles(): void
    {
        $user = $this->seedUser();
        $roleId = $this->seedRole();

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $roleId,
        ]);

        Sanctum::actingAs($user);
        $response = $this->getJson('/api/v1/role-switcher');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertNotEmpty($response->json('data.roles'));
    }

    public function test_switch_sets_role(): void
    {
        $user = $this->seedUser();
        $roleId = $this->seedRole();

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $roleId,
        ]);

        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/role-switcher/switch', [
            'role' => 'admin',
        ]);

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $response->assertJsonPath('data.role', 'admin');
    }

    public function test_requires_authentication(): void
    {
        $response = $this->getJson('/api/v1/role-switcher');

        $response->assertStatus(401);
    }


    public function test_switch_rejects_role_that_user_does_not_have(): void
    {
        $user = $this->seedUser();
        $this->seedRole();
        $teacherRoleId = DB::table('roles')->insertGetId([
            'name' => 'teacher',
            'slug' => 'teacher',
            'description' => 'Teacher',
            'dashboard_route' => 'teacher/classes',
            'priority' => 2,
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $teacherRoleId,
        ]);

        Sanctum::actingAs($user);
        $this->postJson('/api/v1/role-switcher/switch', [
            'role' => 'admin',
        ])
            ->assertForbidden()
            ->assertJsonPath('status', false);
    }

    public function test_switch_validates_empty_role_payload(): void
    {
        $user = $this->seedUser();
        $this->seedRole();
        Sanctum::actingAs($user);

        $this->postJson('/api/v1/role-switcher/switch', [])
            ->assertUnprocessable();
    }

    public function test_index_returns_not_found_when_authenticated_user_has_no_roles(): void
    {
        $user = $this->seedUser();
        Sanctum::actingAs($user);

        $this->getJson('/api/v1/role-switcher')
            ->assertNotFound()
            ->assertJsonPath('status', false);
    }

    private function seedUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'switch@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($userId);
    }

    private function seedRole(): int
    {
        return DB::table('roles')->insertGetId([
            'name' => 'admin',
            'slug' => 'admin',
            'description' => 'Admin',
            'dashboard_route' => 'administrator/administratordashboard',
            'priority' => 1,
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
