<?php

namespace Tests\App\Controllers\Api;

use App\Controllers\Api\ParentController;
use App\Models\AllergyModel;
use App\Models\ClassSectionModel;
use App\Models\MedicalConditionModel;
use App\Models\StudentClassModel;
use App\Models\StudentModel;
use App\Models\UserModel;
use CodeIgniter\Test\CIUnitTestCase;

class TestableParentController extends ParentController
{
    public function __construct()
    {
        // Avoid real model initialization
    }

    public function setModels(UserModel $userModel, StudentModel $studentModel, StudentClassModel $studentClassModel, ClassSectionModel $classSectionModel, MedicalConditionModel $medicalConditionModel, AllergyModel $allergyModel): void
    {
        $this->userModel = $userModel;
        $this->studentModel = $studentModel;
        $this->studentClassModel = $studentClassModel;
        $this->classSectionModel = $classSectionModel;
        $this->medicalConditionModel = $medicalConditionModel;
        $this->allergyModel = $allergyModel;
    }
}

class ParentControllerTest extends CIUnitTestCase
{
    private TestableParentController $controller;
    private $userModel;
    private $studentModel;
    private $studentClassModel;
    private $classSectionModel;
    private $medicalConditionModel;
    private $allergyModel;

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

        $this->userModel = $this->createMock(UserModel::class);
        $this->studentModel = $this->createMock(StudentModel::class);
        $this->studentClassModel = $this->createMock(StudentClassModel::class);
        $this->classSectionModel = $this->createMock(ClassSectionModel::class);
        $this->medicalConditionModel = $this->createMock(MedicalConditionModel::class);
        $this->allergyModel = $this->createMock(AllergyModel::class);

        $this->controller = new TestableParentController();
        $this->controller->setModels(
            $this->userModel,
            $this->studentModel,
            $this->studentClassModel,
            $this->classSectionModel,
            $this->medicalConditionModel,
            $this->allergyModel
        );
    }

    public function testShowReturnsParentById()
    {
        $this->userModel->method('select')->willReturnSelf();
        $this->userModel->method('join')->willReturnSelf();
        $this->userModel->method('where')->willReturnSelf();
        $this->userModel->method('first')->willReturn(['id' => 7, 'firstname' => 'Sara']);

        $response = $this->controller->show(7);
        $this->assertTrue($response['status']);
        $this->assertEquals('Parent retrieved successfully', $response['message']);
    }

    public function testShowMissingParentReturnsError()
    {
        $this->userModel->method('select')->willReturnSelf();
        $this->userModel->method('join')->willReturnSelf();
        $this->userModel->method('where')->willReturnSelf();
        $this->userModel->method('first')->willReturn(null);

        $response = $this->controller->show(99);
        $this->assertFalse($response['status']);
        $this->assertEquals(404, $response['code']);
    }

    public function testGetStudentsReturnsStudentList()
    {
        $this->userModel->method('find')->with(5)->willReturn(['id' => 5]);
        $this->studentModel->method('where')->willReturnSelf();
        $this->studentModel->method('findAll')->willReturn([
            ['id' => 11, 'firstname' => 'Meera', 'lastname' => 'Saleh'],
        ]);

        $this->studentClassModel->method('select')->willReturnSelf();
        $this->studentClassModel->method('join')->willReturnSelf();
        $this->studentClassModel->method('where')->willReturnSelf();
        $this->studentClassModel->method('orderBy')->willReturnSelf();
        $this->studentClassModel->method('first')->willReturn(['id' => 2, 'class_section_id' => 3]);

        $this->classSectionModel->method('getClassSectionNameBySectionId')->with(3)->willReturn('Grade 1A');

        $this->medicalConditionModel->method('where')->willReturnSelf();
        $this->medicalConditionModel->method('findColumn')->willReturn([]);
        $this->allergyModel->method('where')->willReturnSelf();
        $this->allergyModel->method('findColumn')->willReturn([]);

        $response = $this->controller->getStudents(5);

        $this->assertTrue($response['status']);
        $this->assertNotEmpty($response['data']);
        $this->assertEquals('Grade 1A', $response['data'][0]['class_section']);
        $this->assertEquals(3, $response['data'][0]['current_class']['class_section_id']);
    }

    public function testGetStudentsMissingParent()
    {
        $this->userModel->method('find')->with(9)->willReturn(null);

        $response = $this->controller->getStudents(9);
        $this->assertFalse($response['status']);
        $this->assertEquals(404, $response['code']);
    }
}
