<?php
/**
 * Tax Module — bootstrapper.
 *
 * Responsibilities:
 *   1. Hook into `titancart_checkout_tax_total` filter so CheckoutManager
 *      receives the real tax amount calculated from classes + zones + rates.
 *   2. Register REST routes (TaxController).
 *   3. Seed a default tax class on first activation.
 *
 * Filter signature (from CheckoutManager.php):
 *   apply_filters('titancart_checkout_tax_total', 0.0, $cart, $shipping_address, $billing_address)
 *
 * @package TitanCart\Modules\Tax
 */

namespace TitanCart\Modules\Tax;

use TitanCart\REST\Tax\TaxController;

defined( 'ABSPATH' ) || exit;

class TaxModule {

    /**
     * Initialise the Tax module.
     * Called from Plugin::boot() before `titancart_loaded` fires.
     */
    public static function init(): void {
        $instance = new self();

        // 1. Inject the real tax total into checkout totals.
        //    Signature: apply_filters('titancart_checkout_tax_total',
        //                            0.0, $cart, $shipping_address, $billing_address)
        add_filter( 'titancart_checkout_tax_total', [ $instance, 'calculate_tax_total' ], 10, 4 );

        // 2. REST routes.
        add_action( 'rest_api_init', [ $instance, 'register_routes' ] );

        // 3. Seed defaults once on first activation.
        add_action( 'titancart_loaded', [ $instance, 'maybe_seed_defaults' ] );
    }

    // ══════════════════════════════════════════════════════════════════════════
    // CHECKOUT TAX FILTER
    // ══════════════════════════════════════════════════════════════════════════

    /**
     * Filter: `titancart_checkout_tax_total`
     *
     * @param  float  $default          Always 0.0.
     * @param  array  $cart             Full cart array from CartManager.
     * @param  array  $shipping_address Shipping address from checkout.
     * @param  array  $billing_address  Billing address from checkout (not used by default —
     *                                  tax is based on shipping destination, which is standard
     *                                  for most jurisdictions; the billing address is passed
     *                                  through in case a third-party filter needs it).
     * @return float
     */
    public function calculate_tax_total(
        float $default,
        array $cart,
        array $shipping_address,
        array $billing_address
    ): float {
        // Determine which address to tax against.
        // The standard (and default) is the shipping destination.
        // Third-party code can override this behaviour by hooking at a higher priority.
        $tax_address = apply_filters(
            'titancart_tax_address',
            $shipping_address,
            $billing_address,
            $cart
        );

        $manager = new TaxManager();
        return $manager->calculate_for_cart( $cart, $tax_address );
    }

    // ══════════════════════════════════════════════════════════════════════════
    // REST ROUTES
    // ══════════════════════════════════════════════════════════════════════════

    public function register_routes(): void {
        ( new TaxController() )->register_routes();
    }

    // ══════════════════════════════════════════════════════════════════════════
    // DEFAULT SEEDING
    // ══════════════════════════════════════════════════════════════════════════

    public function maybe_seed_defaults(): void {
        ( new TaxManager() )->maybe_seed_defaults();
    }
}
