<?php

namespace Tests\Unit\Services\Subjects;

use App\Services\Subjects\SubjectCurriculumService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class SubjectCurriculumServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_store_creates_entry(): void
    {
        $classId = $this->seedClass();
        $service = new SubjectCurriculumService();

        $entry = $service->store([
            'class_id' => $classId,
            'subject' => 'islamic',
            'unit_number' => 1,
            'unit_title' => 'Faith',
            'chapter_name' => 'Intro',
        ]);

        $this->assertNotNull($entry->id);
        $this->assertDatabaseHas('subject_curriculum_items', [
            'id' => $entry->id,
            'class_id' => $classId,
        ]);
    }

    public function test_update_changes_title(): void
    {
        $classId = $this->seedClass();
        $entryId = DB::table('subject_curriculum_items')->insertGetId([
            'class_id' => $classId,
            'subject' => 'quran',
            'unit_number' => 2,
            'unit_title' => 'Old',
            'chapter_name' => 'Chapter 1',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new SubjectCurriculumService();
        $entry = $service->update($entryId, [
            'class_id' => $classId,
            'subject' => 'quran',
            'unit_number' => 2,
            'unit_title' => 'New',
            'chapter_name' => 'Chapter 1',
        ]);

        $this->assertNotNull($entry);
        $this->assertDatabaseHas('subject_curriculum_items', [
            'id' => $entryId,
            'unit_title' => 'New',
        ]);
    }

    private function seedClass(): int
    {
        return DB::table('classes')->insertGetId([
            'class_name' => 'Grade 1',
            'schedule' => 'Sunday',
            'capacity' => 20,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }
}
