-- CreateEnum
CREATE TYPE "public"."AttachmentStatus" AS ENUM ('ACTIVE', 'DELETED');

-- CreateEnum
CREATE TYPE "public"."RetentionPolicy" AS ENUM ('EXPENSE_7Y', 'TICKET_2Y', 'PROFILE_KEEP_LAST3');

-- CreateTable
CREATE TABLE "public"."blobs" (
    "sha256" CHAR(64) NOT NULL,
    "size" INTEGER NOT NULL,
    "mime" TEXT NOT NULL,
    "storage_path" TEXT NOT NULL,
    "refcount" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "blobs_pkey" PRIMARY KEY ("sha256")
);

-- CreateTable
CREATE TABLE "public"."attachments" (
    "id" SERIAL NOT NULL,
    "sha256" CHAR(64) NOT NULL,
    "owner_type" TEXT NOT NULL,
    "owner_id" TEXT NOT NULL,
    "orignal_name" TEXT NOT NULL,
    "status" "public"."AttachmentStatus" NOT NULL DEFAULT 'ACTIVE',
    "relation_policy" "public"."RetentionPolicy" NOT NULL,
    "created_by" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "attachments_owner_type_owner_id_created_at_idx" ON "public"."attachments"("owner_type", "owner_id", "created_at");

-- CreateIndex
CREATE INDEX "attachments_sha256_idx" ON "public"."attachments"("sha256");

-- AddForeignKey
ALTER TABLE "public"."attachments" ADD CONSTRAINT "attachments_sha256_fkey" FOREIGN KEY ("sha256") REFERENCES "public"."blobs"("sha256") ON DELETE RESTRICT ON UPDATE CASCADE;
