<?php

namespace Tests\Unit\Services\ClassPreparation;

use App\Services\ClassPreparation\ClassPreparationAdjustmentService;
use App\Services\ClassPreparation\ClassPreparationCalculatorService;
use App\Services\ClassPreparation\ClassPreparationContextService;
use App\Services\ClassPreparation\ClassPreparationLogService;
use App\Services\ClassPreparation\ClassPreparationPrintService;
use App\Services\ClassPreparation\ClassPreparationRosterService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class ClassPreparationPrintServiceTest extends TestCase
{
    use RefreshDatabase;

    protected function tearDown(): void
    {
        Mockery::close();
        parent::tearDown();
    }

    public function test_print_prep_creates_log(): void
    {
        $this->seedPrepData();

        $service = app(ClassPreparationPrintService::class);
        $payload = $service->printPrep('101', '2025-2026', 'Fall');

        $this->assertTrue($payload['ok']);
        $this->assertDatabaseHas('class_preparation_log', [
            'class_section_id' => 101,
            'school_year' => '2025-2026',
        ]);
    }

    public function test_print_prep_handles_log_failure(): void
    {
        $context = Mockery::mock(ClassPreparationContextService::class);
        $roster = Mockery::mock(ClassPreparationRosterService::class);
        $calculator = Mockery::mock(ClassPreparationCalculatorService::class);
        $adjustments = Mockery::mock(ClassPreparationAdjustmentService::class);
        $logs = Mockery::mock(ClassPreparationLogService::class);

        $context->shouldReceive('getSemester')->andReturn('Fall');
        $context->shouldReceive('hasRosterForSemester')->andReturn(true);
        $roster->shouldReceive('getStudentCountForSection')->andReturn(10);
        $calculator->shouldReceive('getClassLevelBySection')->andReturn(2);
        $calculator->shouldReceive('calculatePrepItems')->andReturn(['Small Table' => 2]);
        $adjustments->shouldReceive('applyAdjustments')->andReturn([['Small Table' => 2], []]);
        $logs->shouldReceive('createLog')->andReturn(false);

        $service = new ClassPreparationPrintService($context, $roster, $calculator, $adjustments, $logs);
        $payload = $service->printPrep('101', '2025-2026', 'Fall');

        $this->assertFalse($payload['ok']);
        $this->assertNull($payload['printed_at']);
    }

    private function seedPrepData(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        DB::table('classSection')->insert([
            'id' => 1,
            'class_id' => 1,
            'class_section_id' => 101,
            'class_section_name' => '1-A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('students')->insert([
            'school_id' => 'S-1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 8,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_active' => 1,
        ]);

        DB::table('student_class')->insert([
            'student_id' => 1,
            'class_section_id' => 101,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('inventory_categories')->insert([
            'type' => 'classroom',
            'name' => 'Small Table',
        ]);
    }
}
