<?php
/**
 * Abstract PDP (Plateforme de Dématérialisation Partenaire) connector.
 *
 * Defines the interface for transmitting invoices to PDP/PPF.
 * Concrete implementations will handle specific platforms:
 * - Chorus Pro (PPF, B2G)
 * - Pennylane
 * - Other PDP when APIs are available
 *
 * @package FacturX_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

abstract class GOFACTOX_PDP_Connector {

    /**
     * Unique identifier for this connector (e.g., 'chorus_pro', 'pennylane').
     */
    abstract public function get_id(): string;

    /**
     * Human-readable name (e.g., 'Chorus Pro', 'Pennylane').
     */
    abstract public function get_name(): string;

    /**
     * Whether this connector is configured and ready to use.
     */
    abstract public function is_configured(): bool;

    /**
     * Transmit an invoice to the PDP.
     *
     * @param array  $invoice_data Invoice data from GOFACTOX_Invoice::get_invoice_data().
     * @param string $xml          The Factur-X/CII/UBL XML content.
     * @param string $pdf_path     Path to the PDF/A-3 file (if applicable).
     * @return GOFACTOX_Transmission_Result
     */
    abstract public function transmit( array $invoice_data, string $xml, string $pdf_path ): GOFACTOX_Transmission_Result;

    /**
     * Check the transmission status of a previously sent invoice.
     *
     * @param string $transmission_id The ID returned by transmit().
     * @return GOFACTOX_Transmission_Result
     */
    abstract public function check_status( string $transmission_id ): GOFACTOX_Transmission_Result;

    /**
     * Get the settings fields for this connector (for the admin UI).
     *
     * @return array WooCommerce settings fields.
     */
    abstract public function get_settings_fields(): array;
}

/**
 * Transmission result object.
 */
class GOFACTOX_Transmission_Result {

    public const STATUS_PENDING   = 'pending';
    public const STATUS_SENT      = 'sent';
    public const STATUS_ACCEPTED  = 'accepted';
    public const STATUS_REJECTED  = 'rejected';
    public const STATUS_ERROR     = 'error';

    public function __construct(
        public readonly string $status,
        public readonly string $transmission_id = '',
        public readonly string $message = '',
        public readonly array  $raw_response = [],
    ) {}

    public function is_success(): bool {
        return in_array( $this->status, [ self::STATUS_SENT, self::STATUS_ACCEPTED ], true );
    }
}
