CREATE TYPE "ReferralAgentStatus" AS ENUM ('ACTIVE', 'BLOCKED');
CREATE TYPE "ReferralAgentBotState" AS ENUM ('IDLE', 'WAITING_CAMPAIGN_NAME');
CREATE TYPE "ReferralPlatform" AS ENUM ('TELEGRAM', 'INSTAGRAM', 'WEBSITE', 'HOTEL', 'PRINT', 'OTHER');
CREATE TYPE "ReferralAssetKind" AS ENUM ('LINK', 'QR');
CREATE TYPE "ReferralTrafficKind" AS ENUM ('HUMAN_OR_UNKNOWN', 'PREVIEW_BOT');
CREATE TYPE "ReferralStartStatus" AS ENUM ('PENDING_IDENTITY', 'EXISTING_LOCAL', 'EXISTING_CRM', 'ATTRIBUTED');
ALTER TYPE "OutboxKind" ADD VALUE 'REFERRAL_ATTRIBUTION_CHECK';

CREATE TABLE "ReferralAgent" (
  "id" TEXT NOT NULL,
  "telegramUserId" TEXT NOT NULL,
  "username" TEXT,
  "firstName" TEXT,
  "lastName" TEXT,
  "phoneNumber" TEXT,
  "status" "ReferralAgentStatus" NOT NULL DEFAULT 'ACTIVE',
  "botState" "ReferralAgentBotState" NOT NULL DEFAULT 'IDLE',
  "draftPlatform" "ReferralPlatform",
  "registeredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "lastInteractionAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "ReferralAgent_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "ReferralCampaign" (
  "id" TEXT NOT NULL,
  "agentId" TEXT NOT NULL,
  "name" TEXT NOT NULL,
  "platform" "ReferralPlatform" NOT NULL,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "ReferralCampaign_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "ReferralAsset" (
  "id" TEXT NOT NULL,
  "campaignId" TEXT NOT NULL,
  "code" VARCHAR(24) NOT NULL,
  "kind" "ReferralAssetKind" NOT NULL,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "ReferralAsset_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "ReferralVisit" (
  "id" TEXT NOT NULL,
  "assetId" TEXT NOT NULL,
  "visitorKey" VARCHAR(32) NOT NULL,
  "trafficKind" "ReferralTrafficKind" NOT NULL DEFAULT 'HUMAN_OR_UNKNOWN',
  "referrerHost" TEXT,
  "occurredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "ReferralVisit_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "ReferralStart" (
  "id" TEXT NOT NULL,
  "assetId" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "status" "ReferralStartStatus" NOT NULL,
  "wasKnownUser" BOOLEAN NOT NULL,
  "startCount" INTEGER NOT NULL DEFAULT 1,
  "firstStartedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "lastStartedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "ReferralStart_pkey" PRIMARY KEY ("id")
);

CREATE TABLE "ClientAttribution" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "agentId" TEXT NOT NULL,
  "campaignId" TEXT NOT NULL,
  "assetId" TEXT NOT NULL,
  "referralStartId" TEXT NOT NULL,
  "attributedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "crmContactId" TEXT,
  "crmSyncedAt" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "ClientAttribution_pkey" PRIMARY KEY ("id")
);

ALTER TABLE "Application" ADD COLUMN "referralAttributionId" TEXT;

CREATE UNIQUE INDEX "ReferralAgent_telegramUserId_key" ON "ReferralAgent"("telegramUserId");
CREATE INDEX "ReferralAgent_status_createdAt_idx" ON "ReferralAgent"("status", "createdAt");
CREATE INDEX "ReferralCampaign_agentId_createdAt_idx" ON "ReferralCampaign"("agentId", "createdAt");
CREATE INDEX "ReferralCampaign_isActive_createdAt_idx" ON "ReferralCampaign"("isActive", "createdAt");
CREATE UNIQUE INDEX "ReferralAsset_code_key" ON "ReferralAsset"("code");
CREATE UNIQUE INDEX "ReferralAsset_campaignId_kind_key" ON "ReferralAsset"("campaignId", "kind");
CREATE INDEX "ReferralAsset_kind_createdAt_idx" ON "ReferralAsset"("kind", "createdAt");
CREATE INDEX "ReferralVisit_assetId_occurredAt_idx" ON "ReferralVisit"("assetId", "occurredAt");
CREATE INDEX "ReferralVisit_assetId_visitorKey_idx" ON "ReferralVisit"("assetId", "visitorKey");
CREATE INDEX "ReferralVisit_trafficKind_occurredAt_idx" ON "ReferralVisit"("trafficKind", "occurredAt");
CREATE UNIQUE INDEX "ReferralStart_assetId_userId_key" ON "ReferralStart"("assetId", "userId");
CREATE INDEX "ReferralStart_userId_firstStartedAt_idx" ON "ReferralStart"("userId", "firstStartedAt");
CREATE INDEX "ReferralStart_status_firstStartedAt_idx" ON "ReferralStart"("status", "firstStartedAt");
CREATE UNIQUE INDEX "ClientAttribution_userId_key" ON "ClientAttribution"("userId");
CREATE UNIQUE INDEX "ClientAttribution_referralStartId_key" ON "ClientAttribution"("referralStartId");
CREATE INDEX "ClientAttribution_agentId_attributedAt_idx" ON "ClientAttribution"("agentId", "attributedAt");
CREATE INDEX "ClientAttribution_campaignId_attributedAt_idx" ON "ClientAttribution"("campaignId", "attributedAt");
CREATE INDEX "ClientAttribution_assetId_attributedAt_idx" ON "ClientAttribution"("assetId", "attributedAt");
CREATE INDEX "ClientAttribution_crmContactId_idx" ON "ClientAttribution"("crmContactId");
CREATE INDEX "Application_referralAttributionId_createdAt_idx" ON "Application"("referralAttributionId", "createdAt");

ALTER TABLE "ReferralCampaign" ADD CONSTRAINT "ReferralCampaign_agentId_fkey" FOREIGN KEY ("agentId") REFERENCES "ReferralAgent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ReferralAsset" ADD CONSTRAINT "ReferralAsset_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "ReferralCampaign"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ReferralVisit" ADD CONSTRAINT "ReferralVisit_assetId_fkey" FOREIGN KEY ("assetId") REFERENCES "ReferralAsset"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ReferralStart" ADD CONSTRAINT "ReferralStart_assetId_fkey" FOREIGN KEY ("assetId") REFERENCES "ReferralAsset"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ReferralStart" ADD CONSTRAINT "ReferralStart_userId_fkey" FOREIGN KEY ("userId") REFERENCES "TelegramUser"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "ClientAttribution" ADD CONSTRAINT "ClientAttribution_userId_fkey" FOREIGN KEY ("userId") REFERENCES "TelegramUser"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "ClientAttribution" ADD CONSTRAINT "ClientAttribution_agentId_fkey" FOREIGN KEY ("agentId") REFERENCES "ReferralAgent"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "ClientAttribution" ADD CONSTRAINT "ClientAttribution_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "ReferralCampaign"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "ClientAttribution" ADD CONSTRAINT "ClientAttribution_assetId_fkey" FOREIGN KEY ("assetId") REFERENCES "ReferralAsset"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "ClientAttribution" ADD CONSTRAINT "ClientAttribution_referralStartId_fkey" FOREIGN KEY ("referralStartId") REFERENCES "ReferralStart"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE "Application" ADD CONSTRAINT "Application_referralAttributionId_fkey" FOREIGN KEY ("referralAttributionId") REFERENCES "ClientAttribution"("id") ON DELETE SET NULL ON UPDATE CASCADE;
