export const SCHEMA_VERSION = 1

export const SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS schema_version (
  version INTEGER PRIMARY KEY,
  applied_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS settings (
  key TEXT PRIMARY KEY,
  value TEXT
);

CREATE TABLE IF NOT EXISTS exercise (
  id TEXT PRIMARY KEY,
  title TEXT NOT NULL UNIQUE,
  description TEXT DEFAULT '',
  asymmetric INTEGER NOT NULL DEFAULT 0,
  alternate INTEGER NOT NULL DEFAULT 0,
  image_urls TEXT DEFAULT '[]',
  video_urls TEXT DEFAULT '[]',
  default_reps INTEGER NOT NULL DEFAULT 0,
  default_weight REAL NOT NULL DEFAULT 0.0,
  created_by TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS combo (
  id TEXT PRIMARY KEY,
  title TEXT NOT NULL UNIQUE,
  description TEXT DEFAULT '',
  type TEXT NOT NULL CHECK(type IN ('NONE', 'EMOM', 'AMRAP', 'TABATA')),
  timer_config TEXT DEFAULT '{}',
  created_by TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS combo_exercise (
  combo_id TEXT NOT NULL,
  exercise_id TEXT NOT NULL,
  position INTEGER NOT NULL,
  default_reps INTEGER NOT NULL DEFAULT 1,
  default_weight REAL NOT NULL DEFAULT 0.0,
  PRIMARY KEY (combo_id, position),
  FOREIGN KEY (combo_id) REFERENCES combo(id) ON DELETE CASCADE,
  FOREIGN KEY (exercise_id) REFERENCES exercise(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS session (
  id TEXT PRIMARY KEY,
  title TEXT NOT NULL UNIQUE,
  description TEXT DEFAULT '',
  created_by TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS session_item (
  session_id TEXT NOT NULL,
  item_id TEXT NOT NULL,
  item_type TEXT NOT NULL CHECK(item_type IN ('exercise', 'combo')),
  position INTEGER NOT NULL,
  repetitions INTEGER NOT NULL DEFAULT 1,
  weight REAL NOT NULL DEFAULT 0.0,
  PRIMARY KEY (session_id, position),
  FOREIGN KEY (session_id) REFERENCES session(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS collection (
  id TEXT PRIMARY KEY,
  label TEXT NOT NULL UNIQUE,
  created_by TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS collection_session (
  collection_id TEXT NOT NULL,
  session_id TEXT NOT NULL,
  position INTEGER NOT NULL,
  PRIMARY KEY (collection_id, position),
  FOREIGN KEY (collection_id) REFERENCES collection(id) ON DELETE CASCADE,
  FOREIGN KEY (session_id) REFERENCES session(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS execution_log (
  id TEXT PRIMARY KEY,
  session_id TEXT NOT NULL,
  started_at TEXT NOT NULL,
  finished_at TEXT,
  status TEXT NOT NULL DEFAULT 'completed' CHECK(status IN ('completed','aborted')),
  FOREIGN KEY (session_id) REFERENCES session(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS execution_log_item (
  id TEXT PRIMARY KEY,
  log_id TEXT NOT NULL,
  exercise_id TEXT NOT NULL,
  combo_id TEXT DEFAULT NULL,
  round_number INTEGER DEFAULT NULL,
  reps_done INTEGER DEFAULT 0,
  weight_used REAL DEFAULT 0.0,
  side TEXT DEFAULT NULL,
  completed INTEGER NOT NULL DEFAULT 0,
  timestamp TEXT,
  FOREIGN KEY (log_id) REFERENCES execution_log(id) ON DELETE CASCADE,
  FOREIGN KEY (exercise_id) REFERENCES exercise(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS suffix (
  user_uuid TEXT PRIMARY KEY,
  user_name TEXT NOT NULL,
  suffix TEXT NOT NULL UNIQUE
);
`
