# 📋 Instrucciones para Ejecutar el SQL

## ⚠️ IMPORTANTE: Errores Normales

Algunos errores son **NORMALES** y puedes ignorarlos:
- ✅ `Duplicate column name` - La columna ya existe (OK)
- ✅ `Table already exists` - La tabla ya existe (OK)

Solo preocúpate si ves errores de **sintaxis** o **permisos**.

---

## 🎯 Método Recomendado: Paso a Paso

### Paso 1: Crear Tablas (Sin Errores)

Copia y ejecuta esto primero:

```sql
-- Tabla de notificaciones
CREATE TABLE IF NOT EXISTS notifications (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    from_user_id INT NULL,
    type ENUM('like', 'dislike', 'comment', 'reply', 'subscription', 'mention', 'system') NOT NULL,
    video_id INT NULL,
    comment_id INT NULL,
    title VARCHAR(255) NOT NULL,
    message TEXT NULL,
    is_read BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    read_at TIMESTAMP NULL,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (from_user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE,
    FOREIGN KEY (comment_id) REFERENCES comments(id) ON DELETE CASCADE,
    INDEX idx_user_id (user_id),
    INDEX idx_is_read (is_read),
    INDEX idx_created_at (created_at),
    INDEX idx_type (type)
) ENGINE=InnoDB;

-- Tabla de likes en shorts
CREATE TABLE IF NOT EXISTS short_likes (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    short_id INT NOT NULL,
    type ENUM('like', 'dislike') NOT NULL DEFAULT 'like',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (short_id) REFERENCES shorts(id) ON DELETE CASCADE,
    UNIQUE KEY unique_user_short (user_id, short_id),
    INDEX idx_short_id (short_id),
    INDEX idx_user_id (user_id)
) ENGINE=InnoDB;

-- Tabla de comentarios en shorts
CREATE TABLE IF NOT EXISTS short_comments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    short_id INT NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (short_id) REFERENCES shorts(id) ON DELETE CASCADE,
    INDEX idx_short_id (short_id),
    INDEX idx_user_id (user_id),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB;

-- Tabla de historial
CREATE TABLE IF NOT EXISTS watch_history (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    video_id INT NOT NULL,
    watched_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    watch_duration INT DEFAULT 0,
    completed BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE,
    INDEX idx_user_id (user_id),
    INDEX idx_video_id (video_id),
    INDEX idx_watched_at (watched_at),
    INDEX idx_user_video (user_id, video_id, watched_at)
) ENGINE=InnoDB;
```

✅ **Resultado esperado:** "4 consultas ejecutadas correctamente"

---

### Paso 2: Agregar Columnas (Pueden dar error)

Ahora ejecuta **UNA POR UNA** estas consultas:

```sql
ALTER TABLE users ADD COLUMN notifications_count INT UNSIGNED DEFAULT 0;
```

```sql
ALTER TABLE comments ADD COLUMN dislikes_count INT UNSIGNED DEFAULT 0;
```

```sql
ALTER TABLE shorts ADD COLUMN likes_count INT UNSIGNED DEFAULT 0;
```

```sql
ALTER TABLE shorts ADD COLUMN dislikes_count INT UNSIGNED DEFAULT 0;
```

```sql
ALTER TABLE shorts ADD COLUMN comments_count INT UNSIGNED DEFAULT 0;
```

```sql
ALTER TABLE shorts ADD COLUMN views_count INT UNSIGNED DEFAULT 0;
```

⚠️ **Si alguna da error "Duplicate column":**
- Es normal, significa que ya existe
- Continúa con la siguiente

---

## ✅ Verificación

Después de ejecutar todo, verifica:

1. **Ve a la pestaña "Estructura" de cada tabla:**
   - `notifications` - Debe existir
   - `short_likes` - Debe existir
   - `short_comments` - Debe existir
   - `watch_history` - Debe existir

2. **Verifica las columnas:**
   - `users` - Debe tener `notifications_count`
   - `comments` - Debe tener `dislikes_count`
   - `shorts` - Debe tener `likes_count`, `dislikes_count`, `comments_count`, `views_count`

---

## 🎉 ¡Listo!

Si todas las tablas existen y las columnas están agregadas, **todo funcionará**.

Ahora puedes:
- ✅ Ver videos sin errores
- ✅ Usar el historial
- ✅ Crear playlists
- ✅ Dar likes en shorts
- ✅ Comentar en shorts

---

## 🆘 Si Algo Sale Mal

### Error: "Cannot add foreign key constraint"
**Solución:** La tabla referenciada no existe. Asegúrate de que existan:
- `users`
- `videos`
- `shorts`
- `comments`

### Error: "Syntax error"
**Solución:** Copia exactamente como está, sin modificar nada.

### Error: "Access denied"
**Solución:** Verifica que estés usando el usuario correcto de MySQL.
