<?php

namespace Tests\App\Controllers\Api;

use App\Controllers\Api\InfoIconController;
use App\Models\UserModel;
use CodeIgniter\Test\CIUnitTestCase;

class StubInfoIconUserModel extends UserModel
{
    public function __construct()
    {
        // skip parent initialization to avoid DB dependencies
    }

    private array $data = [];

    public function setUserInfoById(int $id, ?array $info): void
    {
        $this->data[$id] = $info;
    }

    public function getUserInfoById($id): ?array
    {
        return $this->data[$id] ?? null;
    }
}

class TestableInfoIconController extends InfoIconController
{
    private ?object $user = null;
    private ?StubInfoIconUserModel $userModel = null;

    public function setCurrentUser(?object $user): void
    {
        $this->user = $user;
    }

    public function setUserModel(StubInfoIconUserModel $model): void
    {
        $this->userModel = $model;
    }

    protected function getCurrentUser(): ?object
    {
        return $this->user;
    }

    protected function createUserModel(): UserModel
    {
        return $this->userModel ?? parent::createUserModel();
    }

    protected function respond($data = null, ?int $status = null, string $message = '')
    {
        return ['status' => true, 'code' => $status, 'message' => $message, 'data' => $data];
    }

    protected function error(string $message = 'An error occurred', int $code = 400, ?array $errors = null)
    {
        return ['status' => false, 'code' => $code, 'message' => $message, 'errors' => $errors];
    }
}

class InfoIconControllerTest extends CIUnitTestCase
{
    private TestableInfoIconController $controller;
    private StubInfoIconUserModel $userModel;

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

        $this->controller = new TestableInfoIconController();
        $this->userModel = new StubInfoIconUserModel();
        $this->controller->setUserModel($this->userModel);
    }

    public function testRequiresAuthentication()
    {
        $response = $this->controller->profileIcon();

        $this->assertFalse($response['status']);
        $this->assertSame(401, $response['code']);
    }

    public function testReturnsNotFoundWhenUserMissing()
    {
        $this->controller->setCurrentUser((object) ['id' => 12]);

        $response = $this->controller->profileIcon();

        $this->assertFalse($response['status']);
        $this->assertSame(404, $response['code']);
    }

    public function testReturnsUserProfileInfo()
    {
        $this->controller->setCurrentUser((object) ['id' => 7]);
        $this->userModel->setUserInfoById(7, ['firstname' => 'Lana', 'lastname' => 'Kay', 'email' => 'test']);

        $response = $this->controller->profileIcon();

        $this->assertTrue($response['status']);
        $this->assertSame('Profile icon info retrieved successfully', $response['message']);
        $this->assertSame('Lana Kay', $response['data']['user_name']);
        $this->assertSame('LK', $response['data']['user_initials']);
    }
}
