<?php
/**
 * Plugin Name: WooNooW Biteship Shipping
 * Plugin URI: https://woonoow.com/addons/biteship
 * Description: Indonesia shipping integration with Biteship API - Example WooNooW Addon
 * Version: 1.0.0
 * Author: WooNooW Team
 * Author URI: https://woonoow.com
 * Requires Plugins: woonoow
 * 
 * This is an EXAMPLE addon demonstrating the WooNooW module system integration.
 * It shows both schema-based settings AND custom React component patterns.
 */

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

/**
 * Register Biteship as a WooNooW Module
 */
add_filter('woonoow/addon_registry', function($addons) {
    $addons['biteship-shipping'] = [
        'id'           => 'biteship-shipping',
        'name'         => 'Biteship Shipping',
        'description'  => 'Real-time shipping rates from Indonesian couriers (JNE, J&T, SiCepat, and more)',
        'version'      => '1.0.0',
        'author'       => 'WooNooW Team',
        'category'     => 'shipping',
        'icon'         => 'truck',
        'features'     => [
            'Real-time shipping rates',
            'Multiple courier support (JNE, J&T, SiCepat, AnterAja, Ninja Express)',
            'Automatic tracking integration',
            'Shipping label generation',
            'Cash on Delivery (COD) support',
        ],
        'has_settings' => true,
        // Option 1: Use schema-based settings (uncomment to use)
        // 'settings_component' => null,
        
        // Option 2: Use custom React component (current)
        'settings_component' => plugin_dir_url(__FILE__) . 'dist/Settings.js',
    ];
    return $addons;
});

/**
 * Register Settings Schema (Option 1: Schema-based)
 * 
 * This provides a no-code settings form automatically
 */
add_filter('woonoow/module_settings_schema', function($schemas) {
    $schemas['biteship-shipping'] = [
        'api_key' => [
            'type' => 'text',
            'label' => __('Biteship API Key', 'biteship'),
            'description' => __('Get your API key from Biteship dashboard', 'biteship'),
            'placeholder' => 'biteship_xxxxxxxxxxxxx',
            'required' => true,
        ],
        'environment' => [
            'type' => 'select',
            'label' => __('Environment', 'biteship'),
            'description' => __('Use test mode for development', 'biteship'),
            'options' => [
                'test' => __('Test Mode', 'biteship'),
                'production' => __('Production', 'biteship'),
            ],
            'default' => 'test',
        ],
        'origin_lat' => [
            'type' => 'text',
            'label' => __('Origin Latitude', 'biteship'),
            'description' => __('Your warehouse latitude coordinate', 'biteship'),
            'placeholder' => '-6.200000',
        ],
        'origin_lng' => [
            'type' => 'text',
            'label' => __('Origin Longitude', 'biteship'),
            'description' => __('Your warehouse longitude coordinate', 'biteship'),
            'placeholder' => '106.816666',
        ],
        'enable_cod' => [
            'type' => 'toggle',
            'label' => __('Enable Cash on Delivery', 'biteship'),
            'description' => __('Allow customers to pay on delivery', 'biteship'),
            'default' => false,
        ],
        'enable_insurance' => [
            'type' => 'toggle',
            'label' => __('Enable Shipping Insurance', 'biteship'),
            'description' => __('Automatically add insurance to shipments', 'biteship'),
            'default' => true,
        ],
        'enabled_couriers' => [
            'type' => 'select',
            'label' => __('Enabled Couriers', 'biteship'),
            'description' => __('Select which couriers to show to customers', 'biteship'),
            'options' => [
                'jne' => 'JNE',
                'jnt' => 'J&T Express',
                'sicepat' => 'SiCepat',
                'anteraja' => 'AnterAja',
                'ninja' => 'Ninja Express',
                'idexpress' => 'ID Express',
            ],
        ],
        'debug_mode' => [
            'type' => 'toggle',
            'label' => __('Debug Mode', 'biteship'),
            'description' => __('Log API requests for troubleshooting', 'biteship'),
            'default' => false,
        ],
    ];
    return $schemas;
});

/**
 * Hook into WooNooW shipping calculation
 * 
 * This is where the actual shipping logic would go
 */
add_filter('woonoow/shipping/calculate_rates', function($rates, $package) {
    // Check if module is enabled
    if (!class_exists('WooNooW\Core\ModuleRegistry')) {
        return $rates;
    }
    
    if (!\WooNooW\Core\ModuleRegistry::is_enabled('biteship-shipping')) {
        return $rates;
    }
    
    // Get settings
    $settings = get_option('woonoow_module_biteship-shipping_settings', []);
    
    if (empty($settings['api_key'])) {
        return $rates;
    }
    
    // TODO: Call Biteship API to get real rates
    // For now, return example rates
    $rates[] = [
        'id' => 'biteship_jne_reg',
        'label' => 'JNE Regular',
        'cost' => 15000,
        'meta_data' => [
            'courier' => 'JNE',
            'service' => 'REG',
            'etd' => '2-3 days',
        ],
    ];
    
    $rates[] = [
        'id' => 'biteship_jnt_reg',
        'label' => 'J&T Express Regular',
        'cost' => 12000,
        'meta_data' => [
            'courier' => 'J&T',
            'service' => 'REG',
            'etd' => '2-4 days',
        ],
    ];
    
    return $rates;
}, 10, 2);

/**
 * React to settings changes
 */
add_action('woonoow/module_settings_updated/biteship-shipping', function($settings) {
    // Clear any caches
    delete_transient('biteship_courier_list');
    
    // Log settings update in debug mode
    if (!empty($settings['debug_mode'])) {
        error_log('Biteship settings updated: ' . print_r($settings, true));
    }
});
