-- AlterSchema
CREATE SCHEMA IF NOT EXISTS "platform_flow_diagram";

-- CreateTable
CREATE TABLE "platform_flow_diagram"."flow_diagram" (
    "id" UUID NOT NULL,
    "title" VARCHAR(100) NOT NULL,
    "description" TEXT,
    "mermaid_source" TEXT NOT NULL,
    "previous_source" TEXT,
    "tags" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "color_preset" VARCHAR(20) NOT NULL DEFAULT 'default',
    "node_count" INTEGER,
    "created_by_id" UUID NOT NULL,
    "organization_id" UUID NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "flow_diagram_organization_id_created_by_id_updated_at_idx" ON "platform_flow_diagram"."flow_diagram"("organization_id", "created_by_id", "updated_at" DESC);

-- CreateIndex
CREATE INDEX "flow_diagram_tags_idx" ON "platform_flow_diagram"."flow_diagram" USING GIN ("tags");

-- AddForeignKey
ALTER TABLE "platform_flow_diagram"."flow_diagram" ADD CONSTRAINT "flow_diagram_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "platform_iam"."users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_flow_diagram"."flow_diagram" ADD CONSTRAINT "flow_diagram_organization_id_fkey" FOREIGN KEY ("organization_id") REFERENCES "corp_hr"."organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
