-- CreateEnum
CREATE TYPE "LicenseStatus" AS ENUM ('VALID', 'INVALID');

-- CreateTable
CREATE TABLE "Customer" (
    "id" SERIAL NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "name" TEXT NOT NULL,
    "project" TEXT,
    "email" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "License" (
    "key" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "customerId" INTEGER NOT NULL,
    "status" "LicenseStatus" NOT NULL DEFAULT E'VALID',
    "commercial" BOOLEAN NOT NULL DEFAULT false,
    "acceptedTOS" BOOLEAN NOT NULL DEFAULT false,

    CONSTRAINT "License_pkey" PRIMARY KEY ("key")
);

-- CreateTable
CREATE TABLE "Active" (
    "id" SERIAL NOT NULL,
    "date" DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "licenseKey" TEXT NOT NULL,
    "profileID" TEXT NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "License_customerId_key" ON "License"("customerId");

-- CreateIndex
CREATE INDEX "License_key_idx" ON "License"("key");

-- CreateIndex
CREATE INDEX "Active_licenseKey_idx" ON "Active"("licenseKey");

-- CreateIndex
CREATE INDEX "Active_date_idx" ON "Active"("date");

-- AddForeignKey
ALTER TABLE "License" ADD CONSTRAINT "License_customerId_fkey" FOREIGN KEY ("customerId") REFERENCES "Customer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Active" ADD CONSTRAINT "Active_licenseKey_fkey" FOREIGN KEY ("licenseKey") REFERENCES "License"("key") ON DELETE RESTRICT ON UPDATE CASCADE;
