<?php

declare(strict_types=1);

use App\CreatorsApi\Model\ReportMetadata;

test('fromListResponse mappa la risposta grezza in DTO', function () {
    $response = [
        'reports' => [
            ['filename' => 'blazemedia-category-report-20260730.tsv.gz', 'md5' => 'abc', 'size' => 2048, 'lastModified' => '2026-07-30T03:00:00Z'],
        ],
    ];

    $reports = ReportMetadata::fromListResponse($response);

    expect($reports)->toHaveCount(1);
    expect($reports[0])->toBeInstanceOf(ReportMetadata::class);
    expect($reports[0]->filename)->toBe('blazemedia-category-report-20260730.tsv.gz');
    expect($reports[0]->size)->toBe(2048.0);
})->group('unit');

test('family riconosce la famiglia piu specifica prima della generica', function () {
    $trackingId = new ReportMetadata(['filename' => 'blazemedia-tracking_id-report-20260730.tsv.gz']);
    $tracking = new ReportMetadata(['filename' => 'blazemedia-tracking-report-20260730.tsv.gz']);
    $linkedTracking = new ReportMetadata(['filename' => 'blazemedia-linked_product_tracking-report-20260730.tsv.gz']);

    // tracking_id NON deve essere classificato come tracking
    expect($trackingId->family())->toBe('tracking_id');
    expect($tracking->family())->toBe('tracking');
    // linked_product_tracking NON deve degradare a linked_product
    expect($linkedTracking->family())->toBe('linked_product_tracking');
})->group('unit');

test('reportDate estrae la data dal suffisso del nome file', function () {
    $report = new ReportMetadata(['filename' => 'blazemedia-earnings-report-20260730.tsv.gz']);

    expect($report->reportDate())->toBe('2026-07-30');
})->group('unit');

test('reportDate e family sono null su un nome file non conforme', function () {
    $report = new ReportMetadata(['filename' => 'qualcosa-di-strano.txt']);

    expect($report->reportDate())->toBeNull();
    expect($report->family())->toBeNull();
})->group('unit');

test('toArray espone campi grezzi e derivati', function () {
    $report = new ReportMetadata([
        'filename' => 'blazemedia-subtag-report-20260730.tsv.gz',
        'md5' => 'xyz',
        'size' => 4096,
        'lastModified' => '2026-07-30T03:00:00Z',
    ]);

    expect($report->toArray())->toBe([
        'filename' => 'blazemedia-subtag-report-20260730.tsv.gz',
        'md5' => 'xyz',
        'size' => 4096.0,
        'lastModified' => '2026-07-30T03:00:00Z',
        'family' => 'subtag',
        'reportDate' => '2026-07-30',
    ]);
})->group('unit');
