-- CreateEnum
CREATE TYPE "AutomationMode" AS ENUM ('AUTO', 'MANUAL', 'PAUSED');

-- CreateEnum
CREATE TYPE "DnsRecordStatus" AS ENUM ('PENDING', 'VERIFIED', 'MISMATCH', 'NOT_FOUND', 'ERROR', 'SKIPPED');

-- AlterTable
ALTER TABLE "orders" ADD COLUMN     "automationMode" "AutomationMode" NOT NULL DEFAULT 'MANUAL',
ADD COLUMN     "automation_paused_at" TIMESTAMP(3),
ADD COLUMN     "automation_paused_reason" TEXT,
ADD COLUMN     "dns_verified_at" TIMESTAMP(3),
ADD COLUMN     "netcup_server_id" TEXT,
ADD COLUMN     "source" TEXT;

-- CreateTable
CREATE TABLE "dns_verifications" (
    "id" TEXT NOT NULL,
    "order_id" TEXT NOT NULL,
    "wildcard_passed" BOOLEAN NOT NULL DEFAULT false,
    "manual_override" BOOLEAN NOT NULL DEFAULT false,
    "all_passed" BOOLEAN NOT NULL DEFAULT false,
    "total_subdomains" INTEGER NOT NULL DEFAULT 0,
    "passed_count" INTEGER NOT NULL DEFAULT 0,
    "last_checked_at" TIMESTAMP(3),
    "verified_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "dns_records" (
    "id" TEXT NOT NULL,
    "dns_verification_id" TEXT NOT NULL,
    "subdomain" TEXT NOT NULL,
    "full_domain" TEXT NOT NULL,
    "expected_ip" TEXT NOT NULL,
    "resolved_ip" TEXT,
    "status" "DnsRecordStatus" NOT NULL DEFAULT 'PENDING',
    "error_message" TEXT,
    "checked_at" TIMESTAMP(3),

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

-- CreateIndex
CREATE UNIQUE INDEX "dns_verifications_order_id_key" ON "dns_verifications"("order_id");

-- CreateIndex
CREATE INDEX "dns_records_dns_verification_id_idx" ON "dns_records"("dns_verification_id");

-- AddForeignKey
ALTER TABLE "dns_verifications" ADD CONSTRAINT "dns_verifications_order_id_fkey" FOREIGN KEY ("order_id") REFERENCES "orders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "dns_records" ADD CONSTRAINT "dns_records_dns_verification_id_fkey" FOREIGN KEY ("dns_verification_id") REFERENCES "dns_verifications"("id") ON DELETE CASCADE ON UPDATE CASCADE;
