<?php

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

function aieo_dmm_install_promo_surfaces_schema() {
    global $wpdb;
    $charset_collate = function_exists('aieo_get_charset_collate')
        ? aieo_get_charset_collate()
        : $wpdb->get_charset_collate();

    $surfaces_tbl = $wpdb->prefix . 'aieo_dmm_promo_surfaces';
    $wpdb->query("CREATE TABLE IF NOT EXISTS {$surfaces_tbl} (
        `surface_id`    bigint unsigned NOT NULL AUTO_INCREMENT,
        `slug`          varchar(64)     NOT NULL,
        `label`         varchar(191)    NOT NULL DEFAULT '',
        `layout_preset` varchar(40)     NOT NULL DEFAULT 'hero_plus_3banners',
        `is_active`     tinyint(1)      NOT NULL DEFAULT 1,
        `created_at`    datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `updated_at`    datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (`surface_id`),
        UNIQUE KEY `uq_promo_surface_slug` (`slug`)
    ) {$charset_collate};");

    $slots_tbl = $wpdb->prefix . 'aieo_dmm_promo_slots';
    $wpdb->query("CREATE TABLE IF NOT EXISTS {$slots_tbl} (
        `slot_id`          bigint unsigned NOT NULL AUTO_INCREMENT,
        `surface_id`       bigint unsigned NOT NULL,
        `position`         smallint unsigned NOT NULL DEFAULT 0,
        `slot_type`        varchar(20)     NOT NULL DEFAULT 'slide',
        `sort_order`       smallint unsigned NOT NULL DEFAULT 0,
        `desktop_image_id` bigint unsigned NOT NULL DEFAULT 0,
        `mobile_image_id`  bigint unsigned NOT NULL DEFAULT 0,
        `youtube_id`       varchar(20)     NOT NULL DEFAULT '',
        `target_url`       varchar(512)    NOT NULL DEFAULT '',
        `alt_text`         varchar(191)    NOT NULL DEFAULT '',
        `time_delay_ms`    int unsigned    NOT NULL DEFAULT 4000,
        `campaign_id`      bigint unsigned NOT NULL DEFAULT 0,
        `is_active`        tinyint(1)      NOT NULL DEFAULT 1,
        `created_at`       datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP,
        `updated_at`       datetime        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        PRIMARY KEY (`slot_id`),
        KEY `idx_promo_surface_pos` (`surface_id`, `position`, `sort_order`),
        KEY `idx_promo_campaign` (`campaign_id`)
    ) {$charset_collate};");
}
