
-- 1. Enable necessary extensions
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "citext";

-- 2. Define the Users table
CREATE TABLE "public"."Users" (
  "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "username" CITEXT UNIQUE NOT NULL,
  "email" CITEXT UNIQUE NOT NULL,
  "hashed_password" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 3. Create the function to update the timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$ language 'plpgsql';

-- 4. Create the trigger and attach it to the Users table
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON "public"."Users"
FOR EACH ROW
  EXECUTE FUNCTION update_updated_at_column();

-- 5. Define the Artworks table
CREATE TABLE "public"."Artworks" (
  "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "title" CITEXT UNIQUE NOT NULL,
  "filename" TEXT NOT NULL,
  "mimetype" TEXT NOT NULL,
  "size" INTEGER NOT NULL,
  "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  "updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 6. Create the trigger and attach it to the Artworks table
CREATE TRIGGER update_artworks_updated_at
BEFORE UPDATE ON "public"."Artworks"
FOR EACH ROW
  EXECUTE FUNCTION update_updated_at_column();
