-- Tabella per le schede predefinite (template)
CREATE TABLE IF NOT EXISTS workout_templates_meta (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    difficulty_level ENUM('principiante', 'intermedio', 'avanzato') NOT NULL,
    estimated_time INT, -- in minuti
    calories_burn INT, -- stima calorie bruciate
    category VARCHAR(50), -- es: "forza", "cardio", "ipertrofia", ecc.
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Tabella per le schede personalizzate degli utenti
CREATE TABLE IF NOT EXISTS user_workouts_meta (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    is_active BOOLEAN DEFAULT true,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_performed TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users_meta(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Tabella per collegare gli esercizi ai template
CREATE TABLE IF NOT EXISTS template_exercises_meta (
    template_id INT NOT NULL,
    exercise_id INT NOT NULL,
    exercise_order INT NOT NULL, -- per mantenere l'ordine degli esercizi nella scheda
    sets INT NOT NULL DEFAULT 3,
    reps INT NOT NULL DEFAULT 12,
    rest_seconds INT NOT NULL DEFAULT 60,
    notes TEXT,
    PRIMARY KEY (template_id, exercise_id, exercise_order),
    FOREIGN KEY (template_id) REFERENCES workout_templates_meta(id) ON DELETE CASCADE,
    FOREIGN KEY (exercise_id) REFERENCES exercises_meta(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Tabella per collegare gli esercizi alle schede personalizzate
CREATE TABLE IF NOT EXISTS user_workout_exercises_meta (
    workout_id INT NOT NULL,
    exercise_id INT NOT NULL,
    exercise_order INT NOT NULL,
    sets INT NOT NULL DEFAULT 3,
    reps INT NOT NULL DEFAULT 12,
    rest_seconds INT NOT NULL DEFAULT 60,
    notes TEXT,
    PRIMARY KEY (workout_id, exercise_id, exercise_order),
    FOREIGN KEY (workout_id) REFERENCES user_workouts_meta(id) ON DELETE CASCADE,
    FOREIGN KEY (exercise_id) REFERENCES exercises_meta(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Inserimento di alcuni template di esempio
INSERT INTO workout_templates_meta (name, description, difficulty_level, estimated_time, calories_burn, category) VALUES
('Full Body Base', 'Allenamento completo per principianti che coinvolge tutti i gruppi muscolari principali', 'principiante', 45, 300, 'forza'),
('Upper Body Power', 'Allenamento intenso per la parte superiore del corpo', 'intermedio', 60, 400, 'ipertrofia'),
('Lower Body Focus', 'Allenamento dedicato alla parte inferiore del corpo', 'intermedio', 50, 450, 'forza'),
('HIIT Cardio', 'Allenamento ad alta intensità intervallato', 'avanzato', 30, 500, 'cardio'),
('Core Strength', 'Allenamento mirato per addominali e core', 'principiante', 30, 200, 'forza');
