<?php

namespace Tests\Feature\Api\Administrator;

use App\Models\User;
use App\Services\Administrator\TeacherSubmissionNotificationService;
use App\Services\Administrator\TeacherSubmissionReportService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Mockery;
use Tests\TestCase;

class AdministratorTeacherSubmissionControllerTest extends TestCase
{
    use RefreshDatabase;

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

    public function test_can_get_teacher_submission_report(): void
    {
        $user = User::factory()->create();
        Sanctum::actingAs($user);

        $mock = Mockery::mock(TeacherSubmissionReportService::class);
        $mock->shouldReceive('report')
            ->once()
            ->andReturn([
                'rows' => [],
                'semester' => 'Fall',
                'schoolYear' => '2025-2026',
                'notificationHistory' => [],
                'summary' => [
                    'total_items' => 0,
                    'missing_items' => 0,
                    'submitted_items' => 0,
                    'submission_percentage' => 100,
                ],
            ]);

        $this->app->instance(TeacherSubmissionReportService::class, $mock);

        $response = $this->getJson('/api/v1/administrator/teacher-submissions');

        $response->assertOk()
            ->assertJson([
                'rows' => [],
                'semester' => 'Fall',
                'schoolYear' => '2025-2026',
            ]);
    }

    public function test_notify_requires_notify_payload(): void
    {
        $user = User::factory()->create();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/administrator/teacher-submissions/notify', [
            'missing_items' => [],
        ]);

        $response->assertStatus(422)
            ->assertJsonValidationErrors(['notify']);
    }

    public function test_notify_calls_service(): void
    {
        $user = User::factory()->create();
        Sanctum::actingAs($user);

        $payload = [
            'notify' => [
                1 => [
                    5 => 1,
                ],
            ],
            'missing_items' => [
                1 => [
                    5 => base64_encode(json_encode(['midterm scores'])),
                ],
            ],
        ];

        $mock = Mockery::mock(TeacherSubmissionNotificationService::class);
        $mock->shouldReceive('send')
            ->once()
            ->andReturn([
                'message' => '1 reminder sent',
                'sent' => 1,
                'failed' => 0,
                'status' => 200,
            ]);

        $this->app->instance(TeacherSubmissionNotificationService::class, $mock);

        $response = $this->postJson('/api/v1/administrator/teacher-submissions/notify', $payload);

        $response->assertOk()
            ->assertJson([
                'message' => '1 reminder sent',
                'sent' => 1,
                'failed' => 0,
            ]);
    }
}