<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_shortcode( 'idsc_progress', 'idsc_progress_shortcode' );

function idsc_progress_shortcode( $atts ) {
    IDSC_Assets::flag_css();

    $atts = shortcode_atts( array(
        'value'    => '75',
        'label'    => '',
        'color'    => '#3b82f6',
        'bg_color' => '#e5e7eb',
        'height'   => '20',
        'show_pct' => 'yes',
        'animated' => 'no',
    ), $atts, 'idsc_progress' );

    $value   = max( 0, min( 100, intval( $atts['value'] ) ) );
    $height  = intval( $atts['height'] );
    $color   = sanitize_hex_color( $atts['color'] ) ?: '#3b82f6';
    $bg      = sanitize_hex_color( $atts['bg_color'] ) ?: '#e5e7eb';
    $anim    = $atts['animated'] === 'yes' ? ' idsc-progress--animated' : '';

    $html = '<div class="idsc-progress' . $anim . '">';

    if ( ! empty( $atts['label'] ) ) {
        $html .= '<div class="idsc-progress__header">';
        $html .= '<span class="idsc-progress__label">' . esc_html( $atts['label'] ) . '</span>';
        if ( $atts['show_pct'] === 'yes' ) {
            $html .= '<span class="idsc-progress__pct">' . $value . '%</span>';
        }
        $html .= '</div>';
    }

    $html .= '<div class="idsc-progress__track" style="background:' . esc_attr( $bg ) . ';height:' . $height . 'px">';
    $html .= '<div class="idsc-progress__bar" style="width:' . $value . '%;background:' . esc_attr( $color ) . '">';

    if ( $atts['show_pct'] === 'yes' && empty( $atts['label'] ) ) {
        $html .= '<span class="idsc-progress__pct-inner">' . $value . '%</span>';
    }

    $html .= '</div></div></div>';

    return $html;
}
