<?php

namespace Tests\Unit;

use App\Models\User;
use App\Services\Auth\ApiLoginSecurityService;
use PHPUnit\Framework\TestCase;

class ApiLoginSecurityServiceTest extends TestCase
{
    public function test_roles_object_map_preserves_role_names(): void
    {
        $service = new ApiLoginSecurityService();
        $map = $service->rolesToObjectMap(['Teacher', 'Admin']);

        $this->assertTrue($map->Teacher);
        $this->assertTrue($map->Admin);

        $payload = json_encode(['roles' => $map]);
        $this->assertIsString($payload);
        $this->assertStringContainsString('"Teacher":true', $payload);
        $this->assertStringContainsString('"Admin":true', $payload);
    }

    public function test_build_login_response_includes_teacher_class_context(): void
    {
        $service = new ApiLoginSecurityService();

        $user = $this->getMockBuilder(User::class)
            ->onlyMethods(['roleNames', 'teacherSessionContext'])
            ->getMock();

        $user->id = 99;
        $user->firstname = 'Amina';
        $user->lastname = 'Teacher';

        $user->method('roleNames')->willReturn(['Teacher']);
        $user->method('teacherSessionContext')->willReturn([
            'class_section_id' => 42,
            'class_section_name' => 'Grade 5 - A',
        ]);

        $payload = $service->buildLoginResponse($user, 'jwt-token');

        $this->assertTrue($payload['status']);
        $this->assertSame('jwt-token', $payload['token']);
        $this->assertSame('jwt-token', $payload['access_token']);
        $this->assertSame('bearer', $payload['token_type']);
        $this->assertSame(99, $payload['user']['id']);
        $this->assertSame('Amina Teacher', $payload['user']['name']);
        $this->assertSame('Amina', $payload['user']['firstname']);
        $this->assertSame('Teacher', $payload['user']['lastname']);
        $this->assertSame(42, $payload['user']['class_section_id']);
        $this->assertSame('Grade 5 - A', $payload['user']['class_section_name']);
        $this->assertTrue($payload['user']['roles']->Teacher);
    }

    public function test_build_login_response_includes_teacher_assistant_class_context(): void
    {
        $service = new ApiLoginSecurityService();

        $user = $this->getMockBuilder(User::class)
            ->onlyMethods(['roleNames', 'teacherSessionContext'])
            ->getMock();

        $user->id = 100;
        $user->firstname = 'Samir';
        $user->lastname = 'TA';

        $user->method('roleNames')->willReturn(['Teacher_Assistant']);
        $user->method('teacherSessionContext')->willReturn([
            'class_section_id' => 17,
            'class_section_name' => 'Grade 3 - B',
        ]);

        $payload = $service->buildLoginResponse($user, 'jwt-token');

        $this->assertSame(17, $payload['user']['class_section_id']);
        $this->assertSame('Grade 3 - B', $payload['user']['class_section_name']);
        $this->assertTrue($payload['user']['roles']->Teacher_Assistant);
    }
}
