<?php
namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;

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

/**
 * Renders due-date and evergreen countdown timers with configurable expired states.
 */
class CountdownWidget extends BaseWidget {
    public function get_name(): string { return 'calio-countdown'; }
    public function get_title(): string { return esc_html__('Countdown', 'calio-addon'); }
    public function get_icon(): string { return 'eicon-countdown'; }
    public function get_style_depends(): array { return $this->resolve_widget_style_depends(); }
    public function get_script_depends(): array { return $this->resolve_widget_script_depends(); }

    /**
     * Registers timer behavior, layout and style controls for the Countdown widget.
     */
    protected function register_controls(): void {
        $this->start_controls_section('timer', ['label' => esc_html__('Timer', 'calio-addon')]);
        $this->add_control('countdown_type', ['label' => esc_html__('Type', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'due_date', 'options' => ['due_date' => esc_html__('Due Date', 'calio-addon'), 'evergreen' => esc_html__('Evergreen', 'calio-addon')]]);
        $this->add_control('due_date', ['label' => esc_html__('Due Date', 'calio-addon'), 'type' => Controls_Manager::DATE_TIME, 'default' => gmdate('Y-m-d H:i', strtotime('+10 days')), 'condition' => ['countdown_type' => 'due_date']]);
        $this->add_control('evergreen_hours', ['label' => esc_html__('Evergreen Hours', 'calio-addon'), 'type' => Controls_Manager::NUMBER, 'default' => 24, 'min' => 1, 'condition' => ['countdown_type' => 'evergreen']]);
        $this->add_control('evergreen_recurring', ['label' => esc_html__('Recurring', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes', 'condition' => ['countdown_type' => 'evergreen']]);
        $this->add_control('layout_mode', ['label' => esc_html__('Layout', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'grid', 'options' => ['grid' => esc_html__('Grid', 'calio-addon'), 'inline' => esc_html__('Inline', 'calio-addon')]]);
        $this->add_control('skin', ['label' => esc_html__('Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'boxed', 'options' => ['boxed' => esc_html__('Boxed', 'calio-addon'), 'soft' => esc_html__('Soft', 'calio-addon'), 'minimal' => esc_html__('Minimal', 'calio-addon')]]);
        $this->add_control('promo_eyebrow', ['label' => esc_html__('Promo Eyebrow', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('promo_note', ['label' => esc_html__('Promo Note', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control('support_note', ['label' => esc_html__('Support Note', 'calio-addon'), 'type' => Controls_Manager::TEXTAREA, 'default' => '']);
        $this->add_control('countdown_caption', ['label' => esc_html__('Countdown Caption', 'calio-addon'), 'type' => Controls_Manager::TEXTAREA, 'default' => '']);
        $this->add_control('evergreen_badge', ['label' => esc_html__('Evergreen Badge', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Auto-Renews', 'calio-addon'), 'condition' => ['countdown_type' => 'evergreen']]);
        $this->add_control('highlight_unit', ['label' => esc_html__('Highlight Unit', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'days' => esc_html__('Days', 'calio-addon'), 'hours' => esc_html__('Hours', 'calio-addon'), 'minutes' => esc_html__('Minutes', 'calio-addon'), 'seconds' => esc_html__('Seconds', 'calio-addon')]]);
        $this->add_control('columns', ['label' => esc_html__('Columns', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => '4', 'options' => ['2' => '2', '3' => '3', '4' => '4'], 'condition' => ['layout_mode' => 'grid']]);
        $this->add_control('show_labels', ['label' => esc_html__('Show Labels', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('label_display', ['label' => esc_html__('Label View', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'block', 'options' => ['block' => esc_html__('Block', 'calio-addon'), 'inline' => esc_html__('Inline', 'calio-addon')], 'condition' => ['show_labels' => 'yes']]);
        $this->add_control('show_separator', ['label' => esc_html__('Show Separator', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'condition' => ['layout_mode' => 'inline']]);
        $this->add_control('separator_text', ['label' => esc_html__('Separator', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => ':', 'condition' => ['show_separator' => 'yes', 'layout_mode' => 'inline']]);
        $this->add_control('alignment', ['label' => esc_html__('Alignment', 'calio-addon'), 'type' => Controls_Manager::CHOOSE, 'default' => 'center', 'options' => ['flex-start' => ['title' => esc_html__('Left', 'calio-addon'), 'icon' => 'eicon-text-align-left'], 'center' => ['title' => esc_html__('Center', 'calio-addon'), 'icon' => 'eicon-text-align-center'], 'flex-end' => ['title' => esc_html__('Right', 'calio-addon'), 'icon' => 'eicon-text-align-right']], 'selectors' => ['{{WRAPPER}} .calio-countdown-wrap' => 'justify-content: {{VALUE}};']]);
        $this->add_control('show_days', ['label' => esc_html__('Show Days', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('show_hours', ['label' => esc_html__('Show Hours', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('show_minutes', ['label' => esc_html__('Show Minutes', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('show_seconds', ['label' => esc_html__('Show Seconds', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('label_days', ['label' => esc_html__('Days Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Days', 'condition' => ['show_labels' => 'yes']]);
        $this->add_control('label_hours', ['label' => esc_html__('Hours Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Hours', 'condition' => ['show_labels' => 'yes']]);
        $this->add_control('label_minutes', ['label' => esc_html__('Minutes Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Minutes', 'condition' => ['show_labels' => 'yes']]);
        $this->add_control('label_seconds', ['label' => esc_html__('Seconds Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Seconds', 'condition' => ['show_labels' => 'yes']]);
        $this->add_control('expire_action', ['label' => esc_html__('Expire Action', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'message', 'options' => ['message' => esc_html__('Show Message', 'calio-addon'), 'hide' => esc_html__('Hide Timer', 'calio-addon'), 'redirect' => esc_html__('Redirect', 'calio-addon')]]);
        $this->add_control('expiry_message', ['label' => esc_html__('Expiry Message', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Time is up!', 'calio-addon'), 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expiry_title', ['label' => esc_html__('Expiry Title', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Offer expired', 'calio-addon'), 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expiry_note', ['label' => esc_html__('Expiry Note', 'calio-addon'), 'type' => Controls_Manager::TEXTAREA, 'default' => '', 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expired_badge', ['label' => esc_html__('Expired Badge', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '', 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expired_skin', ['label' => esc_html__('Expired Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'soft', 'options' => ['soft' => esc_html__('Soft', 'calio-addon'), 'spotlight' => esc_html__('Spotlight', 'calio-addon'), 'minimal' => esc_html__('Minimal', 'calio-addon')], 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expiry_button_text', ['label' => esc_html__('Expiry Button Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '', 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expiry_button_url', ['label' => esc_html__('Expiry Button URL', 'calio-addon'), 'type' => Controls_Manager::URL, 'default' => ['url' => ''], 'condition' => ['expire_action' => 'message']]);
        $this->add_control('expiry_redirect', ['label' => esc_html__('Redirect URL', 'calio-addon'), 'type' => Controls_Manager::URL, 'default' => ['url' => ''], 'condition' => ['expire_action' => 'redirect']]);
        $this->end_controls_section();
        $this->start_controls_section('style_items', ['label' => esc_html__('Items', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_control('grid_gap', ['label' => esc_html__('Gap', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 0, 'max' => 40]], 'selectors' => ['{{WRAPPER}} .calio-countdown' => 'gap: {{SIZE}}{{UNIT}};']]);
        $this->add_style_heading('countdown_item_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('item_padding', esc_html__('Item Padding', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item', 'padding');
        $this->add_dimensions_control('item_margin', esc_html__('Item Margin', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item', 'margin');
        $this->add_style_heading('countdown_item_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('countdown_item_state_tabs');
        $this->start_controls_tab('countdown_item_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_background_type_control('count_item_bg_normal_group', '{{WRAPPER}} .calio-countdown-item');
        $this->add_border_paint_controls('count_item_border_normal', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item');
        $this->add_dimensions_control('box_radius', esc_html__('Border Radius', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_background_type_control('count_number_bg_normal_group', '{{WRAPPER}} .calio-countdown-number');
        $this->end_controls_tab();
        $this->start_controls_tab('countdown_item_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_background_type_control('count_item_bg_hover_group', '{{WRAPPER}} .calio-countdown-item:hover');
        $this->add_border_paint_controls('count_item_border_hover', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item:hover');
        $this->add_background_type_control('count_number_bg_hover_group', '{{WRAPPER}} .calio-countdown-item:hover .calio-countdown-number');
        $this->end_controls_tab();
        $this->start_controls_tab('countdown_item_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_background_type_control('count_item_bg_active_group', '{{WRAPPER}} .calio-countdown-item:focus-within');
        $this->add_border_paint_controls('count_item_border_active', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item:focus-within');
        $this->add_background_type_control('count_number_bg_active_group', '{{WRAPPER}} .calio-countdown-item:focus-within .calio-countdown-number');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('countdown_item_outline_heading', esc_html__('Outline & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'item_border', 'selector' => '{{WRAPPER}} .calio-countdown-item']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'item_shadow', 'selector' => '{{WRAPPER}} .calio-countdown-item']);
        $this->end_controls_section();
        $this->start_controls_section('style_numbers', ['label' => esc_html__('Numbers', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('countdown_number_typo_heading', esc_html__('Typography', 'calio-addon'));
        $this->add_control('number_size', ['label' => esc_html__('Size', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 10, 'max' => 80]], 'selectors' => ['{{WRAPPER}} .calio-countdown-number' => 'font-size: {{SIZE}}{{UNIT}};']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'number_typo', 'selector' => '{{WRAPPER}} .calio-countdown-number']);
        $this->add_style_heading('countdown_number_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('countdown_number_state_tabs');
        $this->start_controls_tab('countdown_number_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('count_number_color_normal', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-number');
        $this->end_controls_tab();
        $this->start_controls_tab('countdown_number_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('count_number_color_hover', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item:hover .calio-countdown-number');
        $this->end_controls_tab();
        $this->start_controls_tab('countdown_number_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('count_number_color_active', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item:focus-within .calio-countdown-number');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->end_controls_section();
        $this->start_controls_section('style_labels', ['label' => esc_html__('Labels', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['show_labels' => 'yes']]);
        $this->add_style_heading('countdown_label_typo_heading', esc_html__('Typography', 'calio-addon'));
        $this->add_control('label_size', ['label' => esc_html__('Size', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 8, 'max' => 40]], 'selectors' => ['{{WRAPPER}} .calio-countdown-label' => 'font-size: {{SIZE}}{{UNIT}};']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'label_typo', 'selector' => '{{WRAPPER}} .calio-countdown-label']);
        $this->add_style_heading('countdown_label_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('countdown_label_state_tabs');
        $this->start_controls_tab('countdown_label_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('count_label_color_normal', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-label');
        $this->end_controls_tab();
        $this->start_controls_tab('countdown_label_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('count_label_color_hover', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item:hover .calio-countdown-label');
        $this->end_controls_tab();
        $this->start_controls_tab('countdown_label_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('count_label_color_active', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-item:focus-within .calio-countdown-label');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->end_controls_section();
        $this->start_controls_section('style_separator', ['label' => esc_html__('Separator', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['show_separator' => 'yes', 'layout_mode' => 'inline']]);
        $this->add_text_paint_controls('count_separator_text', esc_html__('Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-separator');
        $this->add_control('separator_size', ['label' => esc_html__('Size', 'calio-addon'), 'type' => Controls_Manager::SLIDER, 'range' => ['px' => ['min' => 10, 'max' => 48]], 'selectors' => ['{{WRAPPER}} .calio-countdown-separator' => 'font-size: {{SIZE}}{{UNIT}};']]);
        $this->end_controls_section();
        $this->start_controls_section('style_support', ['label' => esc_html__('Promo Notes', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'promo_note_typo', 'selector' => '{{WRAPPER}} .calio-countdown-promo-note, {{WRAPPER}} .calio-countdown-support-note, {{WRAPPER}} .calio-countdown-evergreen-badge']);
        $this->add_style_heading('countdown_support_surface_heading', esc_html__('Surface', 'calio-addon'));
        $this->add_text_paint_controls('count_support_text', esc_html__('Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-countdown-promo-eyebrow', '{{WRAPPER}} .calio-countdown-promo-note', '{{WRAPPER}} .calio-countdown-support-note', '{{WRAPPER}} .calio-countdown-caption', '{{WRAPPER}} .calio-countdown-evergreen-badge']);
        $this->add_background_type_control('count_support_bg_group', '{{WRAPPER}} .calio-countdown-support-note');
        $this->add_style_heading('countdown_support_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'count_support_border_group', 'selector' => '{{WRAPPER}} .calio-countdown-support-note, {{WRAPPER}} .calio-countdown-evergreen-badge']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'count_support_shadow_group', 'selector' => '{{WRAPPER}} .calio-countdown-support-note']);
        $this->add_style_heading('countdown_support_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('support_note_padding', esc_html__('Support Note Padding', 'calio-addon'), '{{WRAPPER}} .calio-countdown-support-note', 'padding');
        $this->add_dimensions_control('support_note_margin', esc_html__('Support Note Margin', 'calio-addon'), '{{WRAPPER}} .calio-countdown-support-note', 'margin');
        $this->add_dimensions_control('support_note_radius', esc_html__('Support Note Radius', 'calio-addon'), '{{WRAPPER}} .calio-countdown-support-note', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_background_type_control('count_support_badge_bg_group', '{{WRAPPER}} .calio-countdown-evergreen-badge');
        $this->end_controls_section();
        $this->start_controls_section('style_expired', ['label' => esc_html__('Expired State', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('countdown_expired_surface_heading', esc_html__('Surface', 'calio-addon'));
        $this->add_text_paint_controls('count_expired_text', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired');
        $this->add_background_type_control('count_expired_bg_group', '{{WRAPPER}} .calio-countdown-expired');
        $this->add_border_paint_controls('count_expired_border', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired');
        $this->add_style_heading('countdown_expired_button_heading', esc_html__('Button States', 'calio-addon'));
        $this->start_controls_tabs('expired_button_state_tabs');
        $this->start_controls_tab('expired_button_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('count_expired_button_text_normal', esc_html__('Button Text Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired-button');
        $this->add_background_type_control('count_expired_button_bg_normal_group', '{{WRAPPER}} .calio-countdown-expired-button');
        $this->add_text_paint_controls('count_expired_badge_text', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired-badge');
        $this->add_background_type_control('count_expired_badge_bg_group', '{{WRAPPER}} .calio-countdown-expired-badge');
        $this->end_controls_tab();
        $this->start_controls_tab('expired_button_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('count_expired_button_text_hover', esc_html__('Button Text Color', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired-button:hover');
        $this->add_background_type_control('count_expired_button_bg_hover_group', '{{WRAPPER}} .calio-countdown-expired-button:hover');
        $this->end_controls_tab();
        $this->start_controls_tab('expired_button_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('count_expired_button_text_active', esc_html__('Button Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-countdown-expired-button:active', '{{WRAPPER}} .calio-countdown-expired-button:focus-visible']);
        $this->add_background_type_control('count_expired_button_bg_active_group', ['{{WRAPPER}} .calio-countdown-expired-button:active', '{{WRAPPER}} .calio-countdown-expired-button:focus-visible']);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('countdown_expired_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'count_expired_border_group', 'selector' => '{{WRAPPER}} .calio-countdown-expired']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'count_expired_shadow_group', 'selector' => '{{WRAPPER}} .calio-countdown-expired']);
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'count_expired_button_border_group', 'selector' => '{{WRAPPER}} .calio-countdown-expired-button']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'count_expired_button_shadow_group', 'selector' => '{{WRAPPER}} .calio-countdown-expired-button']);
        $this->add_style_heading('countdown_expired_spacing_heading', esc_html__('Button Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('expired_button_padding', esc_html__('Button Padding', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired-button', 'padding');
        $this->add_dimensions_control('expired_button_margin', esc_html__('Button Margin', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired-button', 'margin');
        $this->add_dimensions_control('expired_button_radius', esc_html__('Button Radius', 'calio-addon'), '{{WRAPPER}} .calio-countdown-expired-button', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();
    }

    /**
     * Outputs countdown data attributes and expired-state markup for the frontend script.
     */
    protected function render(): void {
        $s = $this->get_settings_for_display();
        $type = (string) ($s['countdown_type'] ?? 'due_date');
        $data = [];
        if ($type === 'evergreen') {
            $data[] = 'data-type="evergreen"';
            $data[] = 'data-hours="' . esc_attr((string) max(1, (int) ($s['evergreen_hours'] ?? 24))) . '"';
            $data[] = 'data-recurring="' . ((($s['evergreen_recurring'] ?? 'yes') === 'yes') ? '1' : '0') . '"';
        } else {
            $date = (string) ($s['due_date'] ?? '');
            if ($date === '') { return; }
            $ts = strtotime($date . ' UTC');
            if ($ts === false) { return; }
            $data[] = 'data-type="due"';
            $data[] = 'data-date="' . esc_attr(gmdate('c', $ts)) . '"';
        }
        $data[] = 'data-expire="' . esc_attr((string) ($s['expire_action'] ?? 'message')) . '"';
        $redirect = (string) ($s['expiry_redirect']['url'] ?? '');
        if ($redirect !== '') { $data[] = 'data-redirect="' . esc_url($redirect) . '"'; }
        $data[] = 'data-separator="' . esc_attr((string) ($s['separator_text'] ?? ':')) . '"';
        $classes = ['calio-countdown', 'layout-' . sanitize_html_class((string) ($s['layout_mode'] ?? 'grid')), 'labels-' . sanitize_html_class((string) ($s['label_display'] ?? 'block')), 'cols-' . sanitize_html_class((string) ($s['columns'] ?? '4')), 'skin-' . sanitize_html_class((string) ($s['skin'] ?? 'boxed')), 'highlight-' . sanitize_html_class((string) ($s['highlight_unit'] ?? 'none'))];
        $expired_classes = ['calio-countdown-expired', 'expired-skin-' . sanitize_html_class((string) ($s['expired_skin'] ?? 'soft'))];
        if (($s['show_labels'] ?? 'yes') !== 'yes') { $classes[] = 'labels-hidden'; }
        if (($s['show_separator'] ?? '') === 'yes' && ($s['layout_mode'] ?? 'grid') === 'inline') { $classes[] = 'has-separator'; }
        echo '<div class="calio-countdown-wrap">';
        if (!empty($s['promo_eyebrow'])) { echo '<div class="calio-countdown-promo-eyebrow">' . esc_html((string) $s['promo_eyebrow']) . '</div>'; }
        if (!empty($s['promo_note'])) { echo '<div class="calio-countdown-promo-note">' . esc_html((string) $s['promo_note']) . '</div>'; }
        if ($type === 'evergreen' && !empty($s['evergreen_badge'])) { echo '<div class="calio-countdown-evergreen-badge">' . esc_html((string) $s['evergreen_badge']) . '</div>'; }
        if (!empty($s['countdown_caption'])) { echo '<div class="calio-countdown-caption">' . esc_html((string) $s['countdown_caption']) . '</div>'; }
        echo '<div class="' . esc_attr(implode(' ', $classes)) . '" ' . wp_kses_data(implode(' ', $data)) . '>';
        $units = [];
        if (($s['show_days'] ?? 'yes') === 'yes') { $units[] = ['days', (string) ($s['label_days'] ?? 'Days')]; }
        if (($s['show_hours'] ?? 'yes') === 'yes') { $units[] = ['hours', (string) ($s['label_hours'] ?? 'Hours')]; }
        if (($s['show_minutes'] ?? 'yes') === 'yes') { $units[] = ['minutes', (string) ($s['label_minutes'] ?? 'Minutes')]; }
        if (($s['show_seconds'] ?? 'yes') === 'yes') { $units[] = ['seconds', (string) ($s['label_seconds'] ?? 'Seconds')]; }
        foreach ($units as $index => $unit) {
            echo wp_kses($this->item((string) $unit[0], (string) $unit[1]), $this->allowed_countdown_markup());
            if (($s['show_separator'] ?? '') === 'yes' && ($s['layout_mode'] ?? 'grid') === 'inline' && $index < count($units) - 1) {
                echo '<span class="calio-countdown-separator">' . esc_html((string) ($s['separator_text'] ?? ':')) . '</span>';
            }
        }
        echo '</div>';
        echo '<div class="' . esc_attr(implode(' ', $expired_classes)) . '" style="display:none;">';
        if (!empty($s['expired_badge'])) { echo '<span class="calio-countdown-expired-badge">' . esc_html((string) $s['expired_badge']) . '</span>'; }
        if (!empty($s['expiry_title'])) { echo '<strong class="calio-countdown-expired-title">' . esc_html((string) $s['expiry_title']) . '</strong>'; }
        echo '<span class="calio-countdown-expired-message">' . esc_html((string) ($s['expiry_message'] ?? '')) . '</span>';
        if (!empty($s['expiry_note'])) { echo '<span class="calio-countdown-expired-note">' . esc_html((string) $s['expiry_note']) . '</span>'; }
        if (!empty($s['expiry_button_text']) && !empty($s['expiry_button_url']['url'])) {
            $button_href = (string) $s['expiry_button_url']['url'];
            $button_target = !empty($s['expiry_button_url']['is_external']) ? '_blank' : '';
            $rel = [];
            if (!empty($s['expiry_button_url']['is_external'])) {
                $rel[] = 'noopener';
                $rel[] = 'noreferrer';
            }
            if (!empty($s['expiry_button_url']['nofollow'])) {
                $rel[] = 'nofollow';
            }
            $button_rel = implode(' ', array_unique($rel));
            echo '<a class="calio-countdown-expired-button" href="' . esc_url($button_href) . '"';
            if ($button_target !== '') {
                echo ' target="' . esc_attr($button_target) . '"';
            }
            if ($button_rel !== '') {
                echo ' rel="' . esc_attr($button_rel) . '"';
            }
            echo '>' . esc_html((string) $s['expiry_button_text']) . '</a>';
        }
        echo '</div>';
        if (!empty($s['support_note'])) { echo '<div class="calio-countdown-support-note">' . esc_html((string) $s['support_note']) . '</div>'; }
        echo '</div>';
    }

    private function item(string $key, string $label): string {
        return sprintf('<div class="calio-countdown-item" data-item-unit="%1$s"><span class="calio-countdown-number" data-unit="%1$s">00</span><span class="calio-countdown-label">%2$s</span></div>', esc_attr($key), esc_html($label));
    }

    /**
     * @return array<string, array<string, bool>>
     */
    private function allowed_countdown_markup(): array {
        return [
            'div' => [
                'class' => true,
                'data-item-unit' => true,
            ],
            'span' => [
                'class' => true,
                'data-unit' => true,
            ],
        ];
    }
}



