-- 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.usage (
    request_id character varying(128) NOT NULL,
    id character varying(128),
    user_id character varying(64),
    api_key_id character varying(64),
    provider_name character varying(255) DEFAULT 'unknown' NOT NULL,
    model character varying(255) DEFAULT 'unknown' NOT NULL,
    target_model character varying(255),
    provider_id character varying(64),
    provider_endpoint_id character varying(64),
    provider_api_key_id character varying(64),
    request_type character varying(64),
    api_format character varying(64),
    api_family character varying(64),
    endpoint_kind character varying(64),
    endpoint_api_format character varying(64),
    provider_api_family character varying(64),
    provider_endpoint_kind character varying(64),
    has_format_conversion boolean DEFAULT false NOT NULL,
    is_stream boolean DEFAULT false NOT NULL,
    upstream_is_stream boolean,
    input_tokens bigint DEFAULT 0 NOT NULL,
    output_tokens bigint DEFAULT 0 NOT NULL,
    input_output_total_tokens bigint DEFAULT 0 NOT NULL,
    total_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_input_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_input_tokens_5m bigint DEFAULT 0 NOT NULL,
    cache_creation_input_tokens_1h bigint DEFAULT 0 NOT NULL,
    cache_creation_ephemeral_5m_input_tokens bigint DEFAULT 0 NOT NULL,
    cache_creation_ephemeral_1h_input_tokens bigint DEFAULT 0 NOT NULL,
    cache_read_input_tokens bigint DEFAULT 0 NOT NULL,
    input_context_tokens bigint DEFAULT 0 NOT NULL,
    input_cost_usd double precision DEFAULT 0 NOT NULL,
    output_cost_usd double precision DEFAULT 0 NOT NULL,
    cache_cost_usd double precision DEFAULT 0 NOT NULL,
    cache_creation_cost_usd double precision DEFAULT 0 NOT NULL,
    cache_creation_cost_usd_5m double precision DEFAULT 0 NOT NULL,
    cache_creation_cost_usd_1h double precision DEFAULT 0 NOT NULL,
    cache_read_cost_usd double precision DEFAULT 0 NOT NULL,
    request_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_input_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_output_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_cache_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_cache_creation_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_cache_creation_cost_usd_5m double precision DEFAULT 0 NOT NULL,
    actual_cache_creation_cost_usd_1h double precision DEFAULT 0 NOT NULL,
    actual_cache_read_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_request_cost_usd double precision DEFAULT 0 NOT NULL,
    rate_multiplier double precision DEFAULT 1 NOT NULL,
    input_price_per_1m double precision,
    output_price_per_1m double precision,
    cache_creation_price_per_1m double precision,
    cache_creation_price_per_1m_5m double precision,
    cache_creation_price_per_1m_1h double precision,
    cache_read_price_per_1m double precision,
    price_per_request double precision,
    status_code integer,
    error_message text,
    error_category character varying(255),
    response_time_ms bigint,
    first_byte_time_ms bigint,
    wallet_id character varying(64),
    status character varying(64) DEFAULT 'completed' NOT NULL,
    billing_status character varying(64) DEFAULT 'pending' NOT NULL,
    total_cost_usd double precision DEFAULT 0 NOT NULL,
    actual_total_cost_usd double precision DEFAULT 0 NOT NULL,
    request_headers jsonb,
    request_body jsonb,
    provider_request_headers jsonb,
    provider_request_body jsonb,
    response_headers jsonb,
    response_body jsonb,
    client_response_headers jsonb,
    client_response_body jsonb,
    request_body_compressed bytea,
    provider_request_body_compressed bytea,
    response_body_compressed bytea,
    client_response_body_compressed bytea,
    request_metadata jsonb,
    created_at bigint,
    candidate_id character varying(128),
    candidate_index bigint,
    key_name character varying(255),
    username character varying(255),
    api_key_name character varying(255),
    planner_kind character varying(64),
    route_family character varying(128),
    route_kind character varying(128),
    execution_path character varying(128),
    local_execution_runtime_miss_reason character varying(255),
    wallet_balance_before double precision,
    wallet_balance_after double precision,
    wallet_recharge_balance_before double precision,
    wallet_recharge_balance_after double precision,
    wallet_gift_balance_before double precision,
    wallet_gift_balance_after double precision,
    finalized_at bigint,
    created_at_unix_ms bigint DEFAULT 0 NOT NULL,
    updated_at_unix_secs bigint DEFAULT 0 NOT NULL
);

ALTER TABLE ONLY public.usage ADD CONSTRAINT usage_pkey PRIMARY KEY (request_id);
CREATE INDEX IF NOT EXISTS usage_api_key_id_idx ON public.usage USING btree (api_key_id);
CREATE INDEX IF NOT EXISTS usage_billing_status_idx ON public.usage USING btree (billing_status);
CREATE INDEX IF NOT EXISTS usage_created_at_idx ON public.usage USING btree (created_at_unix_ms);
CREATE INDEX IF NOT EXISTS usage_provider_api_key_id_idx ON public.usage USING btree (provider_api_key_id);
CREATE INDEX IF NOT EXISTS usage_provider_id_idx ON public.usage USING btree (provider_id);
CREATE INDEX IF NOT EXISTS usage_request_id_idx ON public.usage USING btree (request_id);
CREATE INDEX IF NOT EXISTS usage_user_id_idx ON public.usage USING btree (user_id);
CREATE INDEX IF NOT EXISTS usage_wallet_id_idx ON public.usage USING btree (wallet_id);

CREATE TABLE IF NOT EXISTS public.usage_counter_deltas (
    id character varying(36) NOT NULL,
    request_id character varying(128) NOT NULL,
    kind character varying(64) NOT NULL,
    target_id text NOT NULL,
    request_count_delta bigint DEFAULT 0 NOT NULL,
    total_requests_delta bigint DEFAULT 0 NOT NULL,
    success_count_delta bigint DEFAULT 0 NOT NULL,
    error_count_delta bigint DEFAULT 0 NOT NULL,
    dns_failures_delta bigint DEFAULT 0 NOT NULL,
    stream_errors_delta bigint DEFAULT 0 NOT NULL,
    total_tokens_delta bigint DEFAULT 0 NOT NULL,
    total_cost_usd_delta double precision DEFAULT 0 NOT NULL,
    total_response_time_ms_delta bigint DEFAULT 0 NOT NULL,
    last_used_at_unix_secs bigint,
    last_used_ip text,
    candidate_last_used_at_unix_secs bigint,
    removed_last_used_at_unix_secs bigint,
    usage_created_at_unix_secs bigint,
    created_at timestamp with time zone NOT NULL,
    processed_at timestamp with time zone
);

ALTER TABLE ONLY public.usage_counter_deltas ADD CONSTRAINT usage_counter_deltas_pkey PRIMARY KEY (id);
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_unprocessed ON public.usage_counter_deltas USING btree (created_at, id);
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_processed ON public.usage_counter_deltas USING btree (processed_at, created_at, id);
CREATE INDEX IF NOT EXISTS ix_usage_counter_deltas_request_kind ON public.usage_counter_deltas USING btree (request_id, kind, target_id);

CREATE TABLE IF NOT EXISTS public.usage_settlement_snapshots (
    request_id character varying(128) NOT NULL,
    billing_status character varying(64) NOT NULL,
    wallet_id character varying(64),
    wallet_balance_before double precision,
    wallet_balance_after double precision,
    wallet_recharge_balance_before double precision,
    wallet_recharge_balance_after double precision,
    wallet_gift_balance_before double precision,
    wallet_gift_balance_after double precision,
    provider_monthly_used_usd double precision,
    finalized_at bigint,
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL
);

ALTER TABLE ONLY public.usage_settlement_snapshots ADD CONSTRAINT usage_settlement_snapshots_pkey PRIMARY KEY (request_id);
CREATE INDEX IF NOT EXISTS usage_settlement_snapshots_billing_status_idx ON public.usage_settlement_snapshots USING btree (billing_status);
CREATE INDEX IF NOT EXISTS usage_settlement_snapshots_wallet_id_idx ON public.usage_settlement_snapshots USING btree (wallet_id);

