CREATE TABLE exercise_categories (
	id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
	name VARCHAR(100) NOT NULL UNIQUE,
	created_at TIMESTAMP NOT NULL DEFAULT NOW(),
	updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE exercises (
	id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
	name VARCHAR(255) NOT NULL,
	description TEXT,
	category_id UUID NULL REFERENCES exercise_categories(id) ON DELETE RESTRICT,
	created_at TIMESTAMP NOT NULL DEFAULT NOW(),
	updated_at TIMESTAMP NOT NULL DEFAULT NOW()

);

CREATE INDEX idx_exercises_category_id ON exercises(category_id);

CREATE TABLE workouts (
	id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
	user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
	name VARCHAR(255) NULL,
	notes TEXT,
	workout_date TIMESTAMP NOT NULL DEFAULT NOW(),
	created_at TIMESTAMP NOT NULL DEFAULT NOW(),
	updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_workouts_user_id ON workouts(user_id);
CREATE INDEX idx_workouts_workout_date ON workouts(workout_date);

CREATE TABLE workout_exercises (
	id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
	workout_id UUID NOT NULL REFERENCES workouts(id) ON DELETE CASCADE,
	exercise_id UUID NOT NULL REFERENCES exercises(id) ON DELETE RESTRICT,
	notes TEXT,
	order_index INT,
	created_at TIMESTAMP NOT NULL DEFAULT NOW(),
	updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_workout_exercises_workout_id ON workout_exercises(workout_id);
CREATE INDEX idx_workout_exercises_exercise_id ON workout_exercises(exercise_id);

CREATE TABLE exercise_sets (
	id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
	workout_exercise_id UUID NOT NULL REFERENCES workout_exercises(id) ON DELETE CASCADE,
	set_number INT NOT NULL,
	reps INT,
	weight DECIMAL(10,2),
	weight_unit VARCHAR(10),
	duration_seconds INTEGER,
	distance DECIMAL(10,2),
	distance_unit VARCHAR(10),
	notes TEXT,
	rate_perceived_exertion INT,
	created_at TIMESTAMP NOT NULL DEFAULT NOW(),
	updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_exercise_sets_workout_exercise_id ON exercise_sets(workout_exercise_id);
