-- Exercise media library and versioned training-plan content. -- Apply to an existing database after backing it up. CREATE TABLE exercises ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, exercise_uuid CHAR(36) NOT NULL UNIQUE, slug VARCHAR(100) NOT NULL UNIQUE, title_ar VARCHAR(160) NOT NULL, title_en VARCHAR(160) NULL, instructions_ar JSON NOT NULL, target_muscles JSON NOT NULL, equipment JSON NOT NULL, difficulty ENUM('beginner', 'intermediate', 'advanced') NOT NULL DEFAULT 'beginner', movement_type ENUM('strength', 'mobility', 'cardio', 'recovery') NOT NULL, gif_url VARCHAR(500) NULL, gif_poster_url VARCHAR(500) NULL, duration_seconds SMALLINT UNSIGNED NULL, repetitions VARCHAR(80) NULL, safety_notes_ar JSON NOT NULL, alternative_exercise_id BIGINT UNSIGNED NULL, is_published BOOLEAN NOT NULL DEFAULT FALSE, content_revision INT UNSIGNED NOT NULL DEFAULT 1, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_exercises_public (is_published, movement_type, difficulty), CONSTRAINT fk_exercise_alternative FOREIGN KEY (alternative_exercise_id) REFERENCES exercises(id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE training_plans ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, plan_uuid CHAR(36) NOT NULL UNIQUE, slug VARCHAR(100) NOT NULL UNIQUE, title_ar VARCHAR(160) NOT NULL, summary_ar TEXT NOT NULL, goal ENUM('general_fitness', 'weight_management', 'mobility', 'endurance') NOT NULL, level ENUM('beginner', 'intermediate', 'advanced') NOT NULL, weeks_duration TINYINT UNSIGNED NOT NULL, source_notes JSON NULL, safety_notes_ar JSON NOT NULL, is_published BOOLEAN NOT NULL DEFAULT FALSE, content_revision INT UNSIGNED NOT NULL DEFAULT 1, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_plans_public (is_published, goal, level) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE training_plan_sessions ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, plan_id BIGINT UNSIGNED NOT NULL, week_number TINYINT UNSIGNED NOT NULL, day_number TINYINT UNSIGNED NOT NULL, title_ar VARCHAR(160) NOT NULL, session_type ENUM('strength', 'walking', 'mobility', 'rest') NOT NULL, duration_minutes SMALLINT UNSIGNED NOT NULL DEFAULT 0, intensity ENUM('easy', 'moderate', 'vigorous') NOT NULL DEFAULT 'easy', notes_ar TEXT NULL, sort_order SMALLINT UNSIGNED NOT NULL DEFAULT 0, UNIQUE KEY uq_plan_week_day (plan_id, week_number, day_number), CONSTRAINT fk_plan_sessions_plan FOREIGN KEY (plan_id) REFERENCES training_plans(id) ON DELETE CASCADE, INDEX idx_plan_sessions_order (plan_id, week_number, sort_order) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; CREATE TABLE training_session_exercises ( session_id BIGINT UNSIGNED NOT NULL, exercise_id BIGINT UNSIGNED NOT NULL, sort_order SMALLINT UNSIGNED NOT NULL DEFAULT 0, sets TINYINT UNSIGNED NULL, reps VARCHAR(60) NULL, duration_seconds SMALLINT UNSIGNED NULL, rest_seconds SMALLINT UNSIGNED NOT NULL DEFAULT 45, PRIMARY KEY (session_id, exercise_id), CONSTRAINT fk_session_exercises_session FOREIGN KEY (session_id) REFERENCES training_plan_sessions(id) ON DELETE CASCADE, CONSTRAINT fk_session_exercises_exercise FOREIGN KEY (exercise_id) REFERENCES exercises(id) ON DELETE RESTRICT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;