CREATE TABLE sessions (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    set_id     INTEGER NOT NULL REFERENCES training_sets(id),
    started_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    ended_at   DATETIME,
    note       TEXT DEFAULT ''
);

CREATE TABLE session_logs (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id    INTEGER NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
    exercise_id   INTEGER NOT NULL REFERENCES exercises(id),
    exercise_name TEXT NOT NULL,
    set_number    INTEGER NOT NULL CHECK(set_number >= 1),
    weight_kg     REAL NOT NULL CHECK(weight_kg >= 0 AND weight_kg <= 999),
    reps          INTEGER NOT NULL CHECK(reps >= 0 AND reps <= 999),
    note          TEXT DEFAULT '',
    logged_at     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(session_id, exercise_id, set_number)
);

CREATE INDEX idx_sessions_set_id ON sessions(set_id);
CREATE INDEX idx_sessions_started_at ON sessions(started_at);
CREATE INDEX idx_session_logs_session_id ON session_logs(session_id);
CREATE INDEX idx_session_logs_exercise_id ON session_logs(exercise_id);
