-- 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 public.stats_hourly (
    id character varying(64) NOT NULL,
    hour_utc bigint NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    success_requests bigint DEFAULT 0 NOT NULL,
    error_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    actual_total_cost double precision DEFAULT 0 NOT NULL,
    avg_response_time_ms double precision DEFAULT 0 NOT NULL,
    is_complete boolean DEFAULT false NOT NULL,
    aggregated_at bigint,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_hourly ADD CONSTRAINT stats_hourly_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_hourly ADD CONSTRAINT uq_stats_hourly_hour UNIQUE (hour_utc);

CREATE TABLE IF NOT EXISTS public.stats_summary (
    id character varying(64) NOT NULL,
    cutoff_date bigint NOT NULL,
    all_time_requests bigint DEFAULT 0 NOT NULL,
    all_time_success_requests bigint DEFAULT 0 NOT NULL,
    all_time_error_requests bigint DEFAULT 0 NOT NULL,
    all_time_input_tokens bigint DEFAULT 0 NOT NULL,
    all_time_output_tokens bigint DEFAULT 0 NOT NULL,
    all_time_cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    all_time_cache_read_tokens bigint DEFAULT 0 NOT NULL,
    all_time_cost double precision DEFAULT 0 NOT NULL,
    all_time_actual_cost double precision DEFAULT 0 NOT NULL,
    total_users bigint DEFAULT 0 NOT NULL,
    active_users bigint DEFAULT 0 NOT NULL,
    total_api_keys bigint DEFAULT 0 NOT NULL,
    active_api_keys bigint DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_summary ADD CONSTRAINT stats_summary_pkey PRIMARY KEY (id);

CREATE TABLE IF NOT EXISTS public.stats_hourly_user (
    id character varying(64) NOT NULL,
    hour_utc bigint NOT NULL,
    user_id character varying(64) NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    success_requests bigint DEFAULT 0 NOT NULL,
    error_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_hourly_user ADD CONSTRAINT stats_hourly_user_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_hourly_user ADD CONSTRAINT uq_stats_hourly_user UNIQUE (hour_utc, user_id);

CREATE TABLE IF NOT EXISTS public.stats_hourly_user_model (
    id character varying(64) NOT NULL,
    hour_utc bigint NOT NULL,
    user_id character varying(64) NOT NULL,
    model character varying(255) NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_hourly_user_model ADD CONSTRAINT stats_hourly_user_model_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_hourly_user_model ADD CONSTRAINT uq_stats_hourly_user_model UNIQUE (hour_utc, user_id, model);

CREATE TABLE IF NOT EXISTS public.user_model_usage_counts (
    id character varying(64) NOT NULL,
    user_id character varying(64) NOT NULL,
    model character varying(255) NOT NULL,
    usage_count bigint DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.user_model_usage_counts ADD CONSTRAINT user_model_usage_counts_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.user_model_usage_counts ADD CONSTRAINT uq_user_model_usage_count UNIQUE (user_id, model);
CREATE INDEX IF NOT EXISTS idx_user_model_usage_user ON public.user_model_usage_counts USING btree (user_id);
CREATE INDEX IF NOT EXISTS idx_user_model_usage_model ON public.user_model_usage_counts USING btree (model);

CREATE TABLE IF NOT EXISTS public.stats_hourly_model (
    id character varying(64) NOT NULL,
    hour_utc bigint NOT NULL,
    model character varying(255) NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    avg_response_time_ms double precision DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_hourly_model ADD CONSTRAINT stats_hourly_model_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_hourly_model ADD CONSTRAINT uq_stats_hourly_model UNIQUE (hour_utc, model);

CREATE TABLE IF NOT EXISTS public.stats_hourly_provider (
    id character varying(64) NOT NULL,
    hour_utc bigint NOT NULL,
    provider_name character varying(255) NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_hourly_provider ADD CONSTRAINT stats_hourly_provider_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_hourly_provider ADD CONSTRAINT uq_stats_hourly_provider UNIQUE (hour_utc, provider_name);

CREATE TABLE IF NOT EXISTS public.stats_daily (
    id character varying(64) NOT NULL,
    date bigint NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    success_requests bigint DEFAULT 0 NOT NULL,
    error_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    actual_total_cost double precision DEFAULT 0 NOT NULL,
    input_cost double precision DEFAULT 0 NOT NULL,
    output_cost double precision DEFAULT 0 NOT NULL,
    cache_creation_cost double precision DEFAULT 0 NOT NULL,
    cache_read_cost double precision DEFAULT 0 NOT NULL,
    avg_response_time_ms double precision DEFAULT 0 NOT NULL,
    fallback_count bigint DEFAULT 0 NOT NULL,
    unique_models bigint DEFAULT 0 NOT NULL,
    unique_providers bigint DEFAULT 0 NOT NULL,
    is_complete boolean DEFAULT false NOT NULL,
    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
);

ALTER TABLE ONLY public.stats_daily ADD CONSTRAINT stats_daily_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_daily ADD CONSTRAINT uq_stats_daily_date UNIQUE (date);

CREATE TABLE IF NOT EXISTS public.stats_daily_model (
    id character varying(64) NOT NULL,
    date bigint NOT NULL,
    model character varying(255) NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    avg_response_time_ms double precision DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_daily_model ADD CONSTRAINT stats_daily_model_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_daily_model ADD CONSTRAINT uq_stats_daily_model UNIQUE (date, model);

CREATE TABLE IF NOT EXISTS public.stats_daily_provider (
    id character varying(64) NOT NULL,
    date bigint NOT NULL,
    provider_name character varying(255) NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_daily_provider ADD CONSTRAINT stats_daily_provider_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_daily_provider ADD CONSTRAINT uq_stats_daily_provider UNIQUE (date, provider_name);

CREATE TABLE IF NOT EXISTS public.stats_daily_api_key (
    id character varying(64) NOT NULL,
    api_key_id character varying(64) NOT NULL,
    date bigint NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    success_requests bigint DEFAULT 0 NOT NULL,
    error_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    api_key_name character varying(255),
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_daily_api_key ADD CONSTRAINT stats_daily_api_key_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_daily_api_key ADD CONSTRAINT uq_stats_daily_api_key UNIQUE (date, api_key_id);

CREATE TABLE IF NOT EXISTS public.stats_daily_error (
    id character varying(64) NOT NULL,
    date bigint NOT NULL,
    error_category character varying(255) NOT NULL,
    provider_name character varying(255),
    model character varying(255),
    count bigint DEFAULT 0 NOT NULL,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_daily_error ADD CONSTRAINT stats_daily_error_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_daily_error ADD CONSTRAINT uq_stats_daily_error UNIQUE (date, error_category, provider_name, model);

CREATE TABLE IF NOT EXISTS public.stats_user_daily (
    id character varying(64) NOT NULL,
    user_id character varying(64) NOT NULL,
    date bigint NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    success_requests bigint DEFAULT 0 NOT NULL,
    error_requests bigint DEFAULT 0 NOT NULL,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_tokens bigint DEFAULT 0 NOT NULL,
    total_cost double precision DEFAULT 0 NOT NULL,
    username character varying(255),
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.stats_user_daily ADD CONSTRAINT stats_user_daily_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.stats_user_daily ADD CONSTRAINT uq_stats_user_daily UNIQUE (date, user_id);

