<?php
/**
 * Pro subscription pricing (display + checkout hints).
 *
 * Stripe Prices on maca.se must match these values.
 *
 * @package Maca_Menu_List_Pro
 */

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

/**
 * Pricing configuration.
 *
 * @return array{
 *   lifetime_enabled: bool,
 *   annual_intro_sek: int,
 *   annual_regular_sek: int,
 *   intro_end_date: string,
 *   regular_start_date: string,
 *   currency_label: string
 * }
 */
function maca_menulist_pro_pricing_get_config() {
    $defaults = array(
        'lifetime_enabled'   => false,
        'annual_intro_sek'   => 499,
        'annual_regular_sek' => 799,
        'intro_end_date'     => '2026-08-31',
        'regular_start_date' => '2026-09-01',
        'currency_label'     => 'kr',
    );

    /**
     * Filter Pro subscription pricing shown in admin and docs.
     *
     * @param array<string, mixed> $config Pricing config.
     */
    return apply_filters('maca_menulist_pro_pricing', $defaults);
}

/**
 * @return bool
 */
function maca_menulist_pro_pricing_lifetime_enabled() {
    $config = maca_menulist_pro_pricing_get_config();
    return !empty($config['lifetime_enabled']);
}

/**
 * @return int
 */
function maca_menulist_pro_pricing_get_annual_intro_sek() {
    $config = maca_menulist_pro_pricing_get_config();
    return max(0, (int) $config['annual_intro_sek']);
}

/**
 * @return int
 */
function maca_menulist_pro_pricing_get_annual_regular_sek() {
    $config = maca_menulist_pro_pricing_get_config();
    return max(0, (int) $config['annual_regular_sek']);
}

/**
 * @param string $key intro_end_date|regular_start_date
 * @param string $fallback Fallback Y-m-d.
 * @return string
 */
function maca_menulist_pro_pricing_get_date($key, $fallback) {
    $config = maca_menulist_pro_pricing_get_config();
    $date = isset($config[$key]) ? (string) $config[$key] : '';
    if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
        return $fallback;
    }
    return $date;
}

/**
 * @return string Y-m-d
 */
function maca_menulist_pro_pricing_get_intro_end_date() {
    return maca_menulist_pro_pricing_get_date('intro_end_date', '2026-08-31');
}

/**
 * @return string Y-m-d
 */
function maca_menulist_pro_pricing_get_regular_start_date() {
    return maca_menulist_pro_pricing_get_date('regular_start_date', '2026-09-01');
}

/**
 * Whether the introductory annual price still applies (site timezone).
 *
 * @return bool
 */
function maca_menulist_pro_pricing_is_intro_active() {
    $end = maca_menulist_pro_pricing_get_intro_end_date();
    $end_ts = maca_menulist_wp_mysql_to_timestamp($end . ' 23:59:59');
    if ($end_ts === false) {
        return false;
    }
    return (int) current_time('timestamp') <= $end_ts;
}

/**
 * Current annual price in SEK (intro or regular).
 *
 * @return int
 */
function maca_menulist_pro_pricing_get_current_annual_sek() {
    if (maca_menulist_pro_pricing_is_intro_active()) {
        return maca_menulist_pro_pricing_get_annual_intro_sek();
    }
    return maca_menulist_pro_pricing_get_annual_regular_sek();
}

/**
 * @param string $ymd Date Y-m-d.
 * @return string
 */
function maca_menulist_pro_pricing_format_date_label($ymd) {
    return maca_menulist_format_wp_date($ymd, '', $ymd);
}

/**
 * @return string
 */
function maca_menulist_pro_pricing_get_intro_end_label() {
    return maca_menulist_pro_pricing_format_date_label(maca_menulist_pro_pricing_get_intro_end_date());
}

/**
 * @return string
 */
function maca_menulist_pro_pricing_get_regular_start_label() {
    return maca_menulist_pro_pricing_format_date_label(maca_menulist_pro_pricing_get_regular_start_date());
}

/**
 * @return string
 */
function maca_menulist_pro_pricing_get_currency_label() {
    $config = maca_menulist_pro_pricing_get_config();
    return isset($config['currency_label']) ? (string) $config['currency_label'] : 'kr';
}

/**
 * Short pricing summary for admin (HTML allowed: strong, em, br).
 *
 * @return string
 */
function maca_menulist_pro_pricing_get_summary_html() {
    $intro = maca_menulist_pro_pricing_get_annual_intro_sek();
    $regular = maca_menulist_pro_pricing_get_annual_regular_sek();
    $currency = esc_html(maca_menulist_pro_pricing_get_currency_label());
    $intro_end = esc_html(maca_menulist_pro_pricing_get_intro_end_label());
    $regular_start = esc_html(maca_menulist_pro_pricing_get_regular_start_label());

    if (maca_menulist_pro_pricing_is_intro_active()) {
        $message = sprintf(
            /* translators: 1: intro price, 2: currency, 3: intro end date, 4: regular price, 5: regular start date */
            __(
                '<strong>Annual subscription.</strong> Introductory price: %1$s %2$s/year through %3$s. From %5$s: %4$s %2$s/year. Lifetime license is not offered.',
                'maca-restu-pro'
            ),
            esc_html((string) $intro),
            $currency,
            $intro_end,
            esc_html((string) $regular),
            $regular_start
        );
    } else {
        $message = sprintf(
            /* translators: 1: regular price, 2: currency */
            __(
                '<strong>Annual subscription:</strong> %1$s %2$s per year. Lifetime license is not offered.',
                'maca-restu-pro'
            ),
            esc_html((string) $regular),
            $currency
        );
    }

    return wp_kses($message, array('strong' => array()));
}
