<?php

declare(strict_types=1);

namespace App\Tests\Module\Commercial\Api;

/**
 * Validation back-autoritative du FORMAT de la date de creation (foundedAt,
 * onglet Information) du fournisseur. Miroir de {@see ClientFoundedAtFormatTest}.
 *
 * Une date non parsable (saisie brute forwardee par MalioDate, MUI-44) doit
 * produire un 422 porte sur `foundedAt` (mappable inline par useFormErrors), et
 * non un 400 generique. Repose sur `collectDenormalizationErrors` sur les
 * operations write du Supplier.
 *
 * @internal
 */
final class SupplierFoundedAtFormatTest extends AbstractSupplierApiTestCase
{
    /** Date non parsable -> 422 porte sur foundedAt (et pas un 400 generique). */
    public function testFoundedAtNonParsableEst422(): void
    {
        $seed        = $this->seedSupplier('Founded Format Negoce');
        $credentials = $this->createUserWithPermission('commercial.suppliers.manage');
        $client      = $this->authenticatedClient($credentials['username'], $credentials['password']);

        $body = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE],
            'json'    => ['foundedAt' => '32/13/2026'],
        ])->toArray(false);

        self::assertResponseStatusCodeSame(422);
        self::assertArrayHasKey('foundedAt', $this->violationsByPath($body));
    }

    /**
     * Cas piege : « 12/25/2026 » est invalide cote front (JJ/MM/AAAA -> mois 25)
     * mais PHP DateTime l'accepterait en M/J/AAAA. Le format d'entree strict ISO
     * `Y-m-d` (Context sur foundedAt) doit le rejeter -> 422.
     */
    public function testFoundedAtFormatAmbiguUsEst422(): void
    {
        $seed        = $this->seedSupplier('Founded Ambigu Negoce');
        $credentials = $this->createUserWithPermission('commercial.suppliers.manage');
        $client      = $this->authenticatedClient($credentials['username'], $credentials['password']);

        $body = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE],
            'json'    => ['foundedAt' => '12/25/2026'],
        ])->toArray(false);

        self::assertResponseStatusCodeSame(422);
        self::assertArrayHasKey('foundedAt', $this->violationsByPath($body));
    }

    /** Non-regression : une date ISO valide reste acceptee (200). */
    public function testFoundedAtIsoValideEst200(): void
    {
        $seed        = $this->seedSupplier('Founded Ok Negoce');
        $credentials = $this->createUserWithPermission('commercial.suppliers.manage');
        $client      = $this->authenticatedClient($credentials['username'], $credentials['password']);

        $data = $client->request('PATCH', '/api/suppliers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE],
            'json'    => ['foundedAt' => '2010-05-01'],
        ])->toArray();

        self::assertResponseStatusCodeSame(200);
        self::assertStringStartsWith('2010-05-01', $data['foundedAt']);
    }
}
