<?php
/**
 * Plugin Name: RecordGod Bridge
 * Description: Thin bridge to RecordGod — RecordGod owns the catalog/stock/pricing/shipping; this plugin
 *              renders SEO storefront pages from its /shop API, mints Woo products on the fly at add-to-cart,
 *              injects postage, and posts completed orders back. Successor to WowPlatter.
 * Version: 0.1.0
 * Author: Monster Robot
 * Requires Plugins: woocommerce
 */

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

define('RG_BRIDGE_VERSION', '0.1.0');
define('RG_BRIDGE_DIR', plugin_dir_path(__FILE__));

require_once RG_BRIDGE_DIR . 'includes/class-rg-api.php';
require_once RG_BRIDGE_DIR . 'includes/class-rg-cart.php';
require_once RG_BRIDGE_DIR . 'includes/class-rg-storefront.php';
require_once RG_BRIDGE_DIR . 'includes/class-rg-orders.php';

// Shipping method loads only once Woo's class exists.
add_action('woocommerce_shipping_init', function () {
    require_once RG_BRIDGE_DIR . 'includes/class-rg-shipping.php';
});
add_filter('woocommerce_shipping_methods', function ($methods) {
    $methods['recordgod'] = 'RG_Shipping';
    return $methods;
});

// Wire the pieces.
add_action('plugins_loaded', function () {
    RG_Cart::init();
    RG_Storefront::init();
    RG_Orders::init();
});

// --- Settings (Settings API): base URL + bridge key + store id ---------------
add_action('admin_menu', function () {
    add_options_page('RecordGod Bridge', 'RecordGod Bridge', 'manage_options', 'rg-bridge', 'rg_bridge_settings_page');
});
add_action('admin_init', function () {
    register_setting('rg_bridge', 'rg_base_url');
    register_setting('rg_bridge', 'rg_bridge_key');
    register_setting('rg_bridge', 'rg_store_id');
});
function rg_bridge_settings_page() {
    ?>
    <div class="wrap">
        <h1>RecordGod Bridge</h1>
        <form method="post" action="options.php">
            <?php settings_fields('rg_bridge'); ?>
            <table class="form-table">
                <tr><th>RecordGod base URL</th>
                    <td><input type="url" name="rg_base_url" class="regular-text"
                        value="<?php echo esc_attr(get_option('rg_base_url', 'https://recordgod.com')); ?>"
                        placeholder="https://recordgod.com"></td></tr>
                <tr><th>Bridge key</th>
                    <td><input type="text" name="rg_bridge_key" class="regular-text"
                        value="<?php echo esc_attr(get_option('rg_bridge_key', '')); ?>">
                    <p class="description">Must match the <code>bridge_key</code> secret in RecordGod settings.</p></td></tr>
                <tr><th>Store ID</th>
                    <td><input type="number" name="rg_store_id" class="small-text"
                        value="<?php echo esc_attr(get_option('rg_store_id', '1')); ?>"></td></tr>
            </table>
            <?php submit_button(); ?>
        </form>
        <p><a href="<?php echo esc_url(home_url('/records')); ?>">View storefront →</a></p>
    </div>
    <?php
}

// Storefront rewrite rules need flushing on (de)activate.
register_activation_hook(__FILE__, function () {
    RG_Storefront::rewrites();
    flush_rewrite_rules();
});
register_deactivation_hook(__FILE__, 'flush_rewrite_rules');
