-- CreateEnum
CREATE TYPE "AnalyticsEventType" AS ENUM (
  'BOT_STARTED',
  'BOT_INTERACTED',
  'CONTACT_SHARED',
  'MINIAPP_OPENED',
  'CATALOG_VIEWED',
  'SERVICE_VIEWED',
  'FAVORITE_ADDED',
  'PLAN_CREATED',
  'PLAN_SUBMITTED',
  'APPLICATION_SUBMITTED'
);

-- CreateEnum
CREATE TYPE "PaymentMethod" AS ENUM (
  'PAYMENT_LINK',
  'BANK_TRANSFER',
  'THAI_QR',
  'OTHER'
);

-- CreateEnum
CREATE TYPE "PaymentStatus" AS ENUM (
  'PENDING',
  'PAID',
  'REFUNDED',
  'CANCELLED',
  'FAILED'
);

-- CreateEnum
CREATE TYPE "ConversationStatus" AS ENUM (
  'WAITING_MANAGER',
  'WAITING_CUSTOMER',
  'CLOSED'
);

-- CreateEnum
CREATE TYPE "ChatDirection" AS ENUM ('INBOUND', 'OUTBOUND');

-- CreateEnum
CREATE TYPE "ChatMessageStatus" AS ENUM ('RECEIVED', 'SENT', 'FAILED');

-- AlterTable
ALTER TABLE "Application"
  ADD COLUMN "orderAmount" DECIMAL(12,2),
  ADD COLUMN "currency" VARCHAR(3) NOT NULL DEFAULT 'THB',
  ADD CONSTRAINT "Application_currency_thb_check" CHECK ("currency" = 'THB');

-- CreateTable
CREATE TABLE "Payment" (
  "id" TEXT NOT NULL,
  "applicationId" TEXT NOT NULL,
  "amount" DECIMAL(12,2) NOT NULL,
  "currency" VARCHAR(3) NOT NULL DEFAULT 'THB',
  "method" "PaymentMethod" NOT NULL,
  "status" "PaymentStatus" NOT NULL DEFAULT 'PENDING',
  "externalId" TEXT,
  "note" TEXT,
  "paidAt" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

  CONSTRAINT "Payment_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "Payment_currency_thb_check" CHECK ("currency" = 'THB')
);

-- CreateTable
CREATE TABLE "AnalyticsEvent" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "type" "AnalyticsEventType" NOT NULL,
  "source" TEXT NOT NULL,
  "eventDate" DATE NOT NULL,
  "metadata" JSONB,
  "occurredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "Conversation" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "status" "ConversationStatus" NOT NULL DEFAULT 'WAITING_CUSTOMER',
  "unreadCount" INTEGER NOT NULL DEFAULT 0,
  "lastMessageAt" TIMESTAMP(3),
  "lastInboundAt" TIMESTAMP(3),
  "lastOutboundAt" TIMESTAMP(3),
  "waitingSince" TIMESTAMP(3),
  "closedAt" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ChatMessage" (
  "id" TEXT NOT NULL,
  "conversationId" TEXT NOT NULL,
  "direction" "ChatDirection" NOT NULL,
  "status" "ChatMessageStatus" NOT NULL,
  "text" TEXT,
  "telegramMessageId" TEXT,
  "actor" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "Application_orderAmount_idx" ON "Application"("orderAmount");

-- CreateIndex
CREATE INDEX "Payment_applicationId_createdAt_idx" ON "Payment"("applicationId", "createdAt");

-- CreateIndex
CREATE INDEX "Payment_status_paidAt_idx" ON "Payment"("status", "paidAt");

-- CreateIndex
CREATE INDEX "Payment_method_paidAt_idx" ON "Payment"("method", "paidAt");

-- CreateIndex
CREATE UNIQUE INDEX "AnalyticsEvent_userId_type_eventDate_key" ON "AnalyticsEvent"("userId", "type", "eventDate");

-- CreateIndex
CREATE INDEX "AnalyticsEvent_type_occurredAt_idx" ON "AnalyticsEvent"("type", "occurredAt");

-- CreateIndex
CREATE INDEX "AnalyticsEvent_userId_occurredAt_idx" ON "AnalyticsEvent"("userId", "occurredAt");

-- CreateIndex
CREATE UNIQUE INDEX "Conversation_userId_key" ON "Conversation"("userId");

-- CreateIndex
CREATE INDEX "Conversation_status_waitingSince_idx" ON "Conversation"("status", "waitingSince");

-- CreateIndex
CREATE INDEX "Conversation_lastMessageAt_idx" ON "Conversation"("lastMessageAt");

-- CreateIndex
CREATE INDEX "ChatMessage_conversationId_createdAt_idx" ON "ChatMessage"("conversationId", "createdAt");

-- CreateIndex
CREATE INDEX "ChatMessage_direction_createdAt_idx" ON "ChatMessage"("direction", "createdAt");

-- AddForeignKey
ALTER TABLE "Payment"
  ADD CONSTRAINT "Payment_applicationId_fkey"
  FOREIGN KEY ("applicationId") REFERENCES "Application"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AnalyticsEvent"
  ADD CONSTRAINT "AnalyticsEvent_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "TelegramUser"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Conversation"
  ADD CONSTRAINT "Conversation_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "TelegramUser"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ChatMessage"
  ADD CONSTRAINT "ChatMessage_conversationId_fkey"
  FOREIGN KEY ("conversationId") REFERENCES "Conversation"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;
