<?php

namespace Tests\Unit;

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

class EmailTemplateTest extends TestCase
{
    use RefreshDatabase;

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

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

        $tpl = EmailTemplate::getTemplate('attendance_absent', 'answered');

        $this->assertNotNull($tpl);
        $this->assertSame('answered', $tpl->variant);
        $this->assertSame('Parent Subject', $tpl->subject);
    }

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

        $tpl = EmailTemplate::getTemplate('attendance_late', 'answered'); // missing

        $this->assertNotNull($tpl);
        $this->assertSame('default', $tpl->variant);
        $this->assertSame('Late Default', $tpl->subject);
    }

    public function test_get_template_ignores_inactive_templates_and_can_return_null(): void
    {
        EmailTemplate::query()->create([
            'code' => 'attendance_present',
            'variant' => 'default',
            'subject' => 'Present Default',
            'body_html' => '<p>Present</p>',
            'is_active' => false, // inactive
            'updated_at' => now('UTC'),
        ]);

        $tpl = EmailTemplate::getTemplate('attendance_present', 'default');

        $this->assertNull($tpl);
    }

    public function test_get_template_prefers_exact_variant_over_default(): void
    {
        EmailTemplate::query()->create([
            'code' => 'attendance_notice',
            'variant' => 'default',
            'subject' => 'Default Notice',
            'body_html' => '<p>Default</p>',
            'is_active' => true,
            'updated_at' => now('UTC'),
        ]);

        EmailTemplate::query()->create([
            'code' => 'attendance_notice',
            'variant' => 'no_answer',
            'subject' => 'Short Notice',
            'body_html' => '<p>Short</p>',
            'is_active' => true,
            'updated_at' => now('UTC'),
        ]);

        $tpl = EmailTemplate::getTemplate('attendance_notice', 'no_answer');

        $this->assertNotNull($tpl);
        $this->assertSame('no_answer', $tpl->variant);
        $this->assertSame('Short Notice', $tpl->subject);
    }
}
