-- Corrected Azure Retail Prices provider Silver table.
--
-- Because the table has been purged in the current reconstruction scenario,
-- recreating it from this DDL is the simplest and safest option.

DROP TABLE IF EXISTS azure_retail_prices_silver;

CREATE TABLE azure_retail_prices_silver (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,

    -- Stable identity of one Azure Retail pricing version.
    price_key CHAR(64) NOT NULL,

    currency_code CHAR(3) NOT NULL,
    tier_minimum_units DECIMAL(38,12) NOT NULL DEFAULT 0,
    reservation_term VARCHAR(64) NULL,

    retail_price DECIMAL(38,12) NOT NULL,
    unit_price DECIMAL(38,12) NOT NULL,

    arm_region_name VARCHAR(255) NULL,
    location VARCHAR(500) NULL,
    effective_start_date DATETIME(6) NOT NULL,

    meter_id VARCHAR(128) NOT NULL,
    meter_name VARCHAR(1000) NULL,

    product_id VARCHAR(255) NULL,
    product_name TEXT NULL,

    sku_id VARCHAR(255) NULL,
    sku_name TEXT NULL,

    service_name VARCHAR(500) NULL,
    service_id VARCHAR(255) NULL,
    service_family VARCHAR(500) NULL,

    unit_of_measure VARCHAR(255) NULL,
    api_type VARCHAR(100) NULL,
    arm_sku_name VARCHAR(500) NULL,
    is_primary_meter_region BOOLEAN NULL,

    -- Semantic hash of normalized pricing attributes.
    pricing_hash CHAR(64) NOT NULL,

    -- Exact hash of the RAW provider payload.
    source_hash CHAR(64) NOT NULL,

    first_seen_at DATETIME(6) NOT NULL,
    last_seen_at DATETIME(6) NOT NULL,

    source_bronze_id BIGINT UNSIGNED NOT NULL,
    source_ingestion_run_id CHAR(36) NULL,

    ingestion_run_id CHAR(36) NULL,
    normalization_version VARCHAR(64) NOT NULL,
    normalized_at DATETIME(6) NOT NULL,

    PRIMARY KEY (id),

    UNIQUE KEY uk_retail_silver_price_key (
        price_key
    ),

    KEY idx_retail_silver_meter_date (
        meter_id,
        effective_start_date
    ),

    KEY idx_retail_silver_name_date (
        meter_name(191),
        effective_start_date
    ),

    KEY idx_retail_silver_currency_date (
        currency_code,
        effective_start_date
    ),

    KEY idx_retail_silver_lookup (
        currency_code,
        meter_id,
        api_type,
        effective_start_date
    ),

    KEY idx_retail_silver_source_bronze (
        source_bronze_id
    ),

    KEY idx_retail_silver_pricing_hash (
        pricing_hash
    )
)
ENGINE=InnoDB
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
