<?php

namespace Tests\Unit\Services\Reports\ReportCards;

use App\Services\Reports\ReportCards\ReportCardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ReportCardServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_meta_returns_data_sets(): void
    {
        $this->seedConfig();
        $this->seedReportCardData();

        $service = new ReportCardService();
        $result = $service->meta('2025-2026', 'Fall');

        $this->assertNotEmpty($result['schoolYears']);
        $this->assertSame('2025-2026', $result['selectedYear']);
        $this->assertNotEmpty($result['students']);
        $this->assertNotEmpty($result['classSections']);
    }

    public function test_completeness_returns_summary(): void
    {
        $this->seedConfig();
        $data = $this->seedReportCardData();

        $service = new ReportCardService();
        $result = $service->completeness('2025-2026', 'Fall', $data['class_section_id']);

        $this->assertTrue($result['ok']);
        $this->assertSame(1, $result['summary']['total']);
        $this->assertCount(1, $result['students']);
    }

    public function test_acknowledgement_returns_signed_data(): void
    {
        $this->seedConfig();
        $data = $this->seedReportCardData();
        $this->seedAcknowledgement($data['student_id']);

        $service = new ReportCardService();
        $result = $service->acknowledgement($data['student_id'], '2025-2026', 'Fall');

        $this->assertSame('Parent One', $result['signed_name']);
    }

    public function test_generate_single_report_returns_pdf(): void
    {
        $this->seedConfig();
        $data = $this->seedReportCardData();

        $service = new ReportCardService();
        $result = $service->generateSingleReport($data['student_id'], [
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertNotEmpty($result['pdf']);
        $this->assertStringContainsString('ReportCard_', $result['filename']);
    }

    public function test_generate_single_report_handles_missing_scores(): void
    {
        $this->seedConfig();
        $studentId = $this->seedStudentOnly();

        $service = new ReportCardService();
        $result = $service->generateSingleReport($studentId, [
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $this->assertFalse($result['ok']);
    }

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

    private function seedReportCardData(): array
    {
        DB::table('classes')->insert([
            'id' => 1,
            'class_name' => 'Grade 1',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 200,
            'class_section_name' => 'Grade 1',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

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

        DB::table('student_class')->insert([
            'student_id' => 100,
            'class_section_id' => 200,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('semester_scores')->insert([
            'student_id' => 100,
            'school_id' => 'SCH1',
            'class_section_id' => 200,
            'homework_avg' => 90,
            'quiz_avg' => 88,
            'project_avg' => 92,
            'test_avg' => 89,
            'midterm_exam_score' => 91,
            'final_exam_score' => 93,
            'attendance_score' => 95,
            'ptap_score' => 90,
            'semester_score' => 91,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return [
            'student_id' => 100,
            'class_section_id' => 200,
        ];
    }

    private function seedStudentOnly(): int
    {
        DB::table('classes')->insert([
            'id' => 2,
            'class_name' => 'Grade 2',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 201,
            'class_section_name' => 'Grade 2',
            'class_id' => 2,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('students')->insert([
            'id' => 101,
            'school_id' => 'SCH2',
            'firstname' => 'Student',
            'lastname' => 'Two',
            'age' => 11,
            'gender' => 'Female',
            'photo_consent' => 1,
            'parent_id' => 11,
            'year_of_registration' => '2025-2026',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_active' => 1,
        ]);

        DB::table('student_class')->insert([
            'student_id' => 101,
            'class_section_id' => 201,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return 101;
    }

    private function seedAcknowledgement(int $studentId): void
    {
        DB::table('report_card_acknowledgements')->insert([
            'parent_id' => 10,
            'student_id' => $studentId,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'viewed_at' => now(),
            'signed_at' => now(),
            'signed_name' => 'Parent One',
            'signer_ip' => '127.0.0.1',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
