<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Application\Service;

use App\Administration\Application\Query\GetStudentsImageRights\StudentImageRightsDto;
use App\Administration\Application\Service\ImageRightsExporter;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class ImageRightsExporterTest extends TestCase
{
    private ImageRightsExporter $exporter;

    protected function setUp(): void
    {
        $this->exporter = new ImageRightsExporter();
    }

    #[Test]
    public function startsWithUtf8Bom(): void
    {
        $csv = $this->exporter->export([]);

        self::assertStringStartsWith("\xEF\xBB\xBF", $csv);
    }

    #[Test]
    public function exportsHeaderRow(): void
    {
        $csv = $this->exporter->export([]);

        self::assertStringContainsString('Nom;Prénom;Classe;Statut', $csv);
    }

    #[Test]
    public function exportsStudentData(): void
    {
        $students = [
            new StudentImageRightsDto(
                id: 'id-1',
                firstName: 'Alice',
                lastName: 'Dupont',
                email: 'alice@example.com',
                imageRightsStatus: 'authorized',
                imageRightsStatusLabel: 'Autorisé',
                imageRightsUpdatedAt: new DateTimeImmutable(),
                className: '6ème A',
            ),
            new StudentImageRightsDto(
                id: 'id-2',
                firstName: 'Bob',
                lastName: 'Martin',
                email: 'bob@example.com',
                imageRightsStatus: 'refused',
                imageRightsStatusLabel: 'Refusé',
                imageRightsUpdatedAt: null,
                className: '5ème B',
            ),
        ];

        $csv = $this->exporter->export($students);

        self::assertStringContainsString('Dupont;Alice;"6ème A";Autorisé', $csv);
        self::assertStringContainsString('Martin;Bob;"5ème B";Refusé', $csv);
    }

    #[Test]
    public function handlesNullClassName(): void
    {
        $students = [
            new StudentImageRightsDto(
                id: 'id-1',
                firstName: 'Alice',
                lastName: 'Dupont',
                email: 'alice@example.com',
                imageRightsStatus: 'not_specified',
                imageRightsStatusLabel: 'Non renseigné',
                imageRightsUpdatedAt: null,
                className: null,
            ),
        ];

        $csv = $this->exporter->export($students);

        self::assertStringContainsString('Dupont;Alice;;"Non renseigné"', $csv);
    }

    #[Test]
    public function exportContainsCorrectNumberOfLines(): void
    {
        $students = [
            new StudentImageRightsDto(
                id: 'id-1',
                firstName: 'Alice',
                lastName: 'Dupont',
                email: 'alice@example.com',
                imageRightsStatus: 'authorized',
                imageRightsStatusLabel: 'Autorisé',
                imageRightsUpdatedAt: null,
                className: '6ème A',
            ),
        ];

        $csv = $this->exporter->export($students);
        $lines = array_filter(explode("\n", trim($csv)));

        // 1 header + 1 data row
        self::assertCount(2, $lines);
    }
}
