CREATE TABLE "profile_user_images" (
  "id" UUID NOT NULL,
  "user_id" UUID NOT NULL,
  "storage_key" VARCHAR(500) NOT NULL,
  "mime_type" VARCHAR(80) NOT NULL,
  "byte_size" INTEGER NOT NULL,
  "checksum" CHAR(64) NOT NULL,
  "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMPTZ(3) NOT NULL,

  CONSTRAINT "profile_user_images_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "profile_user_images_byte_size_check"
    CHECK ("byte_size" > 0 AND "byte_size" <= 5242880),
  CONSTRAINT "profile_user_images_mime_type_check"
    CHECK ("mime_type" IN ('image/jpeg', 'image/png', 'image/webp')),
  CONSTRAINT "profile_user_images_user_id_fkey"
    FOREIGN KEY ("user_id") REFERENCES "identity_users"("id")
    ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE UNIQUE INDEX "profile_user_images_user_id_key"
  ON "profile_user_images"("user_id");
CREATE UNIQUE INDEX "profile_user_images_storage_key_key"
  ON "profile_user_images"("storage_key");
CREATE INDEX "profile_user_images_updated_at_idx"
  ON "profile_user_images"("updated_at");
