<?php

if (!defined('ABSPATH')) {
    exit;
}

class AIEO_Checkout_Block_Fields {

    public static function bootstrap() {
        add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_extensions'), 30);
        add_action('woocommerce_blocks_validate_location_address_fields', array(__CLASS__, 'validate_invoice_fields'), 10, 3);
    }

    public static function enqueue_extensions() {
        if (is_admin()) { return; }
        $handle = 'aieo-checkout-block-extensions';
        $rel    = 'assets/js/aieo-checkout-block-extensions.js';
        $abs    = AIEO_DIR . $rel;
        if (!file_exists($abs)) { return; }
        wp_enqueue_script(
            $handle,
            AIEO_URI . $rel,
            array(),
            (string) filemtime($abs),
            true
        );
    }

    
    public static function validate_invoice_fields(\WP_Error $errors, $fields, $group) {
        if ($group !== 'billing') { return; }
        $wants_invoice = !empty($fields['aieo/wants_invoice']);
        if (!$wants_invoice) { return; }

        foreach (array(
            'aieo/billing_vat'   => __('Συμπλήρωσε το ΑΦΜ για το τιμολόγιο.', 'ai-eshop-optimizer'),
            'aieo/billing_irs'   => __('Συμπλήρωσε τη ΔΟΥ.', 'ai-eshop-optimizer'),
            'aieo/billing_store' => __('Συμπλήρωσε το επάγγελμα/δραστηριότητα.', 'ai-eshop-optimizer'),
        ) as $key => $msg) {
            $val = isset($fields[$key]) ? trim((string) $fields[$key]) : '';
            if ($val === '') {
                $errors->add('aieo_invoice_required_' . md5($key), $msg);
            }
        }
    }
}
