-- CreateTable
CREATE TABLE "public"."llm_calls" (
    "id" UUID NOT NULL,
    "trace_id" CHAR(36) NOT NULL,
    "call_type" VARCHAR(50) NOT NULL,
    "model_name" VARCHAR(100) NOT NULL,
    "system_prompt" TEXT NOT NULL,
    "user_prompt" TEXT NOT NULL,
    "messages" JSONB NOT NULL,
    "temperature" DOUBLE PRECISION,
    "n_candidates" INTEGER,
    "max_tokens" INTEGER,
    "raw_response" TEXT,
    "parsed_response" JSONB,
    "prompt_tokens" INTEGER,
    "completion_tokens" INTEGER,
    "total_tokens" INTEGER,
    "llm_cost" DOUBLE PRECISION,
    "latency_ms" INTEGER,
    "status" VARCHAR(20) NOT NULL,
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "error_type" VARCHAR(50),
    "error_message" TEXT,
    "parsing_status" VARCHAR(20),
    "candidates_generated" INTEGER DEFAULT 0,
    "candidates_valid" INTEGER DEFAULT 0,
    "parsing_errors" JSONB,
    "user_id" TEXT,
    "python_version" VARCHAR(20),
    "is_async" BOOLEAN NOT NULL DEFAULT false,
    "context" JSONB,
    "created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "llm_calls_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."optimization_errors" (
    "id" TEXT NOT NULL,
    "trace_id" CHAR(36) NOT NULL,
    "llm_call_id" UUID,
    "error_type" VARCHAR(50) NOT NULL,
    "error_category" VARCHAR(50) NOT NULL,
    "severity" VARCHAR(20) NOT NULL,
    "error_message" TEXT NOT NULL,
    "error_code" VARCHAR(50),
    "stack_trace" TEXT,
    "context" JSONB,
    "created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "optimization_errors_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "llm_calls_trace_id_idx" ON "public"."llm_calls"("trace_id");

-- CreateIndex
CREATE INDEX "llm_calls_call_type_idx" ON "public"."llm_calls"("call_type");

-- CreateIndex
CREATE INDEX "llm_calls_model_name_idx" ON "public"."llm_calls"("model_name");

-- CreateIndex
CREATE INDEX "llm_calls_status_idx" ON "public"."llm_calls"("status");

-- CreateIndex
CREATE INDEX "llm_calls_created_at_idx" ON "public"."llm_calls"("created_at" DESC);

-- CreateIndex
CREATE INDEX "llm_calls_call_type_status_idx" ON "public"."llm_calls"("call_type", "status");

-- CreateIndex
CREATE INDEX "llm_calls_parsing_status_idx" ON "public"."llm_calls"("parsing_status");

-- CreateIndex
CREATE INDEX "optimization_errors_trace_id_idx" ON "public"."optimization_errors"("trace_id");

-- CreateIndex
CREATE INDEX "optimization_errors_error_type_idx" ON "public"."optimization_errors"("error_type");

-- CreateIndex
CREATE INDEX "optimization_errors_error_category_idx" ON "public"."optimization_errors"("error_category");

-- CreateIndex
CREATE INDEX "optimization_errors_severity_idx" ON "public"."optimization_errors"("severity");

-- CreateIndex
CREATE INDEX "optimization_errors_llm_call_id_idx" ON "public"."optimization_errors"("llm_call_id");

-- AddForeignKey
ALTER TABLE "public"."optimization_errors" ADD CONSTRAINT "optimization_errors_llm_call_id_fkey" FOREIGN KEY ("llm_call_id") REFERENCES "public"."llm_calls"("id") ON DELETE SET NULL ON UPDATE CASCADE;
