<?php

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

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

    $tbl = $wpdb->prefix . 'aieo_price_history';
    
    $wpdb->query("CREATE TABLE IF NOT EXISTS {$tbl} (
        `id`            bigint unsigned NOT NULL AUTO_INCREMENT,
        `product_id`    bigint unsigned NOT NULL,
        `parent_id`     bigint unsigned NULL DEFAULT NULL,
        `price`         decimal(12,4)   NOT NULL,
        `regular_price` decimal(12,4)   NULL DEFAULT NULL,
        `on_sale`       tinyint(1)      NOT NULL DEFAULT 0,
        `currency`      char(3)         NOT NULL DEFAULT 'EUR',
        `observed_at`   datetime        NOT NULL,
        `source`        varchar(20)     NOT NULL DEFAULT 'price_change_hook',
        PRIMARY KEY (`id`),
        KEY `idx_product_time` (`product_id`, `observed_at`),
        KEY `idx_currency` (`currency`)
    ) {$charset_collate};");
}
