CREATE TABLE IF NOT EXISTS gcp_billing_silver (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,

    -- Pipeline/acquisition dimensions.
    business_date DATE NOT NULL,
    usage_date DATE NULL,

    -- GCP billing identity / invoice.
    billing_account_id VARCHAR(128) NOT NULL,
    invoice_month CHAR(6) NULL,
    invoice_publisher_type VARCHAR(32) NULL,
    cost_type VARCHAR(64) NULL,

    -- Service and SKU.
    service_id VARCHAR(128) NULL,
    service_description TEXT NULL,
    sku_id VARCHAR(128) NULL,
    sku_description TEXT NULL,

    -- Usage and provider processing timestamps.
    usage_start DATETIME(6) NULL,
    usage_end DATETIME(6) NULL,
    export_time DATETIME(6) NOT NULL,

    -- Project hierarchy.
    project_id VARCHAR(255) NULL,
    project_number VARCHAR(64) NULL,
    project_name TEXT NULL,
    project_ancestry_numbers TEXT NULL,
    project_ancestors LONGTEXT NULL,
    project_labels LONGTEXT NULL,

    -- Resource labels/tags and geography.
    labels LONGTEXT NULL,
    system_labels LONGTEXT NULL,
    tags LONGTEXT NULL,
    location VARCHAR(255) NULL,
    country VARCHAR(32) NULL,
    region VARCHAR(255) NULL,
    zone VARCHAR(255) NULL,

    -- Detailed-export resource identity.
    resource_name TEXT NULL,
    resource_global_name TEXT NULL,

    -- Cost model.
    cost DECIMAL(38,12) NOT NULL,
    cost_at_list DECIMAL(38,12) NULL,
    cost_at_effective_price_default DECIMAL(38,12) NULL,
    cost_at_list_consumption_model DECIMAL(38,12) NULL,
    credits_total DECIMAL(38,12) NOT NULL,
    net_cost DECIMAL(38,12) NOT NULL,
    currency CHAR(3) NOT NULL,
    currency_conversion_rate DECIMAL(38,12) NULL,

    -- Usage quantities.
    usage_amount DECIMAL(38,12) NULL,
    usage_unit VARCHAR(255) NULL,
    usage_amount_in_pricing_units DECIMAL(38,12) NULL,
    usage_pricing_unit VARCHAR(255) NULL,

    -- Provider-native structures retained for audit/reconciliation.
    credits LONGTEXT NULL,
    adjustment_info LONGTEXT NULL,
    transaction_type VARCHAR(64) NULL,
    seller_name TEXT NULL,

    -- Pricing fields available in the detailed export.
    price_list_price DECIMAL(38,12) NULL,
    price_effective_price_default DECIMAL(38,12) NULL,
    price_list_price_consumption_model DECIMAL(38,12) NULL,
    price_effective_price DECIMAL(38,12) NULL,
    price_tier_start_amount DECIMAL(38,12) NULL,
    price_unit VARCHAR(255) NULL,
    price_pricing_unit_quantity DECIMAL(38,12) NULL,

    -- Consumption model / commitments.
    consumption_model_id VARCHAR(255) NULL,
    consumption_model_description TEXT NULL,
    subscription_instance_id VARCHAR(255) NULL,
    applied_subscription_instance_id VARCHAR(255) NULL,

    -- Complete Bronze -> Silver lineage.
    source_bronze_id BIGINT UNSIGNED NOT NULL,
    source_page_number INT NULL,
    source_record_number INT NULL,
    source_payload_hash CHAR(64) 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,

    created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),

    PRIMARY KEY (id),
    KEY idx_gcp_billing_silver_business_date (business_date),
    KEY idx_gcp_billing_silver_usage_date (usage_date),
    KEY idx_gcp_billing_silver_invoice_month (invoice_month),
    KEY idx_gcp_billing_silver_project_usage (project_id, usage_date),
    KEY idx_gcp_billing_silver_service_sku (service_id, sku_id, usage_date),
    KEY idx_gcp_billing_silver_export_time (export_time),
    KEY idx_gcp_billing_silver_source_bronze (source_bronze_id),
    KEY idx_gcp_billing_silver_source_hash (source_payload_hash),

    CONSTRAINT ck_gcp_billing_silver_project_ancestors_json
        CHECK (project_ancestors IS NULL OR JSON_VALID(project_ancestors)),
    CONSTRAINT ck_gcp_billing_silver_project_labels_json
        CHECK (project_labels IS NULL OR JSON_VALID(project_labels)),
    CONSTRAINT ck_gcp_billing_silver_labels_json
        CHECK (labels IS NULL OR JSON_VALID(labels)),
    CONSTRAINT ck_gcp_billing_silver_system_labels_json
        CHECK (system_labels IS NULL OR JSON_VALID(system_labels)),
    CONSTRAINT ck_gcp_billing_silver_tags_json
        CHECK (tags IS NULL OR JSON_VALID(tags)),
    CONSTRAINT ck_gcp_billing_silver_credits_json
        CHECK (credits IS NULL OR JSON_VALID(credits)),
    CONSTRAINT ck_gcp_billing_silver_adjustment_json
        CHECK (adjustment_info IS NULL OR JSON_VALID(adjustment_info))
) ENGINE=InnoDB
DEFAULT CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
