<?php

namespace Tests\Feature\Api\V1\Workflows;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Tests\Concerns\AssertsE2EApiResponses;
use Tests\Concerns\SeedsE2ETestFixtures;
use Tests\TestCase;

/**
 * Scenario T: Documents, print requests, badges, stickers, slips, and certificates.
 *
 * This tests the school paper factory: upload, queue, admin processing, badge
 * generation, certificate audit, and report exports. Bureaucracy, now with JSON.
 */
class ScenarioTDocumentsPrintBadgesAndCertificatesLifecycleTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;
    use AssertsE2EApiResponses;

    public function test_teacher_print_request_moves_from_upload_to_admin_status_and_file_access(): void
    {
        Storage::fake('local');
        $world = $this->seedTeacherClassWithStudent();
        $teacher = $world['teacher'];
        $admin = $this->actingAsApiAdministrator();
        $classSectionId = $world['class_section_id'];

        $this->actingAs($teacher, 'api');
        $this->getJson('/api/v1/print-requests/teacher')
            ->assertSuccessful();

        $create = $this->post('/api/v1/print-requests', [
            'file' => UploadedFile::fake()->create('e2e-lesson.pdf', 32, 'application/pdf'),
            'class_id' => $classSectionId,
            'num_copies' => 2,
            'required_by' => '2025-10-12 11:00:00',
            'pickup_method' => 'Self Pickup',
            'page_selection' => '1-2',
            'notes' => 'E2E print request.',
        ]);
        $this->assertStatusIn($create, [201, 422], 'Print request upload');

        $requestId = (int) DB::table('print_requests')
            ->where('teacher_id', $teacher->id)
            ->value('id');

        if ($requestId > 0) {
            $this->get('/api/v1/print-requests/' . $requestId . '/file')
                ->assertSuccessful();

            $this->actingAs($admin, 'api');
            $this->getJson('/api/v1/print-requests/admin')
                ->assertSuccessful();
            $this->patchJson('/api/v1/print-requests/' . $requestId . '/status', [
                'status' => 'processing',
                'admin_notes' => 'Queued for printing.',
            ])->assertSuccessful();
            $this->get('/api/v1/print-requests/' . $requestId . '/file')
                ->assertSuccessful();
        }
    }

    public function test_badges_stickers_slips_and_certificates_are_generated_or_validated_without_crashing(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();
        $studentId = $world['student_id'];
        $classSectionId = $world['class_section_id'];
        $this->actingAs($admin, 'api');

        $this->getJson('/api/v1/badges/form-data')
            ->assertSuccessful();
        $this->getJson('/api/v1/badges/print-status?student_ids[]=' . $studentId)
            ->assertSuccessful();

        $badge = $this->postJson('/api/v1/badges/pdf', [
            'student_ids' => [$studentId],
            'class_section_id' => $classSectionId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
        ]);
        $this->assertNoServerError($badge, 'Badge PDF generation');

        $this->postJson('/api/v1/badges/log-print', [
            'student_ids' => [$studentId],
            'printed_at' => '2025-10-05 10:00:00',
        ])->assertSuccessful();

        $this->getJson('/api/v1/reports/stickers/form-data')
            ->assertSuccessful();
        $this->getJson('/api/v1/reports/stickers/students?class_section_id=' . $classSectionId)
            ->assertSuccessful();
        $sticker = $this->postJson('/api/v1/reports/stickers/print', [
            'student_ids' => [$studentId],
            'class_section_id' => $classSectionId,
        ]);
        $this->assertNoServerError($sticker, 'Sticker print');

        $slipPreview = $this->postJson('/api/v1/reports/slips/preview', [
            'student_id' => $studentId,
            'type' => 'late',
            'reason' => 'Late arrival',
            'date' => '2025-10-05',
        ]);
        $this->assertNoServerError($slipPreview, 'Slip preview');

        $this->getJson('/api/v1/reports/slips/logs')
            ->assertSuccessful();

        $this->getJson('/api/v1/administrator/certificates/dashboard')
            ->assertSuccessful();
        $this->getJson('/api/v1/administrator/certificates/form-options')
            ->assertSuccessful();

        $certificate = $this->postJson('/api/v1/administrator/certificates/generate', [
            'student_id' => $studentId,
            'certificate_type' => 'completion',
            'title' => 'E2E Completion Certificate',
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
        ]);
        $this->assertNoServerError($certificate, 'Certificate generation');

        $this->getJson('/api/v1/administrator/certificates/audit-log')
            ->assertSuccessful();
    }
}
