CREATE TABLE IF NOT EXISTS users (
  id SERIAL PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  password_hash TEXT NOT NULL,
  is_admin BOOLEAN NOT NULL DEFAULT FALSE,
  is_approved BOOLEAN NOT NULL DEFAULT FALSE,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS pads (
  id SERIAL PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  name TEXT NOT NULL,
  brand TEXT,
  description TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  UNIQUE (user_id, name)
);

CREATE TABLE IF NOT EXISTS reviews (
  id SERIAL PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  pad_id INTEGER NOT NULL REFERENCES pads(id) ON DELETE CASCADE,
  overall INTEGER CHECK (overall BETWEEN 1 AND 5),
  fit INTEGER NOT NULL CHECK (fit BETWEEN 1 AND 5),
  comfort INTEGER NOT NULL CHECK (comfort BETWEEN 1 AND 5),
  absorbency INTEGER NOT NULL CHECK (absorbency BETWEEN 1 AND 5),
  notes TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
  UNIQUE (user_id, pad_id)
);

CREATE TABLE IF NOT EXISTS photos (
  id SERIAL PRIMARY KEY,
  user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  pad_id INTEGER NOT NULL REFERENCES pads(id) ON DELETE CASCADE,
  filename TEXT UNIQUE NOT NULL,
  original_name TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
