-- USERS
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  level INT NOT NULL DEFAULT 1,
  xp_total INT NOT NULL DEFAULT 0,
  streak INT NOT NULL DEFAULT 0
);

-- DAILY LOGS
CREATE TABLE daily_logs (
  id SERIAL PRIMARY KEY,
  user_id INT NOT NULL REFERENCES users(id),
  date DATE NOT NULL,
  xp_earned INT NOT NULL DEFAULT 0,
  daily_perfect BOOLEAN NOT NULL DEFAULT FALSE,
  notes TEXT,
  CONSTRAINT unique_user_date UNIQUE (user_id, date)
);

-- TASKS
CREATE TABLE tasks (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  code VARCHAR(50) NOT NULL UNIQUE,
  category VARCHAR(50) NOT NULL,
  xp_value INT NOT NULL
);

-- TASK LOGS
CREATE TABLE task_logs (
  id SERIAL PRIMARY KEY,
  user_id INT NOT NULL REFERENCES users(id),
  task_id INT NOT NULL REFERENCES tasks(id),
  date DATE NOT NULL,
  completed BOOLEAN NOT NULL DEFAULT TRUE,
  CONSTRAINT unique_user_task_date UNIQUE (user_id, task_id, date)
);

-- REWARDS
CREATE TABLE rewards (
  id SERIAL PRIMARY KEY,
  level_required INT NOT NULL,
  reward_name VARCHAR(100) NOT NULL
);