<?php
/**
 * Chorus Pro connector: PPF (Portail Public de Facturation).
 *
 * Chorus Pro is the French government's e-invoicing platform.
 * Currently used for B2G (Business to Government) invoicing.
 * Will become the central platform for B2B e-invoicing starting Sept 2026.
 *
 * API documentation: https://communaute.chorus-pro.gouv.fr/documentation/
 *
 * Status: SCAFFOLD. API endpoints are defined but the B2B API
 * is not yet finalized by the AIFE. This connector will be
 * completed when the API specifications are published.
 *
 * @package FacturX_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

class GOFACTOX_Chorus_Pro_Connector extends GOFACTOX_PDP_Connector {

    private const API_SANDBOX = 'https://sandbox-api.chorus-pro.gouv.fr';
    private const API_PROD    = 'https://chorus-pro.gouv.fr/api';

    public function get_id(): string {
        return 'chorus_pro';
    }

    public function get_name(): string {
        return 'Chorus Pro (PPF)';
    }

    public function is_configured(): bool {
        $client_id     = get_option( 'gofactox_chorus_client_id', '' );
        $client_secret = get_option( 'gofactox_chorus_client_secret', '' );
        $siret         = get_option( 'gofactox_siret', '' );

        return ! empty( $client_id ) && ! empty( $client_secret ) && ! empty( $siret );
    }

    public function transmit( array $invoice_data, string $xml, string $pdf_path ): GOFACTOX_Transmission_Result {
        if ( ! $this->is_configured() ) {
            return new GOFACTOX_Transmission_Result(
                GOFACTOX_Transmission_Result::STATUS_ERROR,
                '',
                __( 'Chorus Pro is not configured. Please enter your API credentials.', 'gofactox-e-invoicing-for-woocommerce' )
            );
        }

        // TODO: Implement when AIFE publishes the B2B API.
        // Current B2G flow (for reference):
        // 1. Authenticate via OAuth2 (client_credentials)
        // 2. POST /cpro/factures/v1/soumettre with Factur-X PDF
        // 3. Receive idFlux (transmission ID)
        // 4. Poll /cpro/factures/v1/consulter/{idFlux} for status

        return new GOFACTOX_Transmission_Result(
            GOFACTOX_Transmission_Result::STATUS_ERROR,
            '',
            __( 'Chorus Pro B2B transmission is not yet available. The API will be activated by the AIFE before September 2026.', 'gofactox-e-invoicing-for-woocommerce' )
        );
    }

    public function check_status( string $transmission_id ): GOFACTOX_Transmission_Result {
        // TODO: Implement status check.
        return new GOFACTOX_Transmission_Result(
            GOFACTOX_Transmission_Result::STATUS_PENDING,
            $transmission_id,
            __( 'Status check not yet implemented.', 'gofactox-e-invoicing-for-woocommerce' )
        );
    }

    public function get_settings_fields(): array {
        return [
            [
                'title' => 'Chorus Pro (PPF)',
                'type'  => 'title',
                'desc'  => __( 'Connect to the French government e-invoicing platform. You need an account on chorus-pro.gouv.fr.', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'    => 'gofactox_chorus_section',
            ],
            [
                'title'   => __( 'Environment', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'      => 'gofactox_chorus_environment',
                'type'    => 'select',
                'options' => [
                    'sandbox'    => __( 'Sandbox (test)', 'gofactox-e-invoicing-for-woocommerce' ),
                    'production' => __( 'Production', 'gofactox-e-invoicing-for-woocommerce' ),
                ],
                'default' => 'sandbox',
            ],
            [
                'title'       => __( 'Client ID', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_chorus_client_id',
                'type'        => 'text',
                'desc_tip'    => __( 'OAuth2 Client ID from your Chorus Pro technical account.', 'gofactox-e-invoicing-for-woocommerce' ),
                'css'         => 'min-width: 400px;',
            ],
            [
                'title'       => __( 'Client Secret', 'gofactox-e-invoicing-for-woocommerce' ),
                'id'          => 'gofactox_chorus_client_secret',
                'type'        => 'password',
                'desc_tip'    => __( 'OAuth2 Client Secret. Stored encrypted.', 'gofactox-e-invoicing-for-woocommerce' ),
                'css'         => 'min-width: 400px;',
            ],
            [
                'type' => 'sectionend',
                'id'   => 'gofactox_chorus_section',
            ],
        ];
    }

    /**
     * Get API base URL based on environment setting.
     */
    private function get_api_url(): string {
        $env = get_option( 'gofactox_chorus_environment', 'sandbox' );
        return $env === 'production' ? self::API_PROD : self::API_SANDBOX;
    }
}

// Register the connector.
add_action( 'gofactox_register_pdp_connectors', function () {
    GOFACTOX_PDP_Manager::register( new GOFACTOX_Chorus_Pro_Connector() );
} );
