-- CreateTable
CREATE TABLE "platform_iam"."ai_tool_grants" (
    "id" UUID NOT NULL,
    "role_id" UUID NOT NULL,
    "tool_name" VARCHAR(128) NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by" UUID,

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

-- CreateTable
CREATE TABLE "platform_iam"."ai_tool_grants_user" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "tool_name" VARCHAR(128) NOT NULL,
    "reason" VARCHAR(500),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,
    "created_by" UUID,

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

-- CreateIndex
CREATE INDEX "ai_tool_grants_tool_name_idx" ON "platform_iam"."ai_tool_grants"("tool_name");

-- CreateIndex
CREATE UNIQUE INDEX "ai_tool_grants_role_id_tool_name_key" ON "platform_iam"."ai_tool_grants"("role_id", "tool_name");

-- CreateIndex
CREATE INDEX "ai_tool_grants_user_tool_name_idx" ON "platform_iam"."ai_tool_grants_user"("tool_name");

-- CreateIndex
CREATE UNIQUE INDEX "ai_tool_grants_user_user_id_tool_name_key" ON "platform_iam"."ai_tool_grants_user"("user_id", "tool_name");

-- AddForeignKey
ALTER TABLE "platform_iam"."ai_tool_grants" ADD CONSTRAINT "ai_tool_grants_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "platform_iam"."roles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_iam"."ai_tool_grants_user" ADD CONSTRAINT "ai_tool_grants_user_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "platform_iam"."users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
