<?php

declare (strict_types = 1);
namespace Brightfern\Transmission;

use Brightfern\Support\Http\HttpClient;
// Send-side types are premium-only and referenced by fully-qualified name
// inside the __premium_only methods below. Deliberately no `use` imports:
// those would survive into the free build as dangling references to a
// stripped path. HttpClient is a general support utility, so importing it
// is safe.
/**
 * Direct Access Point for Poland's KSeF, talking to the Ministry of Finance's
 * own REST API (https://api-test.ksef.mf.gov.pl) rather than a paid
 * intermediary like Storecove's V-flow — submitting to KSeF itself is free,
 * so this exists as a no-recurring-cost alternative for merchants who don't
 * want a commercial Access Point's platform fee just for Poland.
 *
 * Deliberately a thin shell: the whole session flow, and everything that
 * touches cryptography, lives in {@see \Brightfern\Transmission\Send\KsefClient}
 * so that it disappears together with the rest of Send/ from the free build.
 * This class ships in every build (ProviderFactory builds it, and that
 * factory is on the free receive path), so its only premium references must
 * sit inside the two __premium_only methods Freemius strips — the same shape
 * StorecoveProvider has. See docs/RELEASING.md §3.
 *
 * Proven end to end against the real KSeF test environment: a synthetic test
 * NIP, a real FA(3) document, and a real Ministry-assigned KSeF number back.
 * See docs/DECISIONS.md.
 */
final class KsefProvider implements AccessPointProvider {
    public function __construct(
        private HttpClient $http,
        private string $token,
        private string $sellerNip,
        private string $baseUrl = 'https://api-test.ksef.mf.gov.pl/api/v2',
        // Test seam only: production call sites never pass these. Real
        // polling needs real delay (500ms/1s); tests inject 0 so a "never
        // resolves" case doesn't cost several real seconds per run.
        private int $authPollDelayUs = 500000,
        private int $invoicePollDelayUs = 1000000
    ) {
    }

    public function id() : string {
        return 'ksef-direct';
    }

    public function label() : string {
        return 'Poland KSeF (direct, no intermediary)';
    }

    public function supportsReceiving() : bool {
        // KSeF also lets a taxpayer retrieve invoices issued TO them, but
        // that is a genuinely separate feature (a query/retrieval API, not
        // a webhook/poll shape like Storecove's) — deliberately not built in
        // this pass. Not a silent gap: see docs/DECISIONS.md.
        return false;
    }

    public function handleIncoming( array $payload ) : ?InboundInvoice {
        return null;
    }

    public function poll() : array {
        return array();
    }

}
