-- ===========================================
-- Migration 002 : Table de log des relais WooCommerce
-- ===========================================
-- Trace chaque evenement recu de WordPress et relaye vers l'Ecole

CREATE TABLE IF NOT EXISTS woocommerce_relay_log (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    event_type      VARCHAR(100) NOT NULL COMMENT 'Ex: woocommerce.user.created',
    wc_entity_id    VARCHAR(50) NULL COMMENT 'ID WooCommerce de l entite (user_id, order_id, etc.)',
    payload_in      JSON NULL COMMENT 'Payload brut recu de WordPress',
    payload_out     JSON NULL COMMENT 'Payload transforme envoye a l Ecole',
    relay_status    ENUM('success', 'failed', 'transform_error', 'auth_error') NOT NULL DEFAULT 'failed',
    http_code       SMALLINT UNSIGNED NULL COMMENT 'Code HTTP retour de l Ecole',
    response_body   TEXT NULL COMMENT 'Reponse de l Ecole',
    ecole_processed TINYINT(1) NULL COMMENT 'Champ processed de la reponse Ecole',
    error_message   TEXT NULL,
    source          ENUM('wc_native', 'wp_plugin') NOT NULL DEFAULT 'wc_native',
    created_at      DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_event_type (event_type),
    INDEX idx_relay_status (relay_status, created_at),
    INDEX idx_wc_entity (event_type, wc_entity_id),
    INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
