-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "ssa_schema";

-- CreateTable: ssa_sessions
CREATE TABLE "ssa_schema"."ssa_sessions" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "title" TEXT,
    "data_schema" JSONB,
    "data_payload" JSONB,
    "data_oss_key" TEXT,
    "status" TEXT NOT NULL DEFAULT 'active',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: ssa_messages
CREATE TABLE "ssa_schema"."ssa_messages" (
    "id" TEXT NOT NULL,
    "session_id" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "content_type" TEXT NOT NULL,
    "content" JSONB NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: ssa_tools_library
CREATE TABLE "ssa_schema"."ssa_tools_library" (
    "id" TEXT NOT NULL,
    "tool_code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "version" TEXT NOT NULL DEFAULT '1.0.0',
    "description" TEXT NOT NULL,
    "usage_context" TEXT,
    "params_schema" JSONB NOT NULL,
    "guardrails" JSONB,
    "search_text" TEXT NOT NULL,
    "embedding" vector(1024),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: ssa_execution_logs
CREATE TABLE "ssa_schema"."ssa_execution_logs" (
    "id" TEXT NOT NULL,
    "session_id" TEXT NOT NULL,
    "message_id" TEXT,
    "tool_code" TEXT NOT NULL,
    "input_params" JSONB NOT NULL,
    "output_status" TEXT NOT NULL,
    "output_result" JSONB,
    "trace_log" TEXT[],
    "execution_ms" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: ssa_decision_table
CREATE TABLE "ssa_schema"."ssa_decision_table" (
    "id" TEXT NOT NULL,
    "goal_type" TEXT NOT NULL,
    "y_type" TEXT NOT NULL,
    "x_type" TEXT,
    "design_type" TEXT NOT NULL,
    "tool_code" TEXT NOT NULL,
    "alt_tool_code" TEXT,
    "priority" INTEGER NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: ssa_r_code_library
CREATE TABLE "ssa_schema"."ssa_r_code_library" (
    "id" TEXT NOT NULL,
    "tool_code" TEXT NOT NULL,
    "version" TEXT NOT NULL DEFAULT '1.0.0',
    "file_name" TEXT NOT NULL,
    "code_content" TEXT NOT NULL,
    "entry_func" TEXT NOT NULL DEFAULT 'run_analysis',
    "description" TEXT,
    "dependencies" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: ssa_param_mapping
CREATE TABLE "ssa_schema"."ssa_param_mapping" (
    "id" TEXT NOT NULL,
    "tool_code" TEXT NOT NULL,
    "json_key" TEXT NOT NULL,
    "r_param_name" TEXT NOT NULL,
    "data_type" TEXT NOT NULL,
    "is_required" BOOLEAN NOT NULL DEFAULT false,
    "default_value" TEXT,
    "validation_rule" TEXT,
    "description" TEXT,

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

-- CreateTable: ssa_guardrail_config
CREATE TABLE "ssa_schema"."ssa_guardrail_config" (
    "id" TEXT NOT NULL,
    "tool_code" TEXT NOT NULL,
    "check_name" TEXT NOT NULL,
    "check_order" INTEGER NOT NULL DEFAULT 0,
    "check_code" TEXT NOT NULL,
    "threshold" TEXT,
    "action_type" TEXT NOT NULL,
    "action_target" TEXT,
    "is_enabled" BOOLEAN NOT NULL DEFAULT true,

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

-- CreateTable: ssa_interpretation_templates
CREATE TABLE "ssa_schema"."ssa_interpretation_templates" (
    "id" TEXT NOT NULL,
    "tool_code" TEXT NOT NULL,
    "scenario_key" TEXT NOT NULL,
    "template" TEXT NOT NULL,
    "placeholders" TEXT[] DEFAULT ARRAY[]::TEXT[],

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

-- CreateIndex
CREATE INDEX "idx_ssa_session_user" ON "ssa_schema"."ssa_sessions"("user_id");
CREATE INDEX "idx_ssa_session_status" ON "ssa_schema"."ssa_sessions"("status");

CREATE INDEX "idx_ssa_message_session" ON "ssa_schema"."ssa_messages"("session_id");

CREATE UNIQUE INDEX "ssa_tools_library_tool_code_key" ON "ssa_schema"."ssa_tools_library"("tool_code");

CREATE INDEX "idx_ssa_exec_log_session" ON "ssa_schema"."ssa_execution_logs"("session_id");
CREATE INDEX "idx_ssa_exec_log_tool" ON "ssa_schema"."ssa_execution_logs"("tool_code");

CREATE UNIQUE INDEX "uq_ssa_decision_table" ON "ssa_schema"."ssa_decision_table"("goal_type", "y_type", "x_type", "design_type");

CREATE INDEX "idx_ssa_rcode_tool" ON "ssa_schema"."ssa_r_code_library"("tool_code");

CREATE UNIQUE INDEX "uq_ssa_param_mapping" ON "ssa_schema"."ssa_param_mapping"("tool_code", "json_key");

CREATE INDEX "idx_ssa_guardrail_tool" ON "ssa_schema"."ssa_guardrail_config"("tool_code");

CREATE UNIQUE INDEX "uq_ssa_interpretation" ON "ssa_schema"."ssa_interpretation_templates"("tool_code", "scenario_key");

-- AddForeignKey
ALTER TABLE "ssa_schema"."ssa_messages" ADD CONSTRAINT "ssa_messages_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "ssa_schema"."ssa_sessions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "ssa_schema"."ssa_execution_logs" ADD CONSTRAINT "ssa_execution_logs_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "ssa_schema"."ssa_sessions"("id") ON DELETE CASCADE ON UPDATE CASCADE;
