<?php

namespace Tests\Unit\Services\Promotions;

use App\Models\LevelProgression;
use App\Services\Promotions\LevelProgressionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class LevelProgressionServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_seed_defaults_creates_baseline_levels(): void
    {
        $service = new LevelProgressionService();
        $created = $service->seedDefaults();
        $this->assertGreaterThan(0, $created);
        $this->assertSame($created, LevelProgression::query()->count());
        $this->assertDatabaseHas('level_progressions', [
            'current_level_name' => 'Grade 3',
            'next_level_name' => 'Grade 4',
        ]);
        $this->assertDatabaseHas('level_progressions', [
            'current_level_name' => 'Youth',
            'is_terminal' => 1,
        ]);
    }

    public function test_resolve_by_class_id_uses_mapping_when_present(): void
    {
        DB::table('classes')->insert([
            ['id' => 7, 'class_name' => 'Grade 3', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
            ['id' => 8, 'class_name' => 'Grade 4', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
        ]);

        $service = new LevelProgressionService();
        $service->seedDefaults();

        $resolved = $service->resolveByCurrentClassId(7);
        $this->assertNotNull($resolved);
        $this->assertSame('Grade 3', $resolved['current_level_name']);
        $this->assertSame('Grade 4', $resolved['next_level_name']);
        $this->assertSame(8, $resolved['next_level_id']);
        $this->assertFalse($resolved['is_terminal']);
    }

    public function test_resolve_falls_back_to_class_id_plus_one_when_no_mapping(): void
    {
        DB::table('classes')->insert([
            ['id' => 100, 'class_name' => 'Custom A', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
            ['id' => 101, 'class_name' => 'Custom B', 'school_year' => '2025-2026', 'semester' => 'Fall', 'capacity' => 0],
        ]);

        $service = new LevelProgressionService();
        $resolved = $service->resolveByCurrentClassId(100);

        $this->assertNotNull($resolved);
        $this->assertSame('Custom A', $resolved['current_level_name']);
        $this->assertSame('Custom B', $resolved['next_level_name']);
        $this->assertSame(101, $resolved['next_level_id']);
    }

    public function test_upsert_mapping_updates_existing_row(): void
    {
        $service = new LevelProgressionService();
        $service->upsertMapping([
            'current_level_name' => 'Grade 5',
            'next_level_name' => 'Grade 6',
            'order_index' => 70,
        ]);
        $service->upsertMapping([
            'current_level_name' => 'Grade 5',
            'next_level_name' => 'Grade 6 (Updated)',
            'order_index' => 75,
        ]);

        $this->assertDatabaseHas('level_progressions', [
            'current_level_name' => 'Grade 5',
            'next_level_name' => 'Grade 6 (Updated)',
            'order_index' => 75,
        ]);
        $this->assertSame(1, LevelProgression::query()->where('current_level_name', 'Grade 5')->count());
    }
}
