-- 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.proxy_nodes (
    id character varying(64) NOT NULL,
    name character varying(255) NOT NULL,
    ip character varying(512) NOT NULL,
    port integer NOT NULL,
    region character varying(100),
    status character varying(32) DEFAULT 'online' NOT NULL,
    registered_by character varying(64),
    last_heartbeat_at bigint,
    heartbeat_interval integer DEFAULT 30 NOT NULL,
    active_connections integer DEFAULT 0 NOT NULL,
    total_requests bigint DEFAULT 0 NOT NULL,
    avg_latency_ms double precision,
    is_manual boolean DEFAULT false NOT NULL,
    proxy_url character varying(500),
    proxy_username character varying(255),
    proxy_password character varying(500),
    created_at bigint NOT NULL,
    updated_at bigint NOT NULL,
    remote_config jsonb,
    config_version integer DEFAULT 0 NOT NULL,
    hardware_info jsonb,
    estimated_max_concurrency integer,
    tunnel_mode boolean DEFAULT false NOT NULL,
    tunnel_connected boolean DEFAULT false NOT NULL,
    tunnel_connected_at bigint,
    failed_requests bigint DEFAULT 0 NOT NULL,
    dns_failures bigint DEFAULT 0 NOT NULL,
    stream_errors bigint DEFAULT 0 NOT NULL,
    proxy_metadata jsonb
);

ALTER TABLE ONLY public.proxy_nodes ADD CONSTRAINT proxy_nodes_pkey PRIMARY KEY (id);

CREATE TABLE IF NOT EXISTS public.proxy_node_events (
    id bigserial NOT NULL,
    node_id character varying(64) NOT NULL,
    event_type character varying(64) NOT NULL,
    detail character varying(500),
    event_metadata jsonb,
    created_at bigint NOT NULL
);

ALTER TABLE ONLY public.proxy_node_events ADD CONSTRAINT proxy_node_events_pkey PRIMARY KEY (id);

CREATE TABLE IF NOT EXISTS public.proxy_node_metrics_1m (
    node_id character varying(64) NOT NULL,
    bucket_start_unix_secs bigint NOT NULL,
    samples bigint DEFAULT 0 NOT NULL,
    uptime_samples bigint DEFAULT 0 NOT NULL,
    active_connections_sum bigint DEFAULT 0 NOT NULL,
    active_connections_max bigint DEFAULT 0 NOT NULL,
    heartbeat_rtt_ms_sum bigint DEFAULT 0 NOT NULL,
    heartbeat_rtt_ms_max bigint DEFAULT 0 NOT NULL,
    connect_errors_delta bigint DEFAULT 0 NOT NULL,
    disconnects_delta bigint DEFAULT 0 NOT NULL,
    error_events_delta bigint DEFAULT 0 NOT NULL,
    ws_in_bytes_delta bigint DEFAULT 0 NOT NULL,
    ws_out_bytes_delta bigint DEFAULT 0 NOT NULL,
    ws_in_frames_delta bigint DEFAULT 0 NOT NULL,
    ws_out_frames_delta bigint DEFAULT 0 NOT NULL
);

ALTER TABLE ONLY public.proxy_node_metrics_1m ADD CONSTRAINT proxy_node_metrics_1m_pkey PRIMARY KEY (node_id, bucket_start_unix_secs);
CREATE INDEX IF NOT EXISTS idx_proxy_node_metrics_1m_bucket_start ON public.proxy_node_metrics_1m USING btree (bucket_start_unix_secs);

CREATE TABLE IF NOT EXISTS public.proxy_node_metrics_1h (
    node_id character varying(64) NOT NULL,
    bucket_start_unix_secs bigint NOT NULL,
    samples bigint DEFAULT 0 NOT NULL,
    uptime_samples bigint DEFAULT 0 NOT NULL,
    active_connections_sum bigint DEFAULT 0 NOT NULL,
    active_connections_max bigint DEFAULT 0 NOT NULL,
    heartbeat_rtt_ms_sum bigint DEFAULT 0 NOT NULL,
    heartbeat_rtt_ms_max bigint DEFAULT 0 NOT NULL,
    connect_errors_delta bigint DEFAULT 0 NOT NULL,
    disconnects_delta bigint DEFAULT 0 NOT NULL,
    error_events_delta bigint DEFAULT 0 NOT NULL,
    ws_in_bytes_delta bigint DEFAULT 0 NOT NULL,
    ws_out_bytes_delta bigint DEFAULT 0 NOT NULL,
    ws_in_frames_delta bigint DEFAULT 0 NOT NULL,
    ws_out_frames_delta bigint DEFAULT 0 NOT NULL
);

ALTER TABLE ONLY public.proxy_node_metrics_1h ADD CONSTRAINT proxy_node_metrics_1h_pkey PRIMARY KEY (node_id, bucket_start_unix_secs);
CREATE INDEX IF NOT EXISTS idx_proxy_node_metrics_1h_bucket_start ON public.proxy_node_metrics_1h USING btree (bucket_start_unix_secs);

