<?php
namespace LivraisonAPI;

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

class Shortcodes {
    
    public function __construct() {
        add_shortcode( 'livraison-prix', array( $this, 'render_price_shortcode' ) );
        add_shortcode( 'livraison-tracking', array( $this, 'render_tracking_shortcode' ) );
        add_shortcode( 'livraison-solde', array( $this, 'render_balance_shortcode' ) );
    }
    
    public function render_price_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'depart' => '',
            'arrivee' => ''
        ), $atts );
        
        if ( empty( $atts['depart'] ) || empty( $atts['arrivee'] ) ) {
            return '<p class="livraison-error">' . esc_html__( 'Please specify both departure and arrival communes.', 'sendi-api' ) . '</p>';
        }
        
        $api = new API();
        $agences_response = $api->get_available_agences( $atts['depart'], $atts['depart'], $atts['arrivee'] );
        
        if ( ! $agences_response['success'] || empty( $agences_response['data']['agences'] ) ) {
            return '<p class="livraison-error">' . esc_html__( 'No delivery available for this route.', 'sendi-api' ) . '</p>';
        }
        
        $agences = $agences_response['data']['agences'];
        $best_agence = reset( $agences );
        
        $is_same = $atts['depart'] === $atts['arrivee'];
        $price = $is_same ? ( $best_agence['deliveryPriceSame'] ?? 1500 ) : ( $best_agence['deliveryPriceDifferent'] ?? 2000 );
        
        return '<span class="livraison-price">' . number_format( $price, 0, ',', ' ' ) . ' FCFA</span>';
    }
    
    public function render_tracking_shortcode( $atts ) {
        $atts = shortcode_atts( array(
            'order_id' => ''
        ), $atts );
        
        if ( empty( $atts['order_id'] ) ) {
            return '<p class="livraison-error">' . esc_html__( 'Please specify an order ID.', 'sendi-api' ) . '</p>';
        }
        
        $api = new API();
        $result = $api->track_order( $atts['order_id'] );
        
        if ( ! $result['success'] ) {
            return '<p class="livraison-error">' . esc_html__( 'Unable to track this order.', 'sendi-api' ) . '</p>';
        }
        
        $status = isset( $result['data']['status'] ) ? $result['data']['status'] : 'unknown';
        $status_labels = array(
            'pending' => __( 'Pending', 'sendi-api' ),
            'assigned' => __( 'Assigned to agency', 'sendi-api' ),
            'in_progress' => __( 'In progress', 'sendi-api' ),
            'delivered' => __( 'Delivered', 'sendi-api' ),
            'cancelled' => __( 'Cancelled', 'sendi-api' )
        );
        
        $label = isset( $status_labels[ $status ] ) ? $status_labels[ $status ] : ucfirst( $status );
        $price = isset( $result['data']['price'] ) ? number_format( floatval( $result['data']['price'] ), 0, ',', ' ' ) : 'N/A';
        
        return '<div class="livraison-tracking">
            <p><strong>' . esc_html__( 'Status:', 'sendi-api' ) . '</strong> ' . esc_html( $label ) . '</p>
            <p><strong>' . esc_html__( 'Order ID:', 'sendi-api' ) . '</strong> ' . esc_html( $atts['order_id'] ) . '</p>
            <p><strong>' . esc_html__( 'Price:', 'sendi-api' ) . '</strong> ' . esc_html( $price ) . ' FCFA</p>
        </div>';
    }
    
    public function render_balance_shortcode() {
        $api = new API();
        $api_key = get_option( 'livraison_api_key', '' );
        
        if ( empty( $api_key ) ) {
            return '<p class="livraison-error">' . esc_html__( 'API key not configured.', 'sendi-api' ) . '</p>';
        }
        
        $result = $api->get_credits();
        
        if ( ! $result['success'] ) {
            return '<p class="livraison-error">' . esc_html__( 'Unable to retrieve balance.', 'sendi-api' ) . '</p>';
        }
        
        $credits = $result['data']['credits'] ?? 0;
        
        return '<span class="livraison-balance">' . number_format( $credits, 1, ',', ' ' ) . ' ' . esc_html__( 'credits', 'sendi-api' ) . '</span>';
    }
}