<?php
// pages/components/coupon_modal.php
// Thin wrapper delegating to generic CRUD controller

use Components\Resources\Controllers\CrudController;
use Components\Models\Coupon;
use Components\Router;
use Components\Support\Htmx;
use Components\Ui\Stepper;

ob_start();

$isTemplate = (int) mh_get('is_template', 0, 'int');
if ($isTemplate <= 0) {
    $postedTemplateFlag = mh_post('is_template', null);
    if ($postedTemplateFlag !== null && (string)$postedTemplateFlag !== '') {
        $isTemplate = (int)$postedTemplateFlag;
    }
}

if (
    $isTemplate <= 0
    && strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'POST'
    && (array_key_exists('coupon_code_template', (array)($_POST ?? [])) || (string) mh_post('event_prefix', '') === 'coupon_template')
) {
    $isTemplate = 1;
}

$mode = (string) mh_get('mode', '', 'string');
if ($mode === '') { $mode = (string) mh_post('mode', ''); }

$presetId = (string) mh_get('preset_id', '');
if ($presetId === '') { $presetId = (string) mh_post('preset_id', ''); }

$fieldPresets = ['is_template' => $isTemplate ? '1' : '0'];
$headerHtml = '';
$submitLabel = null;
$cancelLabel = null;
$cancelAttrs = null;
$splitFooterActions = false;

if ($isTemplate > 0 && $mode === 'new') {
    $isPost = strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'POST';

    if (!$isPost && $presetId === '') {
        $url = Router::url('coupon_template_selection_modal', [
            'partial' => 1,
            'is_template' => 1,
        ]);

        Htmx::location([
            'path' => $url,
            'target' => '#' . Htmx::resolvePanelId() . '-body',
            'push' => 'false',
            'history' => false,
        ]);
        exit;
    }

    if ($presetId !== '') {
        $preset = Coupon::getPresetById($presetId);
        if (is_array($preset) && isset($preset['data']) && is_array($preset['data'])) {
            foreach ($preset['data'] as $k => $v) {
                $fieldPresets[(string)$k] = $v;
            }
        }
    }

    $steps = [
        [
            'id' => 'preset',
            'label' => (string) mh_lng('UI_STEP_COUPON_PRESET', 'Select Pre-Set'),
            'description' => (string) mh_lng('UI_STEP_COUPON_PRESET_DESC', 'Choose from commonly used coupon settings'),
        ],
        [
            'id' => 'details',
            'label' => (string) mh_lng('UI_STEP_COUPON_FINALIZE', 'Finalize'),
            'description' => (string) mh_lng('UI_STEP_COUPON_FINALIZE_DESC', 'Customize settings, labels, and restrictions'),
        ],
    ];

    $headerHtml = '<div class="absolute z-100 w-full px-6 py-4 border-b border-zinc-200 bg-zinc-50/80 -m-6">'
        . Stepper::render($steps, 'details')
        . '</div><div class="h-18"></div>';

    $cancelLabel = mh_lng('UI_PREVIOUS', 'Back');
    $submitLabel = mh_lng('UI_CREATE_COUPON_TEMPLATE', 'Create Coupon Template');
    $cancelAttrs = [
        'hx-get' => Router::url('coupon_template_selection_modal', ['partial' => 1, 'is_template' => 1]),
        'hx-target' => '#config-modal-body',
        'hx-push-url' => 'false',
    ];
    $splitFooterActions = true;
}

echo CrudController::handle([
    'modelClass'   => Coupon::class,
    'selfRoute'    => 'coupon_modal',
    'event_prefix' => $isTemplate ? 'coupon_template' : 'coupon',
    'field_presets' => $fieldPresets,
    'submit_label' => $submitLabel,
    'cancel_label' => $cancelLabel,
    'cancel_attrs' => $cancelAttrs,
    'split_footer_actions' => $splitFooterActions,
    'header_html' => $headerHtml,
]);

$__html = ob_get_clean();
echo $__html;
