CREATE TABLE IF NOT EXISTS users (
  name TEXT PRIMARY KEY,
  display_name TEXT NOT NULL,
  password TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE,
  disabled BOOLEAN NOT NULL,
  image TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS groups (
  name TEXT PRIMARY KEY,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS users_groups (
  user_name TEXT NOT NULL,
  group_name TEXT NOT NULL,
  PRIMARY KEY (user_name, group_name),
  FOREIGN KEY (user_name) REFERENCES users(name) ON DELETE CASCADE,
  FOREIGN KEY (group_name) REFERENCES groups(name) ON DELETE CASCADE
);

CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_users_timestamp
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_timestamp();

CREATE TRIGGER update_groups_timestamp
BEFORE UPDATE ON groups
FOR EACH ROW
EXECUTE FUNCTION update_timestamp();

CREATE OR REPLACE FUNCTION update_users_groups_timestamp()
RETURNS TRIGGER AS $$
BEGIN
  UPDATE users SET updated_at = NOW() WHERE name = NEW.user_name;
  UPDATE groups SET updated_at = NOW() WHERE name = NEW.group_name;
  RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_users_groups_timestamp
AFTER INSERT OR DELETE ON users_groups
FOR EACH ROW
EXECUTE FUNCTION update_users_groups_timestamp();
