CREATE TABLE IF NOT EXISTS fitness_user_trainings (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    created TIMESTAMP DEFAULT NOW(),
    modified TIMESTAMP,
    title varchar(255),
    training_date DATE,
    beginning TIME,
    ending TIME,
    sleeping_time_in_min int(11),
    warmup_device varchar(255),
    warmup_duration_in_sec int(11),
    cardio_device varchar(255),
    cardio_duration_in_sec int(11),
    rating int(1),
    notes TEXT,
    series_count int(2) DEFAULT 1,
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_user_nutritions (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    training bigint(20) UNSIGNED NOT NULL,
    food varchar(255),
    nutrition_time TIME,
    kcal DOUBLE(10,2) DEFAULT 0,
    proteins DOUBLE(10,2) DEFAULT 0,
    carbohydrates DOUBLE(10,2) DEFAULT 0,
    fat DOUBLE(10,2) DEFAULT 0,
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE,
    FOREIGN KEY (training) REFERENCES fitness_user_trainings (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_user_exercises (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    training bigint(20) UNSIGNED NOT NULL,
    muscle_group int(2),
    exercise varchar(255),
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE,
    FOREIGN KEY (training) REFERENCES fitness_user_trainings (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_user_exercise_series (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    exercise bigint(20) UNSIGNED NOT NULL,
    number int(2),
    repetitions int(4) DEFAULT 0,
    kg double(4,1) DEFAULT 0,
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE,
    FOREIGN KEY (exercise) REFERENCES fitness_user_exercises (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_training_plans (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    created TIMESTAMP DEFAULT NOW(),
    modified TIMESTAMP,
    title varchar(255),
    keywords varchar(255),
    duration int(3),
    frequency int(1),
    notes TEXT,
    status ENUM('draft', 'final') DEFAULT 'draft',
    security ENUM('public', 'private') DEFAULT 'private',
    type varchar(24) DEFAULT '',
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_training_plan_days (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    training_plan bigint(20) UNSIGNED NOT NULL,
    day int(1),
    position int(2),
    muscle_group int(2),
    exercise varchar(255),
    series int(2) DEFAULT 0,
    repetitions varchar(64),
    break_length int(4) DEFAULT 0,
    break_unit ENUM('sec', 'min') DEFAULT 'sec',
    FOREIGN KEY(training_plan) REFERENCES fitness_training_plans (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_activated_training_plans (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    training_plan bigint(20) UNSIGNED NOT NULL,
    beginning DATE,
    ending DATE,
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE,
    FOREIGN KEY (training_plan) REFERENCES fitness_training_plans (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_training_goals (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    created TIMESTAMP DEFAULT NOW(),
    modified TIMESTAMP,
    title varchar(255),
    beginning_date DATE,
    ending_date DATE,
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;

CREATE TABLE IF NOT EXISTS fitness_user_goals (
    id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    creator bigint(20) UNSIGNED NOT NULL,
    training_goal bigint(20) UNSIGNED NOT NULL,
    body_part int(2),
    initial_value DOUBLE(4,1) DEFAULT 0,
    current_value DOUBLE(4,1) DEFAULT 0,
    final_goal DOUBLE(4,1) DEFAULT 0,
    FOREIGN KEY (creator) REFERENCES fitness_users (id) ON DELETE CASCADE,
    FOREIGN KEY (training_goal) REFERENCES fitness_training_goals (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
