CREATE DATABASE IF NOT EXISTS uigv_cms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE uigv_cms;

CREATE TABLE IF NOT EXISTS usuarios (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(100) NOT NULL,
    email VARCHAR(150) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    rol ENUM('superadmin','admin','editor') DEFAULT 'editor',
    activo TINYINT(1) DEFAULT 1,
    ultimo_acceso DATETIME DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS configuracion (
    id INT AUTO_INCREMENT PRIMARY KEY,
    clave VARCHAR(100) NOT NULL UNIQUE,
    valor TEXT,
    tipo ENUM('text','textarea','image','color','boolean') DEFAULT 'text',
    grupo VARCHAR(50) DEFAULT 'general',
    descripcion VARCHAR(255) DEFAULT NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS menu_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(100) NOT NULL,
    url VARCHAR(255) DEFAULT '#',
    parent_id INT DEFAULT NULL,
    orden INT DEFAULT 0,
    activo TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (parent_id) REFERENCES menu_items(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS sliders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(200) NOT NULL,
    subtitulo VARCHAR(300) DEFAULT NULL,
    imagen VARCHAR(255) NOT NULL,
    enlace VARCHAR(255) DEFAULT '#',
    texto_boton VARCHAR(50) DEFAULT 'Ver mas',
    orden INT DEFAULT 0,
    activo TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS paginas (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(200) NOT NULL,
    slug VARCHAR(200) NOT NULL UNIQUE,
    contenido LONGTEXT,
    imagen_portada VARCHAR(255) DEFAULT NULL,
    meta_titulo VARCHAR(200) DEFAULT NULL,
    meta_descripcion VARCHAR(300) DEFAULT NULL,
    publicado TINYINT(1) DEFAULT 1,
    autor_id INT DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (autor_id) REFERENCES usuarios(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS categorias_noticias (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(100) NOT NULL,
    slug VARCHAR(100) NOT NULL UNIQUE,
    color VARCHAR(7) DEFAULT '#1B3A5C',
    activo TINYINT(1) DEFAULT 1
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS noticias (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(300) NOT NULL,
    slug VARCHAR(300) NOT NULL UNIQUE,
    extracto VARCHAR(500) DEFAULT NULL,
    contenido LONGTEXT,
    imagen VARCHAR(255) DEFAULT NULL,
    categoria_id INT DEFAULT NULL,
    autor_id INT DEFAULT NULL,
    destacado TINYINT(1) DEFAULT 0,
    publicado TINYINT(1) DEFAULT 1,
    fecha_publicacion DATETIME DEFAULT CURRENT_TIMESTAMP,
    vistas INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (categoria_id) REFERENCES categorias_noticias(id) ON DELETE SET NULL,
    FOREIGN KEY (autor_id) REFERENCES usuarios(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS facultades (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(200) NOT NULL,
    slug VARCHAR(200) NOT NULL UNIQUE,
    descripcion TEXT,
    contenido LONGTEXT,
    decano VARCHAR(150) DEFAULT NULL,
    imagen VARCHAR(255) DEFAULT NULL,
    icono VARCHAR(50) DEFAULT 'fa-graduation-cap',
    email VARCHAR(150) DEFAULT NULL,
    telefono VARCHAR(30) DEFAULT NULL,
    color VARCHAR(7) DEFAULT '#1B3A5C',
    orden INT DEFAULT 0,
    activo TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS carreras (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nombre VARCHAR(200) NOT NULL,
    slug VARCHAR(200) NOT NULL UNIQUE,
    descripcion TEXT,
    duracion VARCHAR(50) DEFAULT '5 anios / 10 ciclos',
    grado VARCHAR(100) DEFAULT 'Bachiller',
    facultad_id INT NOT NULL,
    activo TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (facultad_id) REFERENCES facultades(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS eventos (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(200) NOT NULL,
    slug VARCHAR(200) NOT NULL UNIQUE,
    descripcion TEXT,
    fecha_inicio DATETIME NOT NULL,
    lugar VARCHAR(200) DEFAULT NULL,
    publicado TINYINT(1) DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS enlaces_rapidos (
    id INT AUTO_INCREMENT PRIMARY KEY,
    titulo VARCHAR(100) NOT NULL,
    url VARCHAR(255) NOT NULL,
    icono VARCHAR(50) DEFAULT 'fa-link',
    grupo ENUM('servicios','footer','header') DEFAULT 'servicios',
    orden INT DEFAULT 0,
    activo TINYINT(1) DEFAULT 1
) ENGINE=InnoDB;

-- DATOS INICIALES

-- Admin (password: admin123)
INSERT INTO usuarios (nombre, email, password, rol) VALUES
('Administrador', 'admin@universidad.edu.pe', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'superadmin');

INSERT INTO configuracion (clave, valor, tipo, grupo, descripcion) VALUES
('nombre_sitio', 'Universidad Inca Garcilaso de la Vega', 'text', 'general', 'Nombre'),
('siglas', 'UIGV', 'text', 'general', 'Siglas'),
('slogan', 'Nuevos tiempos, nuevas ideas', 'text', 'general', 'Slogan'),
('color_primario', '#1B3A5C', 'color', 'apariencia', 'Color primario'),
('color_secundario', '#C8A84E', 'color', 'apariencia', 'Color secundario'),
('telefono', '(01) 480-1579', 'text', 'contacto', 'Telefono'),
('email', 'informes@universidad.edu.pe', 'text', 'contacto', 'Email'),
('direccion', 'Jiron Rio Tambo 631 - Pueblo Libre, Lima', 'textarea', 'contacto', 'Direccion'),
('facebook', 'https://facebook.com/uigv', 'text', 'redes', 'Facebook'),
('instagram', 'https://instagram.com/uigv', 'text', 'redes', 'Instagram'),
('youtube', 'https://youtube.com/uigv', 'text', 'redes', 'YouTube'),
('horario', 'Lunes a Viernes 8:30am - 5:00pm', 'text', 'contacto', 'Horario'),
('footer_texto', '&copy; 2026 Universidad Inca Garcilaso de la Vega', 'textarea', 'general', 'Texto footer');

INSERT INTO menu_items (titulo, url, parent_id, orden) VALUES
('Inicio', '/', NULL, 1),
('Nosotros', '/pagina/nosotros', NULL, 2),
('Facultades', '/facultades', NULL, 3),
('Admision', '/pagina/admision', NULL, 4),
('Noticias', '/noticias', NULL, 5),
('Servicios', '#', NULL, 6),
('Contacto', '/pagina/contacto', NULL, 7);

INSERT INTO menu_items (titulo, url, parent_id, orden) VALUES
('Intranet', '#', 6, 1),
('Correo Alumnos', '#', 6, 2),
('Repositorio', '#', 6, 3);

INSERT INTO categorias_noticias (nombre, slug, color) VALUES
('Institucional', 'institucional', '#1B3A5C'),
('Academico', 'academico', '#2E7D32'),
('Investigacion', 'investigacion', '#C8A84E'),
('Eventos', 'eventos', '#D4272E');

INSERT INTO sliders (titulo, subtitulo, imagen, enlace, texto_boton, orden) VALUES
('Bienvenidos a la UIGV', 'Formando profesionales desde 1964', '/uploads/slider1.jpg', '/pagina/nosotros', 'Conoce mas', 1),
('Admision 2026-II', 'Inscripciones abiertas', '/uploads/slider2.jpg', '/pagina/admision', 'Inscribete', 2),
('Rumbo al Licenciamiento', 'Calidad educativa', '/uploads/slider3.jpg', '/noticias', 'Ver avances', 3);

INSERT INTO facultades (nombre, slug, descripcion, decano, icono, color, orden) VALUES
('Derecho y Ciencias Politicas', 'derecho', 'Formacion integral de profesionales del Derecho', 'Dr. Ejemplo', 'fa-gavel', '#1B3A5C', 1),
('Ciencias Administrativas', 'administrativas', 'Lideres en gestion empresarial', 'Dr. Ejemplo', 'fa-chart-line', '#2E7D32', 2),
('Ingenieria de Sistemas', 'ingenieria-sistemas', 'Tecnologia e innovacion', 'Dr. Ejemplo', 'fa-laptop-code', '#C8A84E', 3),
('Psicologia', 'psicologia', 'Bienestar humano', 'Dr. Ejemplo', 'fa-brain', '#6A1B9A', 4),
('Comunicaciones', 'comunicaciones', 'Comunicadores con vision critica', 'Dr. Ejemplo', 'fa-broadcast-tower', '#D4272E', 5),
('Estomatologia', 'estomatologia', 'Salud bucal', 'Dr. Ejemplo', 'fa-tooth', '#00838F', 6),
('Educacion', 'educacion', 'Formando formadores', 'Dr. Ejemplo', 'fa-chalkboard-teacher', '#EF6C00', 7);

INSERT INTO paginas (titulo, slug, contenido, publicado) VALUES
('Nosotros', 'nosotros', '<h2>Historia</h2><p>Fundada el 21 de diciembre de 1964. Mas de 60 anios formando profesionales.</p><h2>Mision</h2><p>Formar profesionales competitivos con base cientifica, tecnologica y humanista.</p><h2>Vision</h2><p>Ser referente de servicios universitarios a nivel nacional e internacional.</p>', 1),
('Admision', 'admision', '<h2>Admision 2026-II</h2><p>Inscripciones abiertas para todas las carreras.</p>', 1),
('Contacto', 'contacto', '<h2>Contactenos</h2><p>Estamos para ayudarte.</p>', 1);

INSERT INTO noticias (titulo, slug, extracto, contenido, categoria_id, destacado, fecha_publicacion) VALUES
('UIGV inicia ciclo 2026-I', 'uigv-inicia-ciclo-2026', 'La universidad inicia un nuevo periodo academico.', '<p>Con gran entusiasmo damos inicio al ciclo 2026-I con nuevas aulas y laboratorios.</p>', 1, 1, NOW()),
('Convocatoria docentes 2026', 'convocatoria-docentes-2026', 'Se buscan profesionales para plazas docentes.', '<p>La UIGV convoca profesionales para diversas facultades.</p>', 2, 0, NOW()),
('Avances en IA', 'investigacion-ia', 'Investigacion en inteligencia artificial aplicada.', '<p>La Facultad de Ingenieria presenta avances en IA aplicada a la educacion.</p>', 3, 1, NOW());

INSERT INTO enlaces_rapidos (titulo, url, icono, grupo, orden) VALUES
('Servicios Academicos', '#', 'fa-book', 'servicios', 1),
('Repositorio', '#', 'fa-database', 'servicios', 2),
('Intranet', '#', 'fa-lock', 'servicios', 3),
('Correo Alumnos', '#', 'fa-envelope', 'servicios', 4),
('SISDOC', '#', 'fa-file-alt', 'servicios', 5),
('Mesa de Partes', '#', 'fa-inbox', 'servicios', 6);

-- ============================================================
-- SÍLABOS: Unidades Didácticas y Sesiones por Curso
-- ============================================================
CREATE TABLE IF NOT EXISTS unidades_didacticas (
    id           INT AUTO_INCREMENT PRIMARY KEY,
    facultad_id  INT NOT NULL,
    numero       TINYINT NOT NULL DEFAULT 1 COMMENT 'I, II, III...',
    titulo       VARCHAR(200) NOT NULL,
    descripcion  VARCHAR(255) DEFAULT NULL,
    semanas      VARCHAR(50)  DEFAULT NULL COMMENT 'Ej: Semanas 1-5',
    orden        INT          DEFAULT 0,
    activo       TINYINT(1)   DEFAULT 1,
    FOREIGN KEY (facultad_id) REFERENCES facultades(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS sesiones (
    id          INT AUTO_INCREMENT PRIMARY KEY,
    unidad_id   INT NOT NULL,
    numero      VARCHAR(10)  NOT NULL COMMENT 'Ej: 01, 02, EXAMEN',
    titulo      VARCHAR(200) NOT NULL,
    descripcion TEXT         DEFAULT NULL,
    tipo        ENUM('clase','examen','practica') DEFAULT 'clase',
    orden       INT          DEFAULT 0,
    FOREIGN KEY (unidad_id) REFERENCES unidades_didacticas(id) ON DELETE CASCADE
) ENGINE=InnoDB;
