<?php

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

function aieo_dmm_create_exchanger_feeds_table() {
    global $wpdb;

    $table = $wpdb->prefix . 'aieo_dmm_exchanger_feeds';
    $charset_collate = aieo_get_charset_collate();

    $sql = "CREATE TABLE IF NOT EXISTS {$table} (
        `id`                    bigint unsigned NOT NULL AUTO_INCREMENT,
        `slug`                  varchar(64)     NOT NULL,
        `label`                 varchar(191)    NOT NULL DEFAULT '',
        `enabled`               tinyint(1)      NOT NULL DEFAULT 1,
        `schedule_cron`         varchar(40)     NOT NULL DEFAULT '',
        `last_built_at`         datetime        DEFAULT NULL,
        `last_build_status`     varchar(40)     DEFAULT NULL,
        `feed_token`            char(64)        NOT NULL,
        `shard_size`            smallint unsigned NOT NULL DEFAULT 500,

        -- Targeting (mirrors wp_aieo_dmm_offers shape) -------------
        `brand_term_ids`        longtext        DEFAULT NULL,
        `category_term_ids`     longtext        DEFAULT NULL,
        `need_term_ids`         longtext        DEFAULT NULL,
        `product_ids`           longtext        DEFAULT NULL,
        `excluded_brand_ids`    longtext        DEFAULT NULL,
        `excluded_category_ids` longtext        DEFAULT NULL,
        `excluded_need_ids`     longtext        DEFAULT NULL,
        `excluded_product_ids`  longtext        DEFAULT NULL,
        `rule_operator`         varchar(8)      NOT NULL DEFAULT 'AND',
        `date_from`             date            DEFAULT NULL,
        `date_to`               date            DEFAULT NULL,
        `min_subtotal`          decimal(10,2)   NOT NULL DEFAULT '0.00',
        `price_min`             decimal(10,2)   DEFAULT NULL,
        `price_max`             decimal(10,2)   DEFAULT NULL,

        -- Push-notify configuration --------------------------------
        `push_targets`          longtext        DEFAULT NULL,

        `created_at`            datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `updated_at`            datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        UNIQUE KEY `slug` (`slug`),
        UNIQUE KEY `feed_token` (`feed_token`)
    ) {$charset_collate};";

    $wpdb->query($sql);

    aieo_create_index_if_not_exists('aieo_dmm_idx_exfeed_enabled', $table, '(enabled)');
}

function aieo_dmm_create_exchanger_imports_table() {
    global $wpdb;

    $table = $wpdb->prefix . 'aieo_dmm_exchanger_imports';
    $charset_collate = aieo_get_charset_collate();

    $sql = "CREATE TABLE IF NOT EXISTS {$table} (
        `id`              bigint unsigned NOT NULL AUTO_INCREMENT,
        `slug`            varchar(64)     NOT NULL,
        `label`           varchar(191)    NOT NULL DEFAULT '',
        `enabled`         tinyint(1)      NOT NULL DEFAULT 1,

        `source_url`      varchar(500)    NOT NULL DEFAULT '',
        `feed_token`      varchar(64)     NOT NULL DEFAULT '',
        `match_key`       varchar(20)     NOT NULL DEFAULT 'sku',
        `mode`            varchar(40)     NOT NULL DEFAULT 'create_link_only',

        `schedule_cron`   varchar(40)     NOT NULL DEFAULT '',
        `last_run_at`     datetime        DEFAULT NULL,
        `last_status`     varchar(40)     DEFAULT NULL,
        `cursor_state`    longtext        DEFAULT NULL,

        `created_at`      datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `updated_at`      datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (`id`),
        UNIQUE KEY `slug` (`slug`)
    ) {$charset_collate};";

    $wpdb->query($sql);

    aieo_create_index_if_not_exists('aieo_dmm_idx_eximp_enabled', $table, '(enabled)');
}

function aieo_dmm_create_exchanger_run_log_table() {
    global $wpdb;

    $table = $wpdb->prefix . 'aieo_dmm_exchanger_run_log';
    $charset_collate = aieo_get_charset_collate();

    $sql = "CREATE TABLE IF NOT EXISTS {$table} (
        `id`           bigint unsigned NOT NULL AUTO_INCREMENT,
        `job_id`       bigint unsigned NOT NULL,
        `side`         varchar(10)     NOT NULL DEFAULT 'target',
        `shard`        smallint unsigned NOT NULL DEFAULT 0,
        `started_at`   datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `finished_at`  datetime        DEFAULT NULL,
        `processed`    int unsigned    NOT NULL DEFAULT 0,
        `created`      int unsigned    NOT NULL DEFAULT 0,
        `updated`      int unsigned    NOT NULL DEFAULT 0,
        `skipped`      int unsigned    NOT NULL DEFAULT 0,
        `errors_count` int unsigned    NOT NULL DEFAULT 0,
        `errors_json`  longtext        DEFAULT NULL,
        PRIMARY KEY (`id`),
        KEY `job_started` (`job_id`, `started_at`)
    ) {$charset_collate};";

    $wpdb->query($sql);
}

function aieo_dmm_exchanger_install_schema() {
    aieo_dmm_create_exchanger_feeds_table();
    aieo_dmm_create_exchanger_imports_table();
    aieo_dmm_create_exchanger_run_log_table();
}
