-- Add migration script here

CREATE TABLE exercises (
    exercise_id uuid PRIMARY KEY,
    name varchar NOT NULL,
    description varchar NOT NULL,
    created_at TIMESTAMP DEFAULT now(),
    updated_at TIMESTAMP DEFAULT now()
);

CREATE TABLE muscle_groups (
    muscle_group_id varchar(16) PRIMARY KEY,
    name varchar NOT NULL
);

CREATE TABLE categories (
    category_id varchar(16) PRIMARY KEY,
    name varchar NOT NULL
);

CREATE TABLE workouts (
    workout_id uuid PRIMARY KEY,
    name varchar NOT NULL,
    description varchar NOT NULL,
    created_at TIMESTAMP DEFAULT now(),
    updated_at TIMESTAMP DEFAULT now()
);

CREATE TABLE workout_exercises (
    workout_id uuid NOT NULL,
    exercise_id uuid NOT NULL,
    PRIMARY KEY (workout_id, exercise_id),
    FOREIGN KEY (workout_id) REFERENCES workouts(workout_id) ON DELETE CASCADE,
    FOREIGN KEY (exercise_id) REFERENCES exercises(exercise_id) ON DELETE CASCADE
);

CREATE TABLE exercise_muscle_groups (
    exercise_id uuid NOT NULL,
    muscle_group_id varchar(16) NOT NULL,
    PRIMARY KEY (exercise_id, muscle_group_id),
    FOREIGN KEY (exercise_id) REFERENCES exercises(exercise_id) ON DELETE CASCADE,
    FOREIGN KEY (muscle_group_id) REFERENCES muscle_groups(muscle_group_id) ON DELETE CASCADE
);

CREATE TABLE exercise_categories (
    exercise_id uuid NOT NULL,
    category_id varchar(16) NOT NULL,
    PRIMARY KEY (exercise_id, category_id),
    FOREIGN KEY (exercise_id) REFERENCES exercises(exercise_id) ON DELETE CASCADE,
    FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE
);


-- Initialize muscle data
INSERT INTO muscle_groups (muscle_group_id, name) VALUES
('chest', 'Chest'),
('back', 'Back'),
('arms', 'Arms'),
('legs', 'Legs'),
('shoulders', 'Shoulders');

-- Initialize caregory data
INSERT INTO categories (category_id, name) VALUES
('strength', 'Strength'),
('cardio', 'Cardio');
