CREATE DATABASE IF NOT EXISTS yomitrack;
USE yomitrack;

-- Tabla de usuarios
CREATE TABLE users (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100),
    email VARCHAR(255),
    password VARCHAR(255),
    is_verified TINYINT(1),
    verificationToken VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    avatarUrl TEXT,
    resetToken VARCHAR(255)
);

-- Tabla de anime
CREATE TABLE anime (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    userId INT(11),
    title VARCHAR(255),
    status VARCHAR(50),
    progress INT(11),
    score INT(11),
    totalEpisodes INT(11),
    imageUrl TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    type VARCHAR(20),
    FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
);

-- Tabla de manga
CREATE TABLE manga (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    userId INT(11),
    title VARCHAR(255),
    status VARCHAR(50),
    progress INT(11),
    score INT(11),
    totalChapters INT(11),
    imageUrl TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    type VARCHAR(20),
    FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
);

-- Tabla de actividad
CREATE TABLE activity_log (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    userId INT(11),
    action VARCHAR(255),
    mediaTitle VARCHAR(255),
    type VARCHAR(20),
    imageUrl TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
);

-- Tabla de comentarios
CREATE TABLE comments (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    text TEXT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    user_id INT(11),
    activity_id INT(11),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (activity_id) REFERENCES activity_log(id) ON DELETE CASCADE
);

-- Tabla de likes
CREATE TABLE likes (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    user_id INT(11),
    activity_id INT(11),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (activity_id) REFERENCES activity_log(id) ON DELETE CASCADE
);

-- Tabla de recuperación de contraseña
CREATE TABLE password_resets (
    id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    email VARCHAR(255),
    token VARCHAR(255),
    expires_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    user_id INT(11),
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
