-- Migration: 20260313_deal_type
-- Beschreibung: Vorgangsart (DealType) Tabelle + deal_type_id auf deals

-- CreateTable
CREATE TABLE "app_crm"."deal_types" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "tenant_id" UUID NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "color" VARCHAR(7) NOT NULL DEFAULT '#6B7280',
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- AlterTable: deal_type_id auf deals
ALTER TABLE "app_crm"."deals" ADD COLUMN "deal_type_id" UUID;

-- CreateIndex
CREATE UNIQUE INDEX "deal_types_tenant_id_name_key" ON "app_crm"."deal_types"("tenant_id", "name");
CREATE INDEX "deal_types_tenant_id_idx" ON "app_crm"."deal_types"("tenant_id");

-- AddForeignKey
ALTER TABLE "app_crm"."deals" ADD CONSTRAINT "deals_deal_type_id_fkey" FOREIGN KEY ("deal_type_id") REFERENCES "app_crm"."deal_types"("id") ON DELETE SET NULL ON UPDATE CASCADE;
