<?php

namespace Tests\Unit\Services\CompetitionScores;

use App\Models\CompetitionScore;
use App\Services\CompetitionScores\CompetitionScoresSaveService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class CompetitionScoresSaveServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_filter_scores_rejects_non_integers(): void
    {
        $service = new CompetitionScoresSaveService();
        [$clean, $invalid] = $service->filterScores([
            1 => '10',
            2 => '4.5',
        ]);

        $this->assertSame([1 => 10], $clean);
        $this->assertSame([2], $invalid);
    }

    public function test_save_scores_upserts(): void
    {
        DB::table('competition_scores')->insert([
            'competition_id' => 1,
            'student_id' => 1,
            'class_section_id' => 101,
            'score' => 2,
        ]);

        $service = new CompetitionScoresSaveService();
        $service->saveScores(1, 101, [1 => 7]);

        $this->assertDatabaseHas('competition_scores', [
            'competition_id' => 1,
            'student_id' => 1,
            'class_section_id' => 101,
            'score' => 7,
        ]);
    }
}
