<?php

namespace Tests\Unit\Services\Students;

use App\Services\Students\StudentScoreCardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class StudentScoreCardServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_score_card_returns_rows_and_comments(): void
    {
        $studentId = $this->seedStudent();
        $this->seedClassSection();

        DB::table('semester_scores')->insert([
            'student_id' => $studentId,
            'class_section_id' => 601,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'semester_score' => 88,
        ]);

        DB::table('score_comments')->insert([
            'student_id' => $studentId,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'score_type' => 'midterm',
            'comment_review' => 'Great effort',
            'created_at' => now(),
        ]);

        $service = new StudentScoreCardService();
        $result = $service->scoreCard($studentId);

        $this->assertTrue($result['ok']);
        $this->assertSame('S-800', $result['student']['school_id']);
        $this->assertSame('Great effort', $result['rows'][0]['comments']['midterm']);
    }

    private function seedStudent(): int
    {
        return DB::table('students')->insertGetId([
            'school_id' => 'S-800',
            'firstname' => 'Zane',
            'lastname' => 'Student',
            'dob' => '2014-09-01',
            'age' => 11,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);
    }

    private function seedClassSection(): void
    {
        DB::table('classSection')->insert([
            'class_id' => 1,
            'class_section_id' => 601,
            'class_section_name' => '6A',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }
}
