<?php

namespace Tests\Unit\Models;

use App\Models\AttendanceEmailTemplate;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AttendanceEmailTemplateTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_template_returns_exact_active_variant_when_available(): void
    {
        AttendanceEmailTemplate::query()->create([
            'code' => 'attendance_absent',
            'variant' => 'default',
            'subject' => 'Default Subject',
            'body_html' => '<p>Default</p>',
            'is_active' => true,
        ]);

        AttendanceEmailTemplate::query()->create([
            'code' => 'attendance_absent',
            'variant' => 'answered',
            'subject' => 'Parent Subject',
            'body_html' => '<p>Parent</p>',
            'is_active' => true,
        ]);

        $result = (new AttendanceEmailTemplate())->getTemplate('attendance_absent', 'answered');

        $this->assertIsArray($result);
        $this->assertSame('answered', $result['variant']);
        $this->assertSame('Parent Subject', $result['subject']);
    }

    public function test_get_template_falls_back_to_default_when_requested_variant_missing(): void
    {
        AttendanceEmailTemplate::query()->create([
            'code' => 'attendance_late',
            'variant' => 'default',
            'subject' => 'Late Default',
            'body_html' => '<p>Late default</p>',
            'is_active' => true,
        ]);

        $result = (new AttendanceEmailTemplate())->getTemplate('attendance_late', 'answered');

        $this->assertIsArray($result);
        $this->assertSame('default', $result['variant']);
        $this->assertSame('Late Default', $result['subject']);
    }

    public function test_get_template_ignores_inactive_exact_variant_and_uses_active_default(): void
    {
        AttendanceEmailTemplate::query()->create([
            'code' => 'attendance_present',
            'variant' => 'no_answer',
            'subject' => 'Inactive SMS',
            'body_html' => '<p>Inactive</p>',
            'is_active' => false,
        ]);

        AttendanceEmailTemplate::query()->create([
            'code' => 'attendance_present',
            'variant' => 'default',
            'subject' => 'Active Default',
            'body_html' => '<p>Active</p>',
            'is_active' => true,
        ]);

        $result = (new AttendanceEmailTemplate())->getTemplate('attendance_present', 'no_answer');

        $this->assertIsArray($result);
        $this->assertSame('default', $result['variant']);
        $this->assertSame('Active Default', $result['subject']);
    }

    public function test_get_template_returns_null_when_no_active_template_exists(): void
    {
        AttendanceEmailTemplate::query()->create([
            'code' => 'attendance_unknown',
            'variant' => 'default',
            'subject' => 'Inactive Default',
            'body_html' => '<p>Inactive</p>',
            'is_active' => false,
        ]);

        $result = (new AttendanceEmailTemplate())->getTemplate('attendance_unknown', 'anything');

        $this->assertNull($result);
    }
}
