-- Base de Datos Personal Assistant
CREATE DATABASE IF NOT EXISTS personal_assistant_db;
USE personal_assistant_db;

-- @table: users
-- @description: Tabla de usuarios que contiene la información básica del usuario
CREATE TABLE IF NOT EXISTS users (
    user_id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: task_status
-- @description: Tabla de estados de las tareas
CREATE TABLE IF NOT EXISTS task_status (
    status_id INT AUTO_INCREMENT PRIMARY KEY,
    status_name VARCHAR(50) NOT NULL UNIQUE,
    description TEXT
);

-- @table: tasks
-- @description: Tabla de tareas que almacena las tareas creadas por los usuarios
CREATE TABLE IF NOT EXISTS tasks (
    task_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    status_id INT NOT NULL,
    task_title VARCHAR(150) NOT NULL,
    description TEXT,
    color VARCHAR(10),
    due_date DATE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: notification_types
-- @description: Tabla de tipos de notificación
CREATE TABLE IF NOT EXISTS notification_types (
    type_id INT AUTO_INCREMENT PRIMARY KEY,
    type_name VARCHAR(50) NOT NULL UNIQUE,
    description TEXT
);

-- @table: notifications
-- @description: Tabla de notificaciones que almacena las notificaciones enviadas a los usuarios
CREATE TABLE IF NOT EXISTS notifications (
    notification_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    type_id INT NOT NULL,
    message TEXT NOT NULL,
    is_read BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- @table: settings
-- @description: Tabla de configuraciones del sistema
CREATE TABLE IF NOT EXISTS settings (
    setting_id INT AUTO_INCREMENT PRIMARY KEY,
    setting_key VARCHAR(50) NOT NULL UNIQUE,
    setting_value VARCHAR(255) NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- @table: frequencies
-- @description: Tabla de frecuencias utilizadas para ingresos y gastos
CREATE TABLE IF NOT EXISTS frequencies (
    frequency_id INT AUTO_INCREMENT PRIMARY KEY,
    frequency_name VARCHAR(50) NOT NULL UNIQUE,
    description TEXT
);

-- @table: incomes
-- @description: Tabla de ingresos de los usuarios
CREATE TABLE IF NOT EXISTS incomes (
    income_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    income_name VARCHAR(100) NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    frequency_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: expense_categories
-- @description: Tabla de categorías de gastos
CREATE TABLE IF NOT EXISTS expense_categories (
    category_id INT AUTO_INCREMENT PRIMARY KEY,
    category_name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: expenses
-- @description: Tabla de gastos de los usuarios
CREATE TABLE IF NOT EXISTS expenses (
    expense_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    category_id INT NOT NULL,
    expense_name VARCHAR(100) NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    frequency_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: email_templates
-- @description: Tabla de plantillas de correo
CREATE TABLE IF NOT EXISTS email_templates (
    template_id INT AUTO_INCREMENT PRIMARY KEY,
    template_name VARCHAR(100) NOT NULL UNIQUE,
    html_content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: email_status
-- @description: Tabla de estados de correo
CREATE TABLE IF NOT EXISTS email_status (
    status_id INT AUTO_INCREMENT PRIMARY KEY,
    status_name VARCHAR(50) NOT NULL UNIQUE,
    description TEXT
);

-- @table: sent_emails
-- @description: Tabla de correos enviados por el sistema
CREATE TABLE IF NOT EXISTS sent_emails (
    email_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    template_id INT NOT NULL,
    status_id INT NOT NULL,
    subject VARCHAR(150) NOT NULL,
    message TEXT NOT NULL,
    sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- @table: healthy_recipes
-- @description: Tabla de recetas saludables
CREATE TABLE IF NOT EXISTS healthy_recipes (
    recipe_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    recipe_name VARCHAR(100) NOT NULL,
    description TEXT,
    ingredients TEXT,
    instructions TEXT,
    calories DECIMAL(6, 2),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: workout_routines
-- @description: Tabla de rutinas de gimnasio o calistenia
CREATE TABLE IF NOT EXISTS workout_routines (
    routine_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    routine_name VARCHAR(100) NOT NULL,
    description TEXT,
    routine_type VARCHAR(50) NOT NULL,
    exercises TEXT,
    duration INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: exercises
-- @description: Tabla de ejercicios para rutinas de gimnasio o calistenia
CREATE TABLE IF NOT EXISTS exercises (
    exercise_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    type VARCHAR(50) NOT NULL,
    muscle VARCHAR(50) NOT NULL,
    equipment VARCHAR(50),
    difficulty VARCHAR(20) NOT NULL,
    instructions TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @table: important_events
-- @description: Tabla de eventos importantes
CREATE TABLE IF NOT EXISTS important_events (
    event_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    event_name VARCHAR(100) NOT NULL,
    event_date DATE NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- @insert: task_status
-- @description: Inserta los estados iniciales de las tareas
INSERT INTO task_status (status_name, description) VALUES
('Pendiente', 'Tarea pendiente de realizar'),
('En Proceso', 'Tarea en ejecución'),
('Completada', 'Tarea completada'),
('Cancelada', 'Tarea cancelada');

-- @insert: notification_types
-- @description: Inserta los tipos de notificación iniciales
INSERT INTO notification_types (type_name, description) VALUES
('Alerta', 'Notificación de alerta de alta prioridad'),
('Recordatorio', 'Notificación para recordar tareas o eventos'),
('Información', 'Notificación de información general');

-- @insert: settings
-- @description: Inserta configuraciones iniciales del sistema
INSERT INTO settings (setting_key, setting_value, description) VALUES
('theme', 'dark', 'Tema predeterminado de la aplicación'),
('language', 'es', 'Idioma predeterminado de la aplicación'),
('timezone', 'UTC-6', 'Zona horaria predeterminada de la aplicación');

-- @insert: frequencies
-- @description: Inserta las frecuencias de ingresos y gastos
INSERT INTO frequencies (frequency_name, description) VALUES
('Diario', 'Frecuencia diaria'),
('Semanal', 'Frecuencia semanal'),
('Mensual', 'Frecuencia mensual'),
('Anual', 'Frecuencia anual');
