-- Generated by aether-data-schema from schema/logical/*.toml.
-- Do not edit generated files directly; edit logical schema or explicit overrides instead.

CREATE TABLE IF NOT EXISTS stats_hourly (
    `id` VARCHAR(64) NOT NULL,
    `hour_utc` BIGINT NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `success_requests` BIGINT NOT NULL DEFAULT 0,
    `error_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `actual_total_cost` DOUBLE NOT NULL DEFAULT 0,
    `avg_response_time_ms` DOUBLE NOT NULL DEFAULT 0,
    `is_complete` TINYINT(1) NOT NULL DEFAULT 0,
    `aggregated_at` BIGINT,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_hourly_hour (`hour_utc`)
);

CREATE TABLE IF NOT EXISTS stats_summary (
    `id` VARCHAR(64) NOT NULL,
    `cutoff_date` BIGINT NOT NULL,
    `all_time_requests` BIGINT NOT NULL DEFAULT 0,
    `all_time_success_requests` BIGINT NOT NULL DEFAULT 0,
    `all_time_error_requests` BIGINT NOT NULL DEFAULT 0,
    `all_time_input_tokens` BIGINT NOT NULL DEFAULT 0,
    `all_time_output_tokens` BIGINT NOT NULL DEFAULT 0,
    `all_time_cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `all_time_cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `all_time_cost` DOUBLE NOT NULL DEFAULT 0,
    `all_time_actual_cost` DOUBLE NOT NULL DEFAULT 0,
    `total_users` BIGINT NOT NULL DEFAULT 0,
    `active_users` BIGINT NOT NULL DEFAULT 0,
    `total_api_keys` BIGINT NOT NULL DEFAULT 0,
    `active_api_keys` BIGINT NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS stats_hourly_user (
    `id` VARCHAR(64) NOT NULL,
    `hour_utc` BIGINT NOT NULL,
    `user_id` VARCHAR(64) NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `success_requests` BIGINT NOT NULL DEFAULT 0,
    `error_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_hourly_user (`hour_utc`, `user_id`)
);

CREATE TABLE IF NOT EXISTS stats_hourly_user_model (
    `id` VARCHAR(64) NOT NULL,
    `hour_utc` BIGINT NOT NULL,
    `user_id` VARCHAR(64) NOT NULL,
    `model` VARCHAR(255) NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_hourly_user_model (`hour_utc`, `user_id`, `model`)
);

CREATE TABLE IF NOT EXISTS user_model_usage_counts (
    `id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64) NOT NULL,
    `model` VARCHAR(255) NOT NULL,
    `usage_count` BIGINT NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_user_model_usage_count (`user_id`, `model`),
    KEY idx_user_model_usage_user (`user_id`),
    KEY idx_user_model_usage_model (`model`)
);

CREATE TABLE IF NOT EXISTS stats_hourly_model (
    `id` VARCHAR(64) NOT NULL,
    `hour_utc` BIGINT NOT NULL,
    `model` VARCHAR(255) NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `avg_response_time_ms` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_hourly_model (`hour_utc`, `model`)
);

CREATE TABLE IF NOT EXISTS stats_hourly_provider (
    `id` VARCHAR(64) NOT NULL,
    `hour_utc` BIGINT NOT NULL,
    `provider_name` VARCHAR(255) NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_hourly_provider (`hour_utc`, `provider_name`)
);

CREATE TABLE IF NOT EXISTS stats_daily (
    `id` VARCHAR(64) NOT NULL,
    `date` BIGINT NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `success_requests` BIGINT NOT NULL DEFAULT 0,
    `error_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `actual_total_cost` DOUBLE NOT NULL DEFAULT 0,
    `input_cost` DOUBLE NOT NULL DEFAULT 0,
    `output_cost` DOUBLE NOT NULL DEFAULT 0,
    `cache_creation_cost` DOUBLE NOT NULL DEFAULT 0,
    `cache_read_cost` DOUBLE NOT NULL DEFAULT 0,
    `avg_response_time_ms` DOUBLE NOT NULL DEFAULT 0,
    `fallback_count` BIGINT NOT NULL DEFAULT 0,
    `unique_models` BIGINT NOT NULL DEFAULT 0,
    `unique_providers` BIGINT NOT NULL DEFAULT 0,
    `is_complete` TINYINT(1) NOT NULL DEFAULT 0,
    `aggregated_at` BIGINT,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    `p50_response_time_ms` BIGINT,
    `p90_response_time_ms` BIGINT,
    `p99_response_time_ms` BIGINT,
    `p50_first_byte_time_ms` BIGINT,
    `p90_first_byte_time_ms` BIGINT,
    `p99_first_byte_time_ms` BIGINT,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_daily_date (`date`)
);

CREATE TABLE IF NOT EXISTS stats_daily_model (
    `id` VARCHAR(64) NOT NULL,
    `date` BIGINT NOT NULL,
    `model` VARCHAR(255) NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `avg_response_time_ms` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_daily_model (`date`, `model`)
);

CREATE TABLE IF NOT EXISTS stats_daily_provider (
    `id` VARCHAR(64) NOT NULL,
    `date` BIGINT NOT NULL,
    `provider_name` VARCHAR(255) NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_daily_provider (`date`, `provider_name`)
);

CREATE TABLE IF NOT EXISTS stats_daily_api_key (
    `id` VARCHAR(64) NOT NULL,
    `api_key_id` VARCHAR(64) NOT NULL,
    `date` BIGINT NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `success_requests` BIGINT NOT NULL DEFAULT 0,
    `error_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `api_key_name` VARCHAR(255),
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_daily_api_key (`date`, `api_key_id`)
);

CREATE TABLE IF NOT EXISTS stats_daily_error (
    `id` VARCHAR(64) NOT NULL,
    `date` BIGINT NOT NULL,
    `error_category` VARCHAR(255) NOT NULL,
    `provider_name` VARCHAR(255),
    `model` VARCHAR(255),
    `count` BIGINT NOT NULL DEFAULT 0,
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_daily_error (`date`, `error_category`, `provider_name`, `model`)
);

CREATE TABLE IF NOT EXISTS stats_user_daily (
    `id` VARCHAR(64) NOT NULL,
    `user_id` VARCHAR(64) NOT NULL,
    `date` BIGINT NOT NULL,
    `total_requests` BIGINT NOT NULL DEFAULT 0,
    `success_requests` BIGINT NOT NULL DEFAULT 0,
    `error_requests` BIGINT NOT NULL DEFAULT 0,
    `input_tokens` BIGINT NOT NULL DEFAULT 0,
    `output_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_creation_tokens` BIGINT NOT NULL DEFAULT 0,
    `cache_read_tokens` BIGINT NOT NULL DEFAULT 0,
    `total_cost` DOUBLE NOT NULL DEFAULT 0,
    `username` VARCHAR(255),
    `created_at` BIGINT NOT NULL,
    `updated_at` BIGINT NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY uq_stats_user_daily (`date`, `user_id`)
);

