<?php

namespace Calio\Widgets;

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

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

/**
 * Renders weekday opening hours with closed states and optional "today" highlighting.
 */
class BusinessHoursWidget extends BaseWidget
{
    public function get_name(): string
    {
        return 'calio-business-hours';
    }

    public function get_title(): string
    {
        return esc_html__('Business Hours', 'calio-addon');
    }

    public function get_icon(): string
    {
        return 'eicon-clock';
    }

    public function get_style_depends(): array
    {
        return $this->resolve_widget_style_depends('calio-widget-business-hours');
    }

    public function get_keywords(): array
    {
        return array_merge(parent::get_keywords(), ['business', 'hours', 'open', 'close', 'schedule']);
    }

    protected function register_controls(): void
    {
        $this->register_layout_controls();
        $this->register_hours_controls();
        $this->register_container_style_controls();
        $this->register_header_style_controls();
        $this->register_rows_style_controls();
        $this->register_today_style_controls();
    }

    private function register_layout_controls(): void
    {
        $this->start_controls_section('business_hours_layout', [
            'label' => esc_html__('Layout', 'calio-addon'),
        ]);

        $this->add_control('title', [
            'label' => esc_html__('Title', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Opening Hours', 'calio-addon'),
            'label_block' => true,
        ]);
        $this->add_control('subtitle', [
            'label' => esc_html__('Subtitle', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Plan your visit with our live weekly schedule.', 'calio-addon'),
            'label_block' => true,
        ]);
        $this->add_control('skin', [
            'label' => esc_html__('Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'clean',
            'options' => [
                'clean' => esc_html__('Clean', 'calio-addon'),
                'boxed' => esc_html__('Boxed', 'calio-addon'),
                'minimal' => esc_html__('Minimal', 'calio-addon'),
                'simple-list' => esc_html__('Simple List', 'calio-addon'),
            ],
        ]);
        $this->add_control('simple_list_open_only', [
            'label' => esc_html__('Simple List: Open Rows Only', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
            'condition' => ['skin' => 'simple-list'],
        ]);
        $this->add_control('simple_list_pin_today', [
            'label' => esc_html__('Simple List: Pin Today On Top', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
            'condition' => ['skin' => 'simple-list'],
        ]);
        $this->add_control('simple_list_empty_text', [
            'label' => esc_html__('Simple List Empty Text', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('No open hours available in this list.', 'calio-addon'),
            'condition' => ['skin' => 'simple-list', 'simple_list_open_only' => 'yes'],
        ]);
        $this->add_control('highlight_today', [
            'label' => esc_html__('Highlight Today', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('today_label', [
            'label' => esc_html__('Today Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Today', 'calio-addon'),
            'condition' => ['highlight_today' => 'yes'],
        ]);
        $this->add_control('open_close_separator', [
            'label' => esc_html__('Open / Close Separator', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => ' - ',
            'label_block' => false,
        ]);
        $this->add_control('closed_label', [
            'label' => esc_html__('Closed Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Closed', 'calio-addon'),
            'label_block' => false,
        ]);
        $this->add_control('show_timezone', [
            'label' => esc_html__('Show Timezone', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);
        $this->add_control('timezone_label', [
            'label' => esc_html__('Timezone Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('GMT+3', 'calio-addon'),
            'condition' => ['show_timezone' => 'yes'],
        ]);
        $this->add_control('show_day_icons', [
            'label' => esc_html__('Show Day Icons', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('day_icon_open', [
            'label' => esc_html__('Open Day Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-clock',
                'library' => 'fa-solid',
            ],
            'condition' => ['show_day_icons' => 'yes'],
        ]);
        $this->add_control('day_icon_closed', [
            'label' => esc_html__('Closed Day Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-calendar-times',
                'library' => 'fa-solid',
            ],
            'condition' => ['show_day_icons' => 'yes'],
        ]);
        $this->add_control('show_live_status', [
            'label' => esc_html__('Show Live Status', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('live_status_open_label', [
            'label' => esc_html__('Open Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Open Now', 'calio-addon'),
            'condition' => ['show_live_status' => 'yes'],
        ]);
        $this->add_control('live_status_closed_label', [
            'label' => esc_html__('Closed Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Closed Now', 'calio-addon'),
            'condition' => ['show_live_status' => 'yes'],
        ]);
        $this->add_control('show_row_live_dot', [
            'label' => esc_html__('Show Current Row Status', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
            'condition' => ['show_live_status' => 'yes'],
        ]);
        $this->add_control('row_live_dot_open_label', [
            'label' => esc_html__('Row Open Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Now', 'calio-addon'),
            'condition' => ['show_live_status' => 'yes', 'show_row_live_dot' => 'yes'],
        ]);
        $this->add_control('row_live_dot_closed_label', [
            'label' => esc_html__('Row Closed Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Closed', 'calio-addon'),
            'condition' => ['show_live_status' => 'yes', 'show_row_live_dot' => 'yes'],
        ]);
        $this->add_control('enable_schema_markup', [
            'label' => esc_html__('Schema Markup', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('schema_name', [
            'label' => esc_html__('Schema Business Name', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => get_bloginfo('name'),
            'label_block' => true,
            'condition' => ['enable_schema_markup' => 'yes'],
        ]);
        $this->add_responsive_control('day_column_width', [
            'label' => esc_html__('Day Column Width (%)', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['%'],
            'range' => [
                '%' => ['min' => 20, 'max' => 70],
            ],
            'default' => [
                'unit' => '%',
                'size' => 38,
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours' => '--calio-business-hours-day-width: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_responsive_control('rows_gap', [
            'label' => esc_html__('Rows Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 0, 'max' => 30],
            ],
            'default' => [
                'unit' => 'px',
                'size' => 10,
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours' => '--calio-business-hours-row-gap: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_control('row_alignment', [
            'label' => esc_html__('Row Vertical Alignment', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'center',
            'options' => [
                'flex-start' => esc_html__('Top', 'calio-addon'),
                'center' => esc_html__('Center', 'calio-addon'),
                'flex-end' => esc_html__('Bottom', 'calio-addon'),
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours-row' => 'align-items: {{VALUE}};',
            ],
        ]);

        $this->end_controls_section();
    }

    private function register_hours_controls(): void
    {
        $this->start_controls_section('business_hours_rows', [
            'label' => esc_html__('Rows', 'calio-addon'),
        ]);

        $day_options = $this->get_day_options();
        $repeater = new Repeater();
        $repeater->add_control('day_key', [
            'label' => esc_html__('Day Key', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'monday',
            'options' => $day_options,
        ]);
        $repeater->add_control('day_label', [
            'label' => esc_html__('Day Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Monday', 'calio-addon'),
            'label_block' => true,
        ]);
        $repeater->add_control('opens_at', [
            'label' => esc_html__('Opens At', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '09:00',
            'condition' => ['is_closed!' => 'yes'],
        ]);
        $repeater->add_control('closes_at', [
            'label' => esc_html__('Closes At', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '18:00',
            'condition' => ['is_closed!' => 'yes'],
        ]);
        $repeater->add_control('split_shift', [
            'label' => esc_html__('Split Shift', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
            'condition' => ['is_closed!' => 'yes'],
        ]);
        $repeater->add_control('opens_at_2', [
            'label' => esc_html__('Opens At (2)', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '14:00',
            'condition' => ['is_closed!' => 'yes', 'split_shift' => 'yes'],
        ]);
        $repeater->add_control('closes_at_2', [
            'label' => esc_html__('Closes At (2)', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '18:00',
            'condition' => ['is_closed!' => 'yes', 'split_shift' => 'yes'],
        ]);
        $repeater->add_control('is_closed', [
            'label' => esc_html__('Closed Day', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);
        $repeater->add_control('row_note', [
            'label' => esc_html__('Note', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '',
            'label_block' => true,
        ]);
        $repeater->add_control('highlight_row', [
            'label' => esc_html__('Force Highlight', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);

        $this->add_control('hours_rows', [
            'label' => esc_html__('Business Hours', 'calio-addon'),
            'type' => Controls_Manager::REPEATER,
            'fields' => $repeater->get_controls(),
            'title_field' => '{{{ day_label }}}',
            'default' => $this->get_default_rows(),
        ]);

        $this->end_controls_section();
    }

    private function register_container_style_controls(): void
    {
        $this->start_controls_section('business_hours_style_container', [
            'label' => esc_html__('Container', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_background_type_control('business_hours_container_bg', '{{WRAPPER}} .calio-business-hours');
        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'business_hours_container_border',
            'selector' => '{{WRAPPER}} .calio-business-hours',
        ]);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), [
            'name' => 'business_hours_container_shadow',
            'selector' => '{{WRAPPER}} .calio-business-hours',
        ]);
        $this->add_dimensions_control(
            'business_hours_container_radius',
            esc_html__('Border Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_container_padding',
            esc_html__('Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours',
            'padding'
        );

        $this->end_controls_section();
    }

    private function register_header_style_controls(): void
    {
        $this->start_controls_section('business_hours_style_header', [
            'label' => esc_html__('Header', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_title_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-title',
        ]);
        $this->add_text_paint_controls('business_hours_title_text', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-title');
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_subtitle_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-subtitle',
        ]);
        $this->add_text_paint_controls('business_hours_subtitle_text', esc_html__('Subtitle Color', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-subtitle');
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_timezone_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-timezone',
        ]);
        $this->add_text_paint_controls('business_hours_timezone_text', esc_html__('Timezone Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-timezone');
        $this->add_background_type_control('business_hours_timezone_bg', '{{WRAPPER}} .calio-business-hours-timezone');
        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'business_hours_timezone_border',
            'selector' => '{{WRAPPER}} .calio-business-hours-timezone',
        ]);
        $this->add_dimensions_control(
            'business_hours_timezone_radius',
            esc_html__('Timezone Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-timezone',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_timezone_padding',
            esc_html__('Timezone Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-timezone',
            'padding'
        );
        $this->add_style_heading('business_hours_live_status_heading', esc_html__('Live Status', 'calio-addon'));
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_live_status_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-live-status',
        ]);
        $this->add_text_paint_controls('business_hours_live_status_open_text', esc_html__('Open Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-live-status.is-open');
        $this->add_background_type_control('business_hours_live_status_open_bg', '{{WRAPPER}} .calio-business-hours-live-status.is-open');
        $this->add_text_paint_controls('business_hours_live_status_closed_text', esc_html__('Closed Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-live-status.is-closed');
        $this->add_background_type_control('business_hours_live_status_closed_bg', '{{WRAPPER}} .calio-business-hours-live-status.is-closed');
        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'business_hours_live_status_border',
            'selector' => '{{WRAPPER}} .calio-business-hours-live-status',
        ]);
        $this->add_dimensions_control(
            'business_hours_live_status_radius',
            esc_html__('Live Status Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-live-status',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_live_status_padding',
            esc_html__('Live Status Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-live-status',
            'padding'
        );
        $this->add_style_heading('business_hours_live_row_heading', esc_html__('Current Row Status', 'calio-addon'));
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_live_row_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-row-live',
        ]);
        $this->add_text_paint_controls('business_hours_live_row_open_text', esc_html__('Row Open Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-row-live.is-open');
        $this->add_background_type_control('business_hours_live_row_open_bg', '{{WRAPPER}} .calio-business-hours-row-live.is-open');
        $this->add_text_paint_controls('business_hours_live_row_closed_text', esc_html__('Row Closed Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-row-live.is-closed');
        $this->add_background_type_control('business_hours_live_row_closed_bg', '{{WRAPPER}} .calio-business-hours-row-live.is-closed');
        $this->add_control('business_hours_live_row_dot_open', [
            'label' => esc_html__('Row Open Dot Color', 'calio-addon'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours-row-live.is-open .calio-business-hours-row-live-dot' => 'background-color: {{VALUE}};',
            ],
        ]);
        $this->add_control('business_hours_live_row_dot_closed', [
            'label' => esc_html__('Row Closed Dot Color', 'calio-addon'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours-row-live.is-closed .calio-business-hours-row-live-dot' => 'background-color: {{VALUE}};',
            ],
        ]);
        $this->add_dimensions_control(
            'business_hours_live_row_radius',
            esc_html__('Current Row Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-row-live',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_live_row_padding',
            esc_html__('Current Row Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-row-live',
            'padding'
        );

        $this->end_controls_section();
    }

    private function register_rows_style_controls(): void
    {
        $this->start_controls_section('business_hours_style_rows', [
            'label' => esc_html__('Rows', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_day_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-day',
        ]);
        $this->add_text_paint_controls('business_hours_day_text', esc_html__('Day Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-day');
        $this->add_control('business_hours_day_icon_size', [
            'label' => esc_html__('Day Icon Size', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 8, 'max' => 36],
            ],
            'default' => ['unit' => 'px', 'size' => 14],
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours-day-icon i' => 'font-size: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-business-hours-day-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_text_paint_controls('business_hours_day_icon_open', esc_html__('Open Day Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-day-icon.is-open');
        $this->add_text_paint_controls('business_hours_day_icon_closed', esc_html__('Closed Day Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-day-icon.is-closed');
        $this->add_background_type_control('business_hours_day_icon_open_bg', '{{WRAPPER}} .calio-business-hours-day-icon.is-open');
        $this->add_background_type_control('business_hours_day_icon_closed_bg', '{{WRAPPER}} .calio-business-hours-day-icon.is-closed');
        $this->add_dimensions_control(
            'business_hours_day_icon_radius',
            esc_html__('Day Icon Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-day-icon',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_day_icon_padding',
            esc_html__('Day Icon Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-day-icon',
            'padding'
        );
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_time_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-time',
        ]);
        $this->add_text_paint_controls('business_hours_time_text', esc_html__('Time Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-time');
        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'business_hours_note_typography',
            'selector' => '{{WRAPPER}} .calio-business-hours-note',
        ]);
        $this->add_text_paint_controls('business_hours_note_text', esc_html__('Note Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-note');
        $this->add_text_paint_controls('business_hours_closed_text', esc_html__('Closed Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-time.is-closed');

        $this->add_style_heading('business_hours_rows_bg_heading', esc_html__('Row Background', 'calio-addon'));
        $this->add_background_type_control('business_hours_row_bg', '{{WRAPPER}} .calio-business-hours-row');
        $this->add_background_type_control('business_hours_row_bg_alt', '{{WRAPPER}} .calio-business-hours-row:nth-child(even)');
        $this->add_background_type_control('business_hours_row_bg_hover', '{{WRAPPER}} .calio-business-hours-row:hover, {{WRAPPER}} .calio-business-hours-row:focus-within');

        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'business_hours_row_border',
            'selector' => '{{WRAPPER}} .calio-business-hours-row',
        ]);
        $this->add_dimensions_control(
            'business_hours_row_radius',
            esc_html__('Row Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-row',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_row_padding',
            esc_html__('Row Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-row',
            'padding'
        );

        $this->add_control('business_hours_divider_color', [
            'label' => esc_html__('Divider Color', 'calio-addon'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours-row + .calio-business-hours-row' => 'border-top-color: {{VALUE}};',
            ],
        ]);
        $this->add_control('business_hours_divider_width', [
            'label' => esc_html__('Divider Width', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 0, 'max' => 8],
            ],
            'default' => [
                'unit' => 'px',
                'size' => 1,
            ],
            'selectors' => [
                '{{WRAPPER}} .calio-business-hours-row + .calio-business-hours-row' => 'border-top-width: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();
    }

    private function register_today_style_controls(): void
    {
        $this->start_controls_section('business_hours_style_today', [
            'label' => esc_html__('Today Highlight', 'calio-addon'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_text_paint_controls('business_hours_today_day_text', esc_html__('Today Row Day Text', 'calio-addon'), ['{{WRAPPER}} .calio-business-hours-row.is-today .calio-business-hours-day', '{{WRAPPER}} .calio-business-hours-row.is-highlight .calio-business-hours-day']);
        $this->add_text_paint_controls('business_hours_today_time_text', esc_html__('Today Row Time Text', 'calio-addon'), ['{{WRAPPER}} .calio-business-hours-row.is-today .calio-business-hours-time', '{{WRAPPER}} .calio-business-hours-row.is-highlight .calio-business-hours-time']);
        $this->add_background_type_control('business_hours_today_row_bg', ['{{WRAPPER}} .calio-business-hours-row.is-today', '{{WRAPPER}} .calio-business-hours-row.is-highlight']);
        $this->add_text_paint_controls('business_hours_today_pill_text', esc_html__('Today Pill Text', 'calio-addon'), '{{WRAPPER}} .calio-business-hours-today');
        $this->add_background_type_control('business_hours_today_pill_bg', '{{WRAPPER}} .calio-business-hours-today');
        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'business_hours_today_pill_border',
            'selector' => '{{WRAPPER}} .calio-business-hours-today',
        ]);
        $this->add_dimensions_control(
            'business_hours_today_pill_radius',
            esc_html__('Today Pill Radius', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-today',
            'border-radius',
            ['size_units' => ['px', '%']]
        );
        $this->add_dimensions_control(
            'business_hours_today_pill_padding',
            esc_html__('Today Pill Padding', 'calio-addon'),
            '{{WRAPPER}} .calio-business-hours-today',
            'padding'
        );

        $this->end_controls_section();
    }

    protected function render(): void
    {
        $settings = $this->get_settings_for_display();
        $rows = isset($settings['hours_rows']) && is_array($settings['hours_rows']) ? $settings['hours_rows'] : [];

        if ($rows === []) {
            return;
        }

        $wrapper_classes = [
            'calio-business-hours',
            'skin-' . sanitize_html_class((string) ($settings['skin'] ?? 'clean')),
        ];
        $skin = (string) ($settings['skin'] ?? 'clean');
        $is_simple_list_skin = $skin === 'simple-list';
        $highlight_today = ($settings['highlight_today'] ?? 'yes') === 'yes';
        $today_key = $highlight_today ? $this->get_today_key() : '';
        $today_label = trim((string) ($settings['today_label'] ?? esc_html__('Today', 'calio-addon')));
        $separator = (string) ($settings['open_close_separator'] ?? ' - ');
        if ($separator === '') {
            $separator = ' - ';
        }
        $closed_label = trim((string) ($settings['closed_label'] ?? esc_html__('Closed', 'calio-addon')));
        if ($closed_label === '') {
            $closed_label = esc_html__('Closed', 'calio-addon');
        }

        $title = trim((string) ($settings['title'] ?? ''));
        $subtitle = trim((string) ($settings['subtitle'] ?? ''));
        $show_timezone = ($settings['show_timezone'] ?? '') === 'yes';
        $timezone_label = trim((string) ($settings['timezone_label'] ?? ''));
        $show_day_icons = ($settings['show_day_icons'] ?? 'yes') === 'yes';
        $open_day_icon = $show_day_icons ? $this->render_day_icon($settings['day_icon_open'] ?? []) : '';
        $closed_day_icon = $show_day_icons ? $this->render_day_icon($settings['day_icon_closed'] ?? []) : '';
        $show_live_status = ($settings['show_live_status'] ?? 'yes') === 'yes';
        $open_status_label = trim((string) ($settings['live_status_open_label'] ?? esc_html__('Open Now', 'calio-addon')));
        if ($open_status_label === '') {
            $open_status_label = esc_html__('Open Now', 'calio-addon');
        }
        $closed_status_label = trim((string) ($settings['live_status_closed_label'] ?? esc_html__('Closed Now', 'calio-addon')));
        if ($closed_status_label === '') {
            $closed_status_label = esc_html__('Closed Now', 'calio-addon');
        }
        $show_row_live_dot = $show_live_status && (($settings['show_row_live_dot'] ?? 'yes') === 'yes');
        $row_open_label = trim((string) ($settings['row_live_dot_open_label'] ?? esc_html__('Now', 'calio-addon')));
        if ($row_open_label === '') {
            $row_open_label = esc_html__('Now', 'calio-addon');
        }
        $row_closed_label = trim((string) ($settings['row_live_dot_closed_label'] ?? esc_html__('Closed', 'calio-addon')));
        if ($row_closed_label === '') {
            $row_closed_label = esc_html__('Closed', 'calio-addon');
        }
        $current_day_state = $this->get_current_day_state();
        $simple_list_open_only = $is_simple_list_skin && (($settings['simple_list_open_only'] ?? '') === 'yes');
        $simple_list_pin_today = $is_simple_list_skin && (($settings['simple_list_pin_today'] ?? 'yes') === 'yes');
        $simple_list_empty_text = trim((string) ($settings['simple_list_empty_text'] ?? esc_html__('No open hours available in this list.', 'calio-addon')));
        if ($simple_list_empty_text === '') {
            $simple_list_empty_text = esc_html__('No open hours available in this list.', 'calio-addon');
        }

        if ($simple_list_open_only) {
            $rows = array_values(array_filter($rows, static function ($row): bool {
                return is_array($row) && (($row['is_closed'] ?? '') !== 'yes');
            }));
        }

        if ($simple_list_pin_today && $rows !== []) {
            $today_rows = [];
            $other_rows = [];

            foreach ($rows as $row) {
                if (!is_array($row)) {
                    $other_rows[] = $row;
                    continue;
                }

                $row_day_key = $this->normalize_day_key((string) ($row['day_key'] ?? 'custom'));
                if ($row_day_key === $current_day_state['day_key']) {
                    $today_rows[] = $row;
                } else {
                    $other_rows[] = $row;
                }
            }

            if ($today_rows !== []) {
                $rows = array_merge($today_rows, $other_rows);
            }
        }

        $is_open_now = $show_live_status ? $this->is_open_now($rows, $current_day_state) : false;
        $enable_schema_markup = ($settings['enable_schema_markup'] ?? 'yes') === 'yes';

        echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '">';
        if ($title !== '' || $subtitle !== '' || ($show_timezone && $timezone_label !== '') || $show_live_status) {
            echo '<div class="calio-business-hours-header">';
            if ($title !== '' || $subtitle !== '') {
                echo '<div class="calio-business-hours-header-copy">';
                if ($title !== '') {
                    echo '<h3 class="calio-business-hours-title">' . esc_html($title) . '</h3>';
                }
                if ($subtitle !== '') {
                    echo '<div class="calio-business-hours-subtitle">' . esc_html($subtitle) . '</div>';
                }
                echo '</div>';
            }
            if (($show_timezone && $timezone_label !== '') || $show_live_status) {
                echo '<div class="calio-business-hours-header-meta">';
                if ($show_timezone && $timezone_label !== '') {
                    echo '<div class="calio-business-hours-timezone">' . esc_html($timezone_label) . '</div>';
                }
                if ($show_live_status) {
                    echo '<div class="calio-business-hours-live-status ' . esc_attr($is_open_now ? 'is-open' : 'is-closed') . '" aria-live="polite">';
                    echo esc_html($is_open_now ? $open_status_label : $closed_status_label);
                    echo '</div>';
                }
                echo '</div>';
            }
            echo '</div>';
        }

        if ($rows === []) {
            echo '<div class="calio-business-hours-empty">' . esc_html($simple_list_empty_text) . '</div>';
        } else {
            echo '<div class="calio-business-hours-rows">';
            foreach ($rows as $row) {
                if (!is_array($row)) {
                    continue;
                }

                $day_key = $this->normalize_day_key((string) ($row['day_key'] ?? 'custom'));
                $day_label = trim((string) ($row['day_label'] ?? ''));
                if ($day_label === '') {
                    $day_options = $this->get_day_options();
                    $day_label = $day_options[$day_key] ?? esc_html__('Day', 'calio-addon');
                }

                $is_closed = ($row['is_closed'] ?? '') === 'yes';
                $is_split_shift = !$is_closed && (($row['split_shift'] ?? '') === 'yes');
                $is_forced = ($row['highlight_row'] ?? '') === 'yes';
                $is_today = $highlight_today && $day_key !== 'custom' && $day_key === $today_key;
                $is_current_day = $day_key !== 'custom' && $day_key === $current_day_state['day_key'];
                $row_live_state = '';
                if ($show_row_live_dot && $is_current_day) {
                    $row_live_state = $this->is_row_open_at_minutes($row, $current_day_state['minutes']) ? 'open' : 'closed';
                }
                $hours_value = $is_closed
                    ? $closed_label
                    : $this->build_hours_value(
                        trim((string) ($row['opens_at'] ?? '')),
                        trim((string) ($row['closes_at'] ?? '')),
                        $separator,
                        true
                    );
                $hours_value_2 = $is_split_shift
                    ? $this->build_hours_value(
                        trim((string) ($row['opens_at_2'] ?? '')),
                        trim((string) ($row['closes_at_2'] ?? '')),
                        $separator,
                        false
                    )
                    : '';
                $note = trim((string) ($row['row_note'] ?? ''));

                $row_classes = ['calio-business-hours-row'];
                if ($is_closed) {
                    $row_classes[] = 'is-closed';
                }
                if ($is_today) {
                    $row_classes[] = 'is-today';
                }
                if ($is_forced) {
                    $row_classes[] = 'is-highlight';
                }
                if ($hours_value_2 !== '') {
                    $row_classes[] = 'has-multiple-sessions';
                }

                echo '<div class="' . esc_attr(implode(' ', $row_classes)) . '">';
                echo '<div class="calio-business-hours-day-wrap">';
                if ($show_day_icons && ($open_day_icon !== '' || $closed_day_icon !== '')) {
                    $icon_markup = $is_closed ? $closed_day_icon : $open_day_icon;
                    if ($icon_markup !== '') {
                        echo '<span class="calio-business-hours-day-icon ' . esc_attr($is_closed ? 'is-closed' : 'is-open') . '" aria-hidden="true">' . wp_kses($icon_markup, $this->calio_allowed_icon_markup()) . '</span>';
                    }
                }
                echo '<span class="calio-business-hours-day">' . esc_html($day_label) . '</span>';
                if ($is_today && $today_label !== '') {
                    echo '<span class="calio-business-hours-today">' . esc_html($today_label) . '</span>';
                }
                if ($row_live_state !== '') {
                    echo '<span class="calio-business-hours-row-live is-' . esc_attr($row_live_state) . '">';
                    echo '<span class="calio-business-hours-row-live-dot" aria-hidden="true"></span>';
                    echo '<span class="calio-business-hours-row-live-text">' . esc_html($row_live_state === 'open' ? $row_open_label : $row_closed_label) . '</span>';
                    echo '</span>';
                }
                echo '</div>';
                echo '<div class="calio-business-hours-time-wrap">';
                echo '<span class="calio-business-hours-time' . ($is_closed ? ' is-closed' : '') . '">' . esc_html($hours_value) . '</span>';
                if ($hours_value_2 !== '') {
                    echo '<span class="calio-business-hours-time is-secondary-session">' . esc_html($hours_value_2) . '</span>';
                }
                if ($note !== '') {
                    echo '<span class="calio-business-hours-note">' . esc_html($note) . '</span>';
                }
                echo '</div>';
                echo '</div>';
            }
            echo '</div>';
        }
        echo '</div>';

        if ($enable_schema_markup) {
            $schema_data = $this->build_schema_data($rows, $settings);
            if (!empty($schema_data)) {
                $schema_json = wp_json_encode(
                    $schema_data,
                    JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
                );
                if (is_string($schema_json) && $schema_json !== '') {
                    wp_print_inline_script_tag($schema_json, ['type' => 'application/ld+json']);
                }
            }
        }
    }

    /**
     * @return array<string, string>
     */
    private function get_day_options(): array
    {
        return [
            'monday' => esc_html__('Monday', 'calio-addon'),
            'tuesday' => esc_html__('Tuesday', 'calio-addon'),
            'wednesday' => esc_html__('Wednesday', 'calio-addon'),
            'thursday' => esc_html__('Thursday', 'calio-addon'),
            'friday' => esc_html__('Friday', 'calio-addon'),
            'saturday' => esc_html__('Saturday', 'calio-addon'),
            'sunday' => esc_html__('Sunday', 'calio-addon'),
            'custom' => esc_html__('Custom', 'calio-addon'),
        ];
    }

    /**
     * @return array<int, array<string, string>>
     */
    private function get_default_rows(): array
    {
        return [
            ['day_key' => 'monday', 'day_label' => esc_html__('Monday', 'calio-addon'), 'opens_at' => '09:00', 'closes_at' => '18:00'],
            ['day_key' => 'tuesday', 'day_label' => esc_html__('Tuesday', 'calio-addon'), 'opens_at' => '09:00', 'closes_at' => '18:00'],
            ['day_key' => 'wednesday', 'day_label' => esc_html__('Wednesday', 'calio-addon'), 'opens_at' => '09:00', 'closes_at' => '13:00', 'split_shift' => 'yes', 'opens_at_2' => '14:00', 'closes_at_2' => '18:00', 'row_note' => esc_html__('Lunch break 13:00 - 14:00', 'calio-addon')],
            ['day_key' => 'thursday', 'day_label' => esc_html__('Thursday', 'calio-addon'), 'opens_at' => '09:00', 'closes_at' => '18:00'],
            ['day_key' => 'friday', 'day_label' => esc_html__('Friday', 'calio-addon'), 'opens_at' => '09:00', 'closes_at' => '18:00'],
            ['day_key' => 'saturday', 'day_label' => esc_html__('Saturday', 'calio-addon'), 'opens_at' => '10:00', 'closes_at' => '16:00'],
            ['day_key' => 'sunday', 'day_label' => esc_html__('Sunday', 'calio-addon'), 'is_closed' => 'yes', 'row_note' => esc_html__('Online support only', 'calio-addon')],
        ];
    }

    private function normalize_day_key(string $key): string
    {
        $allowed = array_keys($this->get_day_options());
        return in_array($key, $allowed, true) ? $key : 'custom';
    }

    private function get_today_key(): string
    {
        $map = [
            1 => 'monday',
            2 => 'tuesday',
            3 => 'wednesday',
            4 => 'thursday',
            5 => 'friday',
            6 => 'saturday',
            7 => 'sunday',
        ];

        $today = (int) wp_date('N');
        return $map[$today] ?? '';
    }

    private function build_hours_value(string $open, string $close, string $separator, bool $fallback_unavailable = true): string
    {
        if ($open !== '' && $close !== '') {
            return $open . $separator . $close;
        }

        if ($open !== '') {
            return $open;
        }

        if ($close !== '') {
            return $close;
        }

        if ($fallback_unavailable) {
            return esc_html__('Unavailable', 'calio-addon');
        }

        return '';
    }

    /**
     * @param array<int, mixed> $rows
     * @param array<string, mixed> $settings
     * @return array<string, mixed>
     */
    private function build_schema_data(array $rows, array $settings): array
    {
        $opening_specs = [];

        foreach ($rows as $row) {
            if (!is_array($row)) {
                continue;
            }

            if (($row['is_closed'] ?? '') === 'yes') {
                continue;
            }

            $day_key = $this->normalize_day_key((string) ($row['day_key'] ?? 'custom'));
            $day_url = $this->map_day_to_schema($day_key);
            if ($day_url === null) {
                continue;
            }

            $open_1 = trim((string) ($row['opens_at'] ?? ''));
            $close_1 = trim((string) ($row['closes_at'] ?? ''));
            if ($this->is_schema_time($open_1) && $this->is_schema_time($close_1)) {
                $opening_specs[] = [
                    '@type' => 'OpeningHoursSpecification',
                    'dayOfWeek' => $day_url,
                    'opens' => $open_1,
                    'closes' => $close_1,
                ];
            }

            if (($row['split_shift'] ?? '') === 'yes') {
                $open_2 = trim((string) ($row['opens_at_2'] ?? ''));
                $close_2 = trim((string) ($row['closes_at_2'] ?? ''));
                if ($this->is_schema_time($open_2) && $this->is_schema_time($close_2)) {
                    $opening_specs[] = [
                        '@type' => 'OpeningHoursSpecification',
                        'dayOfWeek' => $day_url,
                        'opens' => $open_2,
                        'closes' => $close_2,
                    ];
                }
            }
        }

        if ($opening_specs === []) {
            return [];
        }

        $business_name = trim((string) ($settings['schema_name'] ?? get_bloginfo('name')));
        if ($business_name === '') {
            $business_name = get_bloginfo('name');
        }

        return [
            '@context' => 'https://schema.org',
            '@type' => 'LocalBusiness',
            'name' => $business_name,
            'openingHoursSpecification' => $opening_specs,
        ];
    }

    private function map_day_to_schema(string $day_key): ?string
    {
        $map = [
            'monday' => 'https://schema.org/Monday',
            'tuesday' => 'https://schema.org/Tuesday',
            'wednesday' => 'https://schema.org/Wednesday',
            'thursday' => 'https://schema.org/Thursday',
            'friday' => 'https://schema.org/Friday',
            'saturday' => 'https://schema.org/Saturday',
            'sunday' => 'https://schema.org/Sunday',
        ];

        return $map[$day_key] ?? null;
    }

    private function is_schema_time(string $time): bool
    {
        return (bool) preg_match('/^(?:[01]?\d|2[0-3]):[0-5]\d$/', $time);
    }

    /**
     * @param array<int, mixed> $rows
     * @param array{day_key: string, minutes: int}|null $current
     */
    private function is_open_now(array $rows, ?array $current = null): bool
    {
        if ($current === null) {
            $current = $this->get_current_day_state();
        }
        $day_key = $current['day_key'];
        $minutes = $current['minutes'];

        foreach ($rows as $row) {
            if (!is_array($row)) {
                continue;
            }

            if (($row['is_closed'] ?? '') === 'yes') {
                continue;
            }

            $row_day = $this->normalize_day_key((string) ($row['day_key'] ?? 'custom'));
            if ($row_day !== $day_key) {
                continue;
            }

            if ($this->is_row_open_at_minutes($row, $minutes)) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param array<string, mixed> $row
     */
    private function is_row_open_at_minutes(array $row, int $minutes): bool
    {
        if (($row['is_closed'] ?? '') === 'yes') {
            return false;
        }

        if ($this->is_time_in_slot(
            trim((string) ($row['opens_at'] ?? '')),
            trim((string) ($row['closes_at'] ?? '')),
            $minutes
        )) {
            return true;
        }

        return ($row['split_shift'] ?? '') === 'yes' && $this->is_time_in_slot(
            trim((string) ($row['opens_at_2'] ?? '')),
            trim((string) ($row['closes_at_2'] ?? '')),
            $minutes
        );
    }

    /**
     * @return array{day_key: string, minutes: int}
     */
    private function get_current_day_state(): array
    {
        $now = new \DateTimeImmutable('now', wp_timezone());
        $map = [
            1 => 'monday',
            2 => 'tuesday',
            3 => 'wednesday',
            4 => 'thursday',
            5 => 'friday',
            6 => 'saturday',
            7 => 'sunday',
        ];

        $day_num = (int) $now->format('N');
        return [
            'day_key' => $map[$day_num] ?? 'custom',
            'minutes' => ((int) $now->format('H') * 60) + (int) $now->format('i'),
        ];
    }

    private function parse_minutes(string $time): ?int
    {
        if (!$this->is_schema_time($time)) {
            return null;
        }

        [$hours, $minutes] = array_map('intval', explode(':', $time));
        return ($hours * 60) + $minutes;
    }

    private function is_time_in_slot(string $opens_at, string $closes_at, int $current_minutes): bool
    {
        $open_minutes = $this->parse_minutes($opens_at);
        $close_minutes = $this->parse_minutes($closes_at);

        if ($open_minutes === null || $close_minutes === null) {
            return false;
        }

        if ($open_minutes === $close_minutes) {
            return false;
        }

        if ($open_minutes < $close_minutes) {
            return $current_minutes >= $open_minutes && $current_minutes < $close_minutes;
        }

        return $current_minutes >= $open_minutes || $current_minutes < $close_minutes;
    }

    /**
     * @param array<string, mixed> $icon Icon control payload.
     */
    private function render_day_icon(array $icon): string
    {
        return $this->get_icon_markup($icon);
    }

}
