<?php
/**
 * Plugin dashboard page
 *
 * @package AfriRegions
 */

defined( 'ABSPATH' ) or die( 'Forbidden' );

class SCPWOO_Page_Dashboard {

    /** @return void */
    public static function render(): void {
        $country_codes = SCPWOO_Geo_Helper::get_country_codes();
        $wc_countries  = function_exists( 'WC' ) && WC()->countries ? WC()->countries->get_countries() : array();

        // ── Country names ──
        $country_names = array();
        foreach ( $country_codes as $cc ) {
            $country_names[] = isset( $wc_countries[ $cc ] ) ? $wc_countries[ $cc ] : $cc;
        }

        // ── Restrictions ──
        $restrictions = SCPWOO_States_Places::get_json_option( SCPWOO_States_Places::OPTION_RESTRICTED_ZONES );
        $restrictions_enabled = ( isset( $restrictions['enabled'] ) && $restrictions['enabled'] === 'yes' );
        $blocked_regions = isset( $restrictions['regions'] ) ? count( $restrictions['regions'] ) : 0;
        $blocked_cities  = isset( $restrictions['cities'] ) ? count( $restrictions['cities'] ) : 0;

        // ── Orders ──
        $has_orders = function_exists( 'wc_get_orders' )
            ? ! empty( wc_get_orders( array( 'limit' => 1, 'return' => 'ids', 'type' => 'shop_order' ) ) )
            : false;

        // ── Shipping methods count ──
        $shipping_count = 1; // Zone Shipping is always active

        // ── Setup guide ──
        $states_by_country = SCPWOO_Geo_Helper::get_states_by_country();
        $guide_dismissed = get_user_meta( get_current_user_id(), SCPWOO_States_Places::META_GUIDE_DISMISSED, true );

        $steps = array(
            array(
                'key'   => 'countries',
                'title' => __( 'Configure your countries', 'afriregions-city-neighborhood-for-woocommerce' ),
                'desc'  => __( 'Add at least one supported African country in your WooCommerce selling/shipping settings.', 'afriregions-city-neighborhood-for-woocommerce' ),
                'done'  => count( $country_codes ) > 0,
                'link'  => admin_url( 'admin.php?page=wc-settings&tab=general' ),
                'cta'   => __( 'WooCommerce settings', 'afriregions-city-neighborhood-for-woocommerce' ),
                'icon'  => 'dashicons-admin-site-alt3',
            ),
            array(
                'key'   => 'geodata',
                'title' => __( 'Review your geographic data', 'afriregions-city-neighborhood-for-woocommerce' ),
                'desc'  => __( 'Check the regions, cities, and neighborhoods available. Add custom ones if needed.', 'afriregions-city-neighborhood-for-woocommerce' ),
                'done'  => ! empty( $states_by_country ),
                'link'  => admin_url( 'admin.php?page=scpwoo&tab=geodata' ),
                'cta'   => __( 'Geo Data', 'afriregions-city-neighborhood-for-woocommerce' ),
                'icon'  => 'dashicons-location-alt',
            ),
            array(
                'key'   => 'settings',
                'title' => __( 'Configure your features', 'afriregions-city-neighborhood-for-woocommerce' ),
                'desc'  => __( 'Enable or disable address validation.', 'afriregions-city-neighborhood-for-woocommerce' ),
                'done'  => ! empty( SCPWOO_States_Places::get_json_option( SCPWOO_States_Places::OPTION_SETTINGS ) ),
                'link'  => admin_url( 'admin.php?page=scpwoo&tab=settings' ),
                'cta'   => __( 'Settings', 'afriregions-city-neighborhood-for-woocommerce' ),
                'icon'  => 'dashicons-admin-generic',
            ),
            array(
                'key'   => 'test',
                'title' => __( 'Place a test order', 'afriregions-city-neighborhood-for-woocommerce' ),
                'desc'  => __( 'Go to your store, add a product to cart, and verify that regions/cities/neighborhoods appear at checkout.', 'afriregions-city-neighborhood-for-woocommerce' ),
                'done'  => $has_orders,
                'link'  => wc_get_checkout_url(),
                'cta'   => __( 'Go to checkout', 'afriregions-city-neighborhood-for-woocommerce' ),
                'icon'  => 'dashicons-cart',
                'external' => true,
            ),
        );

        $steps_done  = count( array_filter( $steps, function( $s ) { return $s['done']; } ) );
        $steps_total = count( $steps );
        $all_done    = $steps_done === $steps_total;
        $progress    = round( ( $steps_done / $steps_total ) * 100 );

        ?>
        <div class="scpwoo-dash">


            <!-- Stats grid -->
            <div class="scpwoo-dash-stats-grid">
                <div class="scpwoo-dash-stat-card">
                    <p class="scpwoo-dash-label"><?php esc_html_e( 'Active countries', 'afriregions-city-neighborhood-for-woocommerce' ); ?></p>
                    <p class="scpwoo-dash-value"><?php echo esc_html( count( $country_codes ) ); ?></p>
                    <p class="scpwoo-dash-delta"><?php
                        $flags = array();
                        foreach ( array_slice( $country_codes, 0, 6 ) as $cc ) {
                            $cc = strtoupper( $cc );
                            $flag = mb_chr( 0x1F1E6 + ord( $cc[0] ) - ord( 'A' ) )
                                  . mb_chr( 0x1F1E6 + ord( $cc[1] ) - ord( 'A' ) );
                            $flags[] = $flag . ' ' . esc_html( $cc );
                        }
                        echo implode( ' · ', $flags );
                    ?></p>
                </div>
                <div class="scpwoo-dash-stat-card">
                    <p class="scpwoo-dash-label"><?php esc_html_e( 'Blocked zones', 'afriregions-city-neighborhood-for-woocommerce' ); ?></p>
                    <?php if ( $restrictions_enabled && ( $blocked_regions + $blocked_cities ) > 0 ) : ?>
                        <p class="scpwoo-dash-value"><?php echo esc_html( $blocked_regions + $blocked_cities ); ?></p>
                        <p class="scpwoo-dash-delta"><?php
                            /* translators: %1$d = blocked regions count, %2$d = blocked cities count */
                            echo esc_html( sprintf( __( '%1$d regions · %2$d cities', 'afriregions-city-neighborhood-for-woocommerce' ), $blocked_regions, $blocked_cities ) );
                        ?></p>
                    <?php else : ?>
                        <p class="scpwoo-dash-value"><?php esc_html_e( 'Off', 'afriregions-city-neighborhood-for-woocommerce' ); ?></p>
                        <p class="scpwoo-dash-delta"><?php esc_html_e( 'restrictions disabled', 'afriregions-city-neighborhood-for-woocommerce' ); ?></p>
                    <?php endif; ?>
                </div>
                <div class="scpwoo-dash-stat-card">
                    <p class="scpwoo-dash-label"><?php esc_html_e( 'Shipping methods', 'afriregions-city-neighborhood-for-woocommerce' ); ?></p>
                    <p class="scpwoo-dash-value"><?php echo esc_html( $shipping_count ); ?></p>
                    <p class="scpwoo-dash-delta"><?php esc_html_e( 'active', 'afriregions-city-neighborhood-for-woocommerce' ); ?></p>
                </div>
            </div>

            <?php if ( ! $guide_dismissed ) : ?>
            <!-- Setup Guide -->
            <div class="scpwoo-guide-card scpwoo-guide">
                <div class="scpwoo-guide-header">
                    <div class="scpwoo-guide-header-left">
                        <h2 class="scpwoo-guide-title">
                            <?php echo $all_done ? esc_html__( 'All set!', 'afriregions-city-neighborhood-for-woocommerce' ) : esc_html__( 'Getting started', 'afriregions-city-neighborhood-for-woocommerce' ); ?>
                        </h2>
                        <p class="scpwoo-guide-subtitle">
                            <?php
                            /* translators: %1$d = completed steps, %2$d = total steps */
                            echo esc_html( sprintf( __( '%1$d of %2$d steps completed', 'afriregions-city-neighborhood-for-woocommerce' ), $steps_done, $steps_total ) );
                            ?>
                        </p>
                    </div>
                    <button type="button" class="scpwoo-guide-dismiss" id="scpwoo-dismiss-guide" title="<?php esc_attr_e( 'Dismiss', 'afriregions-city-neighborhood-for-woocommerce' ); ?>">&times;</button>
                </div>
                <div class="scpwoo-guide-progress">
                    <div class="scpwoo-guide-progress-bar" style="width:<?php echo esc_attr( $progress ); ?>%;"></div>
                </div>
                <div class="scpwoo-guide-steps">
                    <?php foreach ( $steps as $i => $step ) : ?>
                    <div class="scpwoo-guide-step <?php echo $step['done'] ? 'scpwoo-guide-step-done' : ''; ?>">
                        <div class="scpwoo-guide-step-check">
                            <?php if ( $step['done'] ) : ?>
                                <span class="dashicons dashicons-yes-alt"></span>
                            <?php else : ?>
                                <span class="scpwoo-guide-step-num"><?php echo esc_html( $i + 1 ); ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="scpwoo-guide-step-body">
                            <h4 class="scpwoo-guide-step-title"><?php echo esc_html( $step['title'] ); ?></h4>
                            <p class="scpwoo-guide-step-desc"><?php echo esc_html( $step['desc'] ); ?></p>
                            <?php if ( ! $step['done'] ) : ?>
                                <a href="<?php echo esc_url( $step['link'] ); ?>" class="button button-primary button-small" <?php echo ! empty( $step['external'] ) ? 'target="_blank"' : ''; ?>>
                                    <?php echo esc_html( $step['cta'] ); ?>
                                </a>
                            <?php endif; ?>
                        </div>
                        <span class="dashicons <?php echo esc_attr( $step['icon'] ); ?> scpwoo-guide-step-icon"></span>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>


        </div>
        <?php
    }
}
