<?php
if (!defined('ABSPATH')) exit;
$webhooks_active = class_exists('Fusion_WooDoo_Webhooks') ? (new Fusion_WooDoo_Webhooks())->check_stale() : false;
?>
<div class="wrap fusion-woodoo-admin">
    <h1><?php esc_html_e('Fusion WooDoo Settings', 'fusion-woodoo'); ?></h1>

    <div class="fusion-woodoo-header">
        <p><?php esc_html_e('Configure your Odoo connection and customer portal settings below.', 'fusion-woodoo'); ?></p>
    </div>

    <div class="fusion-woodoo-card">
        <form method="post" action="options.php">
            <?php
            settings_fields('fusion_woodoo_settings');
            do_settings_sections('fusion-woodoo-settings');
            submit_button(__('Save Settings', 'fusion-woodoo'));
            ?>
        </form>
    </div>

    <div class="fusion-woodoo-card">
        <h2><?php esc_html_e('Connection Status', 'fusion-woodoo'); ?></h2>
        <p>
            <strong><?php esc_html_e('Odoo URL:', 'fusion-woodoo'); ?></strong>
            <?php echo esc_html(get_option('fusion_woodoo_odoo_url', __('Not configured', 'fusion-woodoo'))); ?>
        </p>
        <p>
            <strong><?php esc_html_e('Odoo Reachable:', 'fusion-woodoo'); ?></strong>
            <?php if ($webhooks_active): ?>
                <span class="fusion-woodoo-status fusion-woodoo-status--ok"><?php esc_html_e('Yes', 'fusion-woodoo'); ?></span>
            <?php else: ?>
                <span class="fusion-woodoo-status fusion-woodoo-status--error"><?php esc_html_e('Not confirmed', 'fusion-woodoo'); ?></span>
            <?php endif; ?>
        </p>
        <button type="button" id="fusion-woodoo-test-connection" class="button button-secondary">
            <?php esc_html_e('Test Connection', 'fusion-woodoo'); ?>
        </button>
        <span id="fusion-woodoo-test-result" class="fusion-woodoo-test-result" style="display:none;margin-left:12px;"></span>
    </div>

    <div class="fusion-woodoo-card">
        <h2><?php esc_html_e('Webhook Status', 'fusion-woodoo'); ?></h2>
        <p><?php esc_html_e('Webhooks push real-time order, product, and customer updates from WooCommerce to Odoo.', 'fusion-woodoo'); ?></p>
        <?php
        $data_store = WC_Data_Store::load('webhook');
        $all_ids    = $data_store->get_webhooks_ids('active');
        $wh_list    = [];
        foreach ($all_ids as $id) {
            $wh = wc_get_webhook($id);
            if ($wh && str_starts_with($wh->get_name(), 'fusion-woodoo-')) {
                $wh_list[] = $wh;
            }
        }
        ?>
        <?php if (empty($wh_list)): ?>
            <p><em><?php esc_html_e('No Fusion WooDoo webhooks registered. Save a valid Odoo URL to register them automatically.', 'fusion-woodoo'); ?></em></p>
        <?php else: ?>
            <table class="widefat striped">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Name', 'fusion-woodoo'); ?></th>
                        <th><?php esc_html_e('Topic', 'fusion-woodoo'); ?></th>
                        <th><?php esc_html_e('Delivery URL', 'fusion-woodoo'); ?></th>
                        <th><?php esc_html_e('Status', 'fusion-woodoo'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($wh_list as $wh): ?>
                        <tr>
                            <td><?php echo esc_html($wh->get_name()); ?></td>
                            <td><?php echo esc_html($wh->get_topic()); ?></td>
                            <td><?php echo esc_html($wh->get_delivery_url()); ?></td>
                            <td>
                                <span class="fusion-woodoo-status fusion-woodoo-status--<?php echo $wh->get_status() === 'active' ? 'ok' : 'error'; ?>">
                                    <?php echo esc_html(ucfirst($wh->get_status())); ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>
