<?php

declare(strict_types=1);

namespace App\Tests\Module\Technique\Api;

/**
 * Tests fonctionnels des RG comptables inter-champs portees par les Assert\Callback
 * de l'entite Provider (M3, RG-3.07 / RG-3.08), via le PATCH de l'onglet
 * Comptabilite (groupe provider:write:accounting). On asserte le code HTTP et le
 * propertyPath de la violation (consommable par extractApiViolations cote front,
 * ERP-101). Jumeau de SupplierAccountingApiTest (M2), completude de l'onglet
 * INCLUSE : a la validation complete de l'onglet, les six scalaires comptables
 * sont obligatoires (spec-front M3 § Onglet Comptabilite — aligne M1/M2).
 *
 * @internal
 */
final class ProviderAccountingValidationTest extends AbstractProviderApiTestCase
{
    // === RG-3.07 : Virement impose une banque ===

    public function testVirementWithoutBankReturns422OnBankPath(): void
    {
        $client = $this->createAdminClient();
        $seed   = $this->seedProvider('Virement No Bank');

        $response = $client->request('PATCH', '/api/providers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE, 'Accept' => self::LD],
            'json'    => ['paymentType' => '/api/payment_types/'.$this->paymentType('VIREMENT')->getId()],
        ]);

        self::assertResponseStatusCodeSame(422);
        self::assertArrayHasKey('bank', $this->violationsByPath($response->toArray(false)));
    }

    public function testVirementWithBankReturns200(): void
    {
        $client = $this->createAdminClient();
        $seed   = $this->seedProvider('Virement With Bank');

        $client->request('PATCH', '/api/providers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE],
            'json'    => [
                'paymentType' => '/api/payment_types/'.$this->paymentType('VIREMENT')->getId(),
                'bank'        => '/api/banks/'.$this->bank('SG')->getId(),
            ],
        ]);

        self::assertResponseStatusCodeSame(200);
    }

    // === RG-3.08 : LCR impose au moins un RIB (volet ecriture du formulaire) ===

    public function testLcrWithoutRibReturns422OnPaymentTypePath(): void
    {
        $client = $this->createAdminClient();
        $seed   = $this->seedProvider('Lcr No Rib');

        $response = $client->request('PATCH', '/api/providers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE, 'Accept' => self::LD],
            'json'    => ['paymentType' => '/api/payment_types/'.$this->paymentType('LCR')->getId()],
        ]);

        self::assertResponseStatusCodeSame(422);
        // Miroir client : violation portee sur `paymentType` (select « Type de
        // règlement »), les RIB n'ayant pas de champ de formulaire pour l'ancrer.
        self::assertArrayHasKey('paymentType', $this->violationsByPath($response->toArray(false)));
    }

    public function testLcrWithRibReturns200(): void
    {
        $client = $this->createAdminClient();
        $seed   = $this->seedProvider('Lcr With Rib');
        $this->addRib($seed);

        $client->request('PATCH', '/api/providers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE],
            'json'    => ['paymentType' => '/api/payment_types/'.$this->paymentType('LCR')->getId()],
        ]);

        self::assertResponseStatusCodeSame(200);
    }

    // === Completude de l'onglet Comptabilite (six scalaires obligatoires) ===

    /**
     * spec-front M3 § Onglet Comptabilite : a la validation COMPLETE de l'onglet
     * (les six champs requis presents dans le payload), chacun vide doit renvoyer
     * une 422 sur son propre propertyPath (mapping inline front, ERP-101). Miroir
     * M1/M2 (ProviderAccountingCompletenessValidator).
     */
    public function testIncompleteAccountingTabReturns422OnEachField(): void
    {
        $client = $this->createAdminClient();
        $seed   = $this->seedProvider('Accounting Incomplete');

        $response = $client->request('PATCH', '/api/providers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE, 'Accept' => self::LD],
            'json'    => [
                'siren'         => null,
                'accountNumber' => null,
                'tvaMode'       => null,
                'nTva'          => null,
                'paymentDelay'  => null,
                'paymentType'   => null,
            ],
        ]);

        self::assertResponseStatusCodeSame(422);
        $paths = $this->violationsByPath($response->toArray(false));
        self::assertArrayHasKey('siren', $paths);
        self::assertArrayHasKey('accountNumber', $paths);
        self::assertArrayHasKey('tvaMode', $paths);
        self::assertArrayHasKey('nTva', $paths);
        self::assertArrayHasKey('paymentDelay', $paths);
        self::assertArrayHasKey('paymentType', $paths);
    }

    /**
     * Un PATCH ciblant un sous-ensemble de champs comptables n'est PAS une
     * validation d'onglet : la completude ne se declenche pas (edition ponctuelle
     * preservee, cf. validateAccountingCompleteness).
     */
    public function testPartialAccountingPatchSkipsCompleteness(): void
    {
        $client = $this->createAdminClient();
        $seed   = $this->seedProvider('Accounting Partial');

        $client->request('PATCH', '/api/providers/'.$seed->getId(), [
            'headers' => ['Content-Type' => self::MERGE],
            'json'    => ['nTva' => 'FR12345678901'],
        ]);

        self::assertResponseStatusCodeSame(200);
    }

    // violationsByPath() : helper mutualise dans AbstractProviderApiTestCase.
}
