<?php

namespace Tests\Unit\Services\AttendanceTracking;

use App\Services\AttendanceTracking\AttendanceEmailComposerService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class AttendanceEmailComposerServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_render_template_replaces_tokens(): void
    {
        DB::table('email_templates')->insert([
            'code' => 'ABS_1',
            'variant' => 'default',
            'subject' => 'Notice for {{student_name}}',
            'body_html' => '<p>{{parent_name}}</p>',
            'is_active' => 1,
        ]);

        $service = new AttendanceEmailComposerService(new \App\Models\AttendanceEmailTemplate());
        $context = ['{{student_name}}' => 'Student A', '{{parent_name}}' => 'Parent A'];
        $rendered = $service->renderTemplate('ABS_1', 'default', $context);

        $this->assertSame('Notice for Student A', $rendered[0]);
        $this->assertSame('<p>Parent A</p>', $rendered[1]);
    }

    public function test_pick_variant_fallback(): void
    {
        $service = new AttendanceEmailComposerService(new \App\Models\AttendanceEmailTemplate());
        $this->assertSame('no_answer', $service->pickVariant('ABS_2'));
        $this->assertSame('default', $service->pickVariant('ABS_1'));
    }
}
