<?php

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

class AIEO_MOI_Schema {

    const OPTION_VERSION = 'aieo_moi_schema_version';
    const SCHEMA_VERSION = '1.0.0';

    public static function install() {
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';

        $vendors_tbl = $wpdb->prefix . 'aieo_moi_vendors';
        $links_tbl   = $wpdb->prefix . 'aieo_moi_order_links';
        $log_tbl     = $wpdb->prefix . 'aieo_moi_inbound_log';

        
        
        
        
        $sql_vendors = "CREATE TABLE {$vendors_tbl} (
            id              bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            slug            varchar(64)         NOT NULL,
            provider        varchar(40)         NOT NULL,
            label           varchar(120)        NOT NULL,
            credentials_json longtext           NULL,
            webhook_token   varchar(64)         NOT NULL,
            default_order_status varchar(40)    NOT NULL DEFAULT 'wc-on-hold',
            sku_strategy    varchar(20)         NOT NULL DEFAULT 'sku',
            sku_meta_key    varchar(64)         NULL,
            enabled         tinyint(1)          NOT NULL DEFAULT 1,
            created_at      datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at      datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY uk_slug (slug),
            UNIQUE KEY uk_webhook_token (webhook_token),
            KEY ix_provider_enabled (provider, enabled)
        ) {$charset_collate};";

        
        
        
        $sql_links = "CREATE TABLE {$links_tbl} (
            id              bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            vendor_id       bigint(20) unsigned NOT NULL,
            remote_order_id varchar(80)         NOT NULL,
            wc_order_id     bigint(20) unsigned NOT NULL,
            remote_state    varchar(40)         NULL,
            created_at      datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at      datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY uk_vendor_remote (vendor_id, remote_order_id),
            KEY ix_wc_order (wc_order_id)
        ) {$charset_collate};";

        
        
        
        
        $sql_log = "CREATE TABLE {$log_tbl} (
            id              bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            vendor_id       bigint(20) unsigned NULL,
            event_type      varchar(40)         NOT NULL,
            remote_order_id varchar(80)         NULL,
            payload_hash    char(64)            NULL,
            status          varchar(20)         NOT NULL DEFAULT 'received',
            error_text      text                NULL,
            wc_order_id     bigint(20) unsigned NULL,
            received_at     datetime            NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY ix_vendor_received (vendor_id, received_at),
            KEY ix_status (status),
            KEY ix_payload_hash (payload_hash)
        ) {$charset_collate};";

        dbDelta($sql_vendors);
        dbDelta($sql_links);
        dbDelta($sql_log);

        update_option(self::OPTION_VERSION, self::SCHEMA_VERSION, false);
    }

    public static function tables() {
        global $wpdb;
        return array(
            'vendors'     => $wpdb->prefix . 'aieo_moi_vendors',
            'order_links' => $wpdb->prefix . 'aieo_moi_order_links',
            'inbound_log' => $wpdb->prefix . 'aieo_moi_inbound_log',
        );
    }
}
