<?php
/**
 * Payment terms resolver: compute due date + label from settings.
 *
 * Used by the XML generator (BG-19 payment terms + BT-9 due date) and by the
 * PDF template (human-readable text block).
 *
 * @package FacturX_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

class GOFACTOX_Payment_Terms {

    /**
     * Resolve payment terms for a given issue date.
     *
     * @param string $invoice_date Y-m-d formatted issue date.
     * @return array{key:string, label:string, due_date:\DateTime}
     */
    public static function resolve( string $invoice_date ): array {
        $key       = (string) get_option( 'gofactox_payment_terms', 'immediate' );
        $issue     = \DateTime::createFromFormat( 'Y-m-d', $invoice_date ) ?: new \DateTime( $invoice_date );
        $due       = clone $issue;
        $label     = '';

        // Advanced payment terms (end-of-month variants + custom text) are a Pro feature.
        // Basic options stay free.
        $is_pro = class_exists( 'GOFACTOX_License' ) && GOFACTOX_License::is_pro();
        $pro_only_keys = [ 'eom_30', 'eom_45', 'custom' ];
        if ( in_array( $key, $pro_only_keys, true ) && ! $is_pro ) {
            $key = 'net_30';
        }

        switch ( $key ) {
            case 'net_15':
                $due->modify( '+15 days' );
                $label = __( 'Net 15 days', 'gofactox-e-invoicing-for-woocommerce' );
                break;
            case 'net_30':
                $due->modify( '+30 days' );
                $label = __( 'Net 30 days', 'gofactox-e-invoicing-for-woocommerce' );
                break;
            case 'net_45':
                $due->modify( '+45 days' );
                $label = __( 'Net 45 days', 'gofactox-e-invoicing-for-woocommerce' );
                break;
            case 'net_60':
                $due->modify( '+60 days' );
                $label = __( 'Net 60 days', 'gofactox-e-invoicing-for-woocommerce' );
                break;
            case 'eom_30':
                // Code de commerce L441-10 calculation: end of the current month, then add N days.
                $due->modify( 'last day of this month' )->modify( '+30 days' );
                $label = __( '30 days end of month from invoice date', 'gofactox-e-invoicing-for-woocommerce' );
                break;
            case 'eom_45':
                $due->modify( 'last day of this month' )->modify( '+45 days' );
                $label = __( '45 days end of month from invoice date', 'gofactox-e-invoicing-for-woocommerce' );
                break;
            case 'custom':
                $label = (string) get_option( 'gofactox_payment_terms_custom', '' );
                if ( $label === '' ) {
                    $label = __( 'See payment conditions on order.', 'gofactox-e-invoicing-for-woocommerce' );
                }
                break;
            case 'immediate':
            default:
                $label = __( 'Payment due upon receipt.', 'gofactox-e-invoicing-for-woocommerce' );
                break;
        }

        return [
            'key'      => $key,
            'label'    => $label,
            'due_date' => $due,
        ];
    }
}
