<?php

namespace Tests\App\Helpers;

use CodeIgniter\Test\CIUnitTestCase;

class AttendanceCommentHelperTest extends CIUnitTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        helper('attendance_comment');
    }

    public function testTemplateMatchReturnsDirectRangeHit(): void
    {
        $templates = [
            ['min_score' => 60, 'max_score' => 69, 'template_text' => 'below average'],
            ['min_score' => 70, 'max_score' => 79, 'template_text' => 'fair'],
        ];

        $match = attendance_comment_template_match($templates, 70.0);

        $this->assertNotNull($match);
        $this->assertSame('fair', $match['template_text']);
    }

    public function testTemplateMatchFallsBackToNearestLowerBandForDecimalGap(): void
    {
        $templates = [
            ['min_score' => 60, 'max_score' => 69, 'template_text' => 'below average'],
            ['min_score' => 70, 'max_score' => 79, 'template_text' => 'fair'],
        ];

        $match = attendance_comment_template_match($templates, 69.23);

        $this->assertNotNull($match);
        $this->assertSame('below average', $match['template_text']);
    }

    public function testCommentUsesResolvedTemplateForDecimalGap(): void
    {
        $templates = [
            ['min_score' => 60, 'max_score' => 69, 'template_text' => 'attendance has been below average.'],
            ['min_score' => 70, 'max_score' => 79, 'template_text' => 'attendance has been fair.'],
        ];

        $match = attendance_comment_template_match($templates, 69.23);

        $this->assertNotNull($match);
        $this->assertSame('attendance has been below average.', $match['template_text']);
    }
}
