CREATE TABLE IF NOT EXISTS `usage` (
    request_id VARCHAR(128) PRIMARY KEY,
    id VARCHAR(128),
    user_id VARCHAR(64),
    api_key_id VARCHAR(64),
    provider_name VARCHAR(255) NOT NULL DEFAULT 'unknown',
    model VARCHAR(255) NOT NULL DEFAULT 'unknown',
    target_model VARCHAR(255),
    provider_id VARCHAR(64),
    provider_endpoint_id VARCHAR(64),
    provider_api_key_id VARCHAR(64),
    request_type VARCHAR(64),
    api_format VARCHAR(64),
    api_family VARCHAR(64),
    endpoint_kind VARCHAR(64),
    endpoint_api_format VARCHAR(64),
    provider_api_family VARCHAR(64),
    provider_endpoint_kind VARCHAR(64),
    has_format_conversion TINYINT(1) NOT NULL DEFAULT 0,
    is_stream TINYINT(1) NOT NULL DEFAULT 0,
    input_tokens BIGINT NOT NULL DEFAULT 0,
    output_tokens BIGINT NOT NULL DEFAULT 0,
    total_tokens BIGINT NOT NULL DEFAULT 0,
    cache_creation_input_tokens BIGINT NOT NULL DEFAULT 0,
    cache_creation_ephemeral_5m_input_tokens BIGINT NOT NULL DEFAULT 0,
    cache_creation_ephemeral_1h_input_tokens BIGINT NOT NULL DEFAULT 0,
    cache_read_input_tokens BIGINT NOT NULL DEFAULT 0,
    cache_creation_cost_usd DOUBLE NOT NULL DEFAULT 0,
    cache_read_cost_usd DOUBLE NOT NULL DEFAULT 0,
    output_price_per_1m DOUBLE,
    status_code INT,
    error_message TEXT,
    error_category VARCHAR(255),
    response_time_ms BIGINT,
    first_byte_time_ms BIGINT,
    wallet_id VARCHAR(64),
    status VARCHAR(64) NOT NULL DEFAULT 'completed',
    billing_status VARCHAR(64) NOT NULL DEFAULT 'pending',
    total_cost_usd DOUBLE NOT NULL DEFAULT 0,
    actual_total_cost_usd DOUBLE NOT NULL DEFAULT 0,
    request_metadata TEXT,
    candidate_id VARCHAR(128),
    candidate_index BIGINT,
    key_name VARCHAR(255),
    planner_kind VARCHAR(64),
    route_family VARCHAR(128),
    route_kind VARCHAR(128),
    execution_path VARCHAR(128),
    local_execution_runtime_miss_reason VARCHAR(255),
    wallet_balance_before DOUBLE,
    wallet_balance_after DOUBLE,
    wallet_recharge_balance_before DOUBLE,
    wallet_recharge_balance_after DOUBLE,
    wallet_gift_balance_before DOUBLE,
    wallet_gift_balance_after DOUBLE,
    finalized_at BIGINT,
    created_at_unix_ms BIGINT NOT NULL DEFAULT 0,
    updated_at_unix_secs BIGINT NOT NULL DEFAULT 0,
    KEY usage_api_key_id_idx (api_key_id),
    KEY usage_billing_status_idx (billing_status),
    KEY usage_created_at_idx (created_at_unix_ms),
    KEY usage_provider_api_key_id_idx (provider_api_key_id),
    KEY usage_provider_id_idx (provider_id),
    KEY usage_request_id_idx (request_id),
    KEY usage_user_id_idx (user_id),
    KEY usage_wallet_id_idx (wallet_id)
);

CREATE TABLE IF NOT EXISTS usage_settlement_snapshots (
    request_id VARCHAR(128) PRIMARY KEY,
    billing_status VARCHAR(64) NOT NULL,
    wallet_id VARCHAR(64),
    wallet_balance_before DOUBLE,
    wallet_balance_after DOUBLE,
    wallet_recharge_balance_before DOUBLE,
    wallet_recharge_balance_after DOUBLE,
    wallet_gift_balance_before DOUBLE,
    wallet_gift_balance_after DOUBLE,
    provider_monthly_used_usd DOUBLE,
    finalized_at BIGINT,
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    KEY usage_settlement_snapshots_billing_status_idx (billing_status),
    KEY usage_settlement_snapshots_wallet_id_idx (wallet_id)
);
