CREATE TABLE IF NOT EXISTS apps (
    id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    name        TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    status      TEXT NOT NULL DEFAULT 'draft',
    repo_owner  TEXT NOT NULL DEFAULT '',
    repo_name   TEXT NOT NULL DEFAULT '',
    preview_url TEXT NOT NULL DEFAULT '',
    created_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS sessions (
    id         UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    app_id     UUID NOT NULL REFERENCES apps(id) ON DELETE CASCADE,
    provider   TEXT NOT NULL DEFAULT 'claude-code',
    status     TEXT NOT NULL DEFAULT 'pending',
    prompt     TEXT NOT NULL DEFAULT '',
    branch     TEXT NOT NULL DEFAULT '',
    pod_name   TEXT NOT NULL DEFAULT '',
    config     JSONB NOT NULL DEFAULT '{}',
    cost_json  JSONB NOT NULL DEFAULT '{}',
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_sessions_app_id ON sessions(app_id);
CREATE INDEX idx_sessions_status ON sessions(status);
CREATE INDEX idx_apps_status ON apps(status);
