CREATE TABLE IF NOT EXISTS gateway_debug_logs (
    id                    BIGSERIAL PRIMARY KEY,
    upstream_request_id   TEXT,
    account_id            BIGINT,
    account_email         TEXT,
    account_platform      TEXT,
    event_type            TEXT NOT NULL DEFAULT 'api_call',
    method                TEXT,
    full_url              TEXT,
    request_headers       TEXT,
    request_body          TEXT,
    request_size          INTEGER,
    response_status       INTEGER,
    response_headers      TEXT,
    response_body_preview TEXT,
    response_size         INTEGER,
    model_requested       TEXT,
    model_upstream        TEXT,
    is_stream             BOOLEAN NOT NULL DEFAULT FALSE,
    duration_ms           INTEGER,
    tls_profile           TEXT,
    error_message         TEXT,
    created_at            TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS upstream_request_id TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS account_id BIGINT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS account_email TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS account_platform TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS event_type TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS method TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS full_url TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS request_headers TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS request_body TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS request_size INTEGER;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS response_status INTEGER;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS response_headers TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS response_body_preview TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS response_size INTEGER;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS model_requested TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS model_upstream TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS is_stream BOOLEAN;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS duration_ms INTEGER;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS tls_profile TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS error_message TEXT;
ALTER TABLE gateway_debug_logs ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ;

UPDATE gateway_debug_logs
SET event_type = 'api_call'
WHERE event_type IS NULL;

UPDATE gateway_debug_logs
SET is_stream = FALSE
WHERE is_stream IS NULL;

UPDATE gateway_debug_logs
SET created_at = NOW()
WHERE created_at IS NULL;

ALTER TABLE gateway_debug_logs ALTER COLUMN event_type SET DEFAULT 'api_call';
ALTER TABLE gateway_debug_logs ALTER COLUMN event_type SET NOT NULL;
ALTER TABLE gateway_debug_logs ALTER COLUMN is_stream SET DEFAULT FALSE;
ALTER TABLE gateway_debug_logs ALTER COLUMN is_stream SET NOT NULL;
ALTER TABLE gateway_debug_logs ALTER COLUMN created_at SET DEFAULT NOW();
ALTER TABLE gateway_debug_logs ALTER COLUMN created_at SET NOT NULL;

CREATE INDEX IF NOT EXISTS idx_gdl_account_id ON gateway_debug_logs (account_id);
CREATE INDEX IF NOT EXISTS idx_gdl_created_at ON gateway_debug_logs (created_at);
CREATE INDEX IF NOT EXISTS idx_gdl_event_type ON gateway_debug_logs (event_type);
CREATE INDEX IF NOT EXISTS idx_gdl_model ON gateway_debug_logs (model_requested);
