DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'OtpEventType') THEN
        CREATE TYPE "OtpEventType" AS ENUM ('SENT', 'RESENT', 'VERIFIED', 'FAILED');
    END IF;
END $$;

CREATE TABLE IF NOT EXISTS "OtpEvent" (
    "id" SERIAL NOT NULL,
    "phone" TEXT NOT NULL,
    "eventType" "OtpEventType" NOT NULL,
    "provider" TEXT NOT NULL DEFAULT 'firebase',
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX IF NOT EXISTS "OtpEvent_phone_idx" ON "OtpEvent"("phone");
CREATE INDEX IF NOT EXISTS "OtpEvent_createdAt_idx" ON "OtpEvent"("createdAt");
