-- ============================================================
--  BASE DE DATOS: Sistema de Gestión de Cine
--  Framework: CodeIgniter (PHP)
--  Autor: Proyecto Final UNAC
-- ============================================================

CREATE DATABASE IF NOT EXISTS db_cine
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

USE db_cine;

-- ============================================================
-- 0. CONFIGURACIÓN DEL SISTEMA
-- ============================================================

CREATE TABLE settings (
    id          INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `key`       VARCHAR(255) NOT NULL,
    context     VARCHAR(100) NOT NULL DEFAULT 'default',
    value       LONGTEXT,
    created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uq_key_context (`key`, context)
);

-- ============================================================
-- 1. ROLES Y USUARIOS
-- ============================================================

CREATE TABLE rol (
    id_rol      INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    nombre      VARCHAR(50) NOT NULL,          -- 'administrador', 'empleado', 'cliente'
    descripcion VARCHAR(255)
);

CREATE TABLE usuario (
    id_usuario  INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_rol      INT UNSIGNED NOT NULL,
    nombres     VARCHAR(100) NOT NULL,
    apellidos   VARCHAR(100) NOT NULL,
    email       VARCHAR(150) NOT NULL UNIQUE,
    password    VARCHAR(255) NOT NULL,         -- hash bcrypt
    telefono    VARCHAR(20),
    estado      TINYINT(1) NOT NULL DEFAULT 1, -- 1=activo, 0=inactivo
    created_at  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_usuario_rol FOREIGN KEY (id_rol) REFERENCES rol(id_rol)
);

-- ============================================================
-- 2. MEMBRESÍA / PROGRAMA DE FIDELIZACIÓN
-- ============================================================

CREATE TABLE membresia (
    id_membresia    INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    nombre          VARCHAR(80) NOT NULL,       -- 'Básica', 'Silver', 'Gold'
    descuento_pct   DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    puntos_por_sol  DECIMAL(5,2) NOT NULL DEFAULT 1.00,
    precio_mensual  DECIMAL(8,2) NOT NULL DEFAULT 0.00
);

CREATE TABLE cliente_membresia (
    id_cm           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_usuario      INT UNSIGNED NOT NULL,
    id_membresia    INT UNSIGNED NOT NULL,
    puntos          INT UNSIGNED NOT NULL DEFAULT 0,
    fecha_inicio    DATE NOT NULL,
    fecha_fin       DATE,
    CONSTRAINT fk_cm_usuario    FOREIGN KEY (id_usuario)   REFERENCES usuario(id_usuario),
    CONSTRAINT fk_cm_membresia  FOREIGN KEY (id_membresia) REFERENCES membresia(id_membresia)
);

-- ============================================================
-- 3. PELÍCULAS
-- ============================================================

CREATE TABLE genero (
    id_genero   INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    nombre      VARCHAR(80) NOT NULL
);

CREATE TABLE pelicula (
    id_pelicula     INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_genero       INT UNSIGNED NOT NULL,
    titulo          VARCHAR(200) NOT NULL,
    sinopsis        TEXT,
    duracion_min    SMALLINT UNSIGNED NOT NULL,   -- minutos
    clasificacion   ENUM('G','PG','PG-13','R','NC-17') NOT NULL DEFAULT 'G',
    idioma          VARCHAR(50) NOT NULL DEFAULT 'Español',
    poster_url      VARCHAR(500),
    trailer_url     VARCHAR(500),
    estado          TINYINT(1) NOT NULL DEFAULT 1,
    CONSTRAINT fk_pelicula_genero FOREIGN KEY (id_genero) REFERENCES genero(id_genero)
);

-- ============================================================
-- 4. LOCALES (sedes del cine)  →  una película puede estar en muchos locales
-- ============================================================

CREATE TABLE local (
    id_local    INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    nombre      VARCHAR(150) NOT NULL,
    direccion   VARCHAR(255) NOT NULL,
    distrito    VARCHAR(100),
    ciudad      VARCHAR(100) NOT NULL DEFAULT 'Lima',
    telefono    VARCHAR(20),
    estado      TINYINT(1) NOT NULL DEFAULT 1
);

-- ============================================================
-- 5. SALAS  →  cada local tiene varias salas
-- ============================================================

CREATE TABLE sala (
    id_sala     INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_local    INT UNSIGNED NOT NULL,
    nombre      VARCHAR(80) NOT NULL,           -- 'Sala 1', 'IMAX', '4DX'
    tipo        ENUM('2D','3D','IMAX','4DX') NOT NULL DEFAULT '2D',
    capacidad   SMALLINT UNSIGNED NOT NULL,
    estado      TINYINT(1) NOT NULL DEFAULT 1,
    CONSTRAINT fk_sala_local FOREIGN KEY (id_local) REFERENCES local(id_local)
);

-- ============================================================
-- 6. ASIENTOS  →  cada sala tiene muchos asientos
-- ============================================================

CREATE TABLE asiento (
    id_asiento  INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_sala     INT UNSIGNED NOT NULL,
    fila        CHAR(2) NOT NULL,               -- 'A', 'B', ... 'Z'
    numero      TINYINT UNSIGNED NOT NULL,
    tipo        ENUM('estandar','preferencial','discapacitado') NOT NULL DEFAULT 'estandar',
    CONSTRAINT fk_asiento_sala  FOREIGN KEY (id_sala) REFERENCES sala(id_sala),
    CONSTRAINT uq_asiento       UNIQUE (id_sala, fila, numero)  -- no duplicar butaca
);

-- ============================================================
-- 7. CARTELERA / FUNCIÓN  →  Película + Sala + Fecha + Hora
-- ============================================================

CREATE TABLE funcion (
    id_funcion      INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_pelicula     INT UNSIGNED NOT NULL,
    id_sala         INT UNSIGNED NOT NULL,
    fecha           DATE NOT NULL,
    hora_inicio     TIME NOT NULL,
    hora_fin        TIME NOT NULL,              -- calculada al insertar
    precio_base     DECIMAL(8,2) NOT NULL,
    estado          ENUM('programada','en_curso','finalizada','cancelada') NOT NULL DEFAULT 'programada',
    CONSTRAINT fk_funcion_pelicula  FOREIGN KEY (id_pelicula) REFERENCES pelicula(id_pelicula),
    CONSTRAINT fk_funcion_sala      FOREIGN KEY (id_sala)     REFERENCES sala(id_sala),
    -- Evita que la misma sala tenga dos funciones solapadas en la misma fecha+hora
    CONSTRAINT uq_funcion           UNIQUE (id_sala, fecha, hora_inicio)
);

-- ============================================================
-- 8. RESERVAS
-- ============================================================

CREATE TABLE reserva (
    id_reserva      INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_usuario      INT UNSIGNED NOT NULL,
    fecha_reserva   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    total           DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    estado          ENUM('pendiente','confirmada','cancelada','pagada') NOT NULL DEFAULT 'pendiente',
    CONSTRAINT fk_reserva_usuario FOREIGN KEY (id_usuario) REFERENCES usuario(id_usuario)
);

-- ============================================================
-- 9. DETALLE DE RESERVA  →  RESTRICCIÓN CRÍTICA: no vender el mismo asiento dos veces
-- ============================================================

CREATE TABLE detalle_reserva (
    id_detalle      INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_reserva      INT UNSIGNED NOT NULL,
    id_funcion      INT UNSIGNED NOT NULL,
    id_asiento      INT UNSIGNED NOT NULL,
    precio_unitario DECIMAL(8,2) NOT NULL,
    -- ★ RESTRICCIÓN CRÍTICA: un asiento sólo puede venderse UNA VEZ por función
    CONSTRAINT uq_asiento_funcion   UNIQUE (id_funcion, id_asiento),
    CONSTRAINT fk_detalle_reserva   FOREIGN KEY (id_reserva)  REFERENCES reserva(id_reserva),
    CONSTRAINT fk_detalle_funcion   FOREIGN KEY (id_funcion)  REFERENCES funcion(id_funcion),
    CONSTRAINT fk_detalle_asiento   FOREIGN KEY (id_asiento)  REFERENCES asiento(id_asiento)
);

-- ============================================================
-- 10. PROMOCIONES DE ALIMENTOS Y BEBIDAS
-- ============================================================

CREATE TABLE producto (
    id_producto     INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    nombre          VARCHAR(150) NOT NULL,
    descripcion     TEXT,
    precio          DECIMAL(8,2) NOT NULL,
    categoria       ENUM('comida','bebida','combo') NOT NULL DEFAULT 'combo',
    imagen_url      VARCHAR(500),
    disponible      TINYINT(1) NOT NULL DEFAULT 1
);

CREATE TABLE promocion (
    id_promocion INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

    nombre VARCHAR(150) NOT NULL,

    descripcion TEXT,

    imagen VARCHAR(255),

    tipo ENUM(
        'ENTRADA',
        'COMBO',
        'PRODUCTO',
        'GENERAL'
    ) NOT NULL,

    tipo_descuento ENUM(
        'PORCENTAJE',
        'PRECIO_FIJO'
    ) NOT NULL,

    valor_descuento DECIMAL(8,2) NOT NULL,

    -- cantidad mínima de items para que la promo se active (ej: 2x1 exige 2 entradas)
    cantidad_minima SMALLINT UNSIGNED NOT NULL DEFAULT 1,

    -- si no es NULL, la promo solo aplica a funciones en salas de este tipo
    aplica_tipo_sala ENUM('2D','3D','IMAX','4DX') NULL DEFAULT NULL,

    fecha_inicio DATE NOT NULL,

    fecha_fin DATE NOT NULL,

    stock INT DEFAULT NULL,

    activo TINYINT(1) DEFAULT 1,

    CONSTRAINT chk_promocion_fechas CHECK (fecha_inicio <= fecha_fin),
    CONSTRAINT chk_promocion_porcentaje CHECK (
        tipo_descuento <> 'PORCENTAJE' OR (valor_descuento BETWEEN 0 AND 100)
    )
);

CREATE INDEX idx_promocion_vigencia ON promocion (activo, fecha_inicio, fecha_fin);

CREATE TABLE promocion_producto (

    id_pp INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

    id_promocion INT UNSIGNED NOT NULL,

    id_producto INT UNSIGNED NOT NULL,

    cantidad SMALLINT DEFAULT 1,

    obligatorio TINYINT DEFAULT 1,

    FOREIGN KEY(id_promocion)
        REFERENCES promocion(id_promocion),

    FOREIGN KEY(id_producto)
        REFERENCES producto(id_producto)
);

CREATE TABLE promocion_dia (

    id INT AUTO_INCREMENT PRIMARY KEY,

    id_promocion INT UNSIGNED,

    dia ENUM(
        'LUNES',
        'MARTES',
        'MIERCOLES',
        'JUEVES',
        'VIERNES',
        'SABADO',
        'DOMINGO'
    ),

    FOREIGN KEY(id_promocion)
        REFERENCES promocion(id_promocion)
);

CREATE TABLE reserva_producto (
    id_rp           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_reserva      INT UNSIGNED NOT NULL,
    id_producto     INT UNSIGNED NOT NULL,
    id_promocion    INT UNSIGNED,               -- NULL si no aplica promo
    cantidad        TINYINT UNSIGNED NOT NULL DEFAULT 1,
    precio_unitario DECIMAL(8,2) NOT NULL,
    CONSTRAINT fk_rp_reserva    FOREIGN KEY (id_reserva)   REFERENCES reserva(id_reserva),
    CONSTRAINT fk_rp_producto   FOREIGN KEY (id_producto)  REFERENCES producto(id_producto),
    CONSTRAINT fk_rp_promocion  FOREIGN KEY (id_promocion) REFERENCES promocion(id_promocion)
);

-- ============================================================
-- 11. PAGOS
-- ============================================================

CREATE TABLE pago (
    id_pago         INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    id_reserva      INT UNSIGNED NOT NULL,
    monto           DECIMAL(10,2) NOT NULL,
    metodo          ENUM('efectivo','tarjeta','yape','plin','transferencia') NOT NULL,
    estado          ENUM('pendiente','aprobado','rechazado','reembolsado') NOT NULL DEFAULT 'pendiente',
    fecha_pago      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    referencia      VARCHAR(100),               -- código de operación
    CONSTRAINT fk_pago_reserva FOREIGN KEY (id_reserva) REFERENCES reserva(id_reserva)
);

-- ============================================================
--  INSERCIONES DE EJEMPLO
-- ============================================================

-- Roles
INSERT INTO rol (nombre, descripcion) VALUES
  ('administrador', 'Control total del sistema'),
  ('empleado',      'Gestión de boletería y funciones'),
  ('cliente',       'Compra de entradas y reservas');

-- Usuarios
INSERT INTO usuario (id_rol, nombres, apellidos, email, password, telefono) VALUES
  (1, 'Carlos',  'Ramirez',  'admin@cine.pe',    '$2y$10$HRsLhiCOKzchUIY6SKDZ1e0xzYap4TZo.ggqMJwxfmfgDfTi7yNm.',    '999000001'),
  (2, 'Lucia',   'Torres',   'empleado@cine.pe', '$2y$10$lk/P/Yeuw2wUxFopjv7uluPsTpKaUwGRCNou9fiRATzXSdJAq7gBO', '999000002'),
  (3, 'Gianmarco','Flores',  'cliente@cine.pe',  '$2y$10$lk/P/Yeuw2wUxFopjv7uluPsTpKaUwGRCNou9fiRATzXSdJAq7gBO',  '999000003'),
  (3, 'Andrea',  'Paredes',  'andrea@cine.pe',   '$2y$10$lk/P/Yeuw2wUxFopjv7uluPsTpKaUwGRCNou9fiRATzXSdJAq7gBO',   '999000004');

-- Membresías
INSERT INTO membresia (nombre, descuento_pct, puntos_por_sol, precio_mensual) VALUES
  ('Básica',  0.00, 1.00,  0.00),
  ('Silver', 10.00, 1.50, 15.00),
  ('Gold',   20.00, 2.00, 30.00);

-- Asignar membresía a clientes
INSERT INTO cliente_membresia (id_usuario, id_membresia, puntos, fecha_inicio) VALUES
  (3, 2, 120, '2025-01-15'),
  (4, 1,   0, '2025-03-10');

-- Géneros
INSERT INTO genero (nombre) VALUES
  ('Acción'), ('Drama'), ('Comedia'), ('Terror'), ('Ciencia Ficción'), ('Animación');

-- Películas
INSERT INTO pelicula (id_genero, titulo, sinopsis, duracion_min, clasificacion, idioma, poster_url, trailer_url) VALUES

  -- Las 4 originales
  (5, 'Dune: Parte 3', 'La saga continúa en Arrakis.', 145, 'PG-13', 'Español', 'dune_parte_3.jpg', 'https://youtube.com/watch?v=dune3-trailer'),
  (1, 'Fast & Furious 11', 'La familia regresa en su última misión.', 130, 'PG-13', 'Español', 'fast_and_furious_11.jpg', 'https://youtube.com/watch?v=fast11-trailer'),
  (6, 'Inside Out 3', 'Nuevas emociones en la mente de Riley.', 110, 'G', 'Español', 'inside_out_3.jpg', 'https://youtube.com/watch?v=insideout3-trailer'),
  (4, 'Terrifier 4', 'El payaso regresa más aterrador que nunca.', 100, 'R', 'Subtitulado', 'terrifier_4.jpg', 'https://youtube.com/watch?v=terrifier4-trailer'),

  -- 1. Acción
  (1, 'The Dark Knight', 'Batman se enfrenta al Joker, un cerebro criminal que desata el caos en Gotham.', 152, 'PG-13', 'Subtitulado', 'the_dark_knight.jpg', 'https://youtube.com/watch?v=EXeTwQWrcwY'),
  (1, 'Gladiator', 'Un general romano traicionado regresa a Roma como gladiador para vengar a su familia.', 155, 'R', 'Español', 'gladiator.jpg', 'https://youtube.com/watch?v=MfM79O9O2MA'),
  (1, 'John Wick', 'Un exasesino a sueldo sale del retiro para perseguir a los gánsteres que le quitaron todo.', 101, 'R', 'Subtitulado', 'john_wick.jpg', 'https://youtube.com/watch?v=2AUmvWm5ZDQ'),
  (1, 'Mad Max: Fury Road', 'En un futuro postapocalíptico, una mujer se rebela contra un tirano en busca de su hogar.', 120, 'R', 'Español', 'mad_max_fury_road.jpg', 'https://youtube.com/watch?v=hEJnMQG9ev8'),

  -- 2. Drama
  (2, 'The Godfather', 'La historia de una envejecida dinastía del crimen organizado en Nueva York y el ascenso de su hijo.', 175, 'R', 'Subtitulado', 'the_godfather.jpg', 'https://youtube.com/watch?v=1x0GpEZnwa8'),
  (2, 'Forrest Gump', 'Las presidencias de Kennedy y Johnson, Vietnam y otros eventos históricos se desarrollan desde la perspectiva de un hombre de Alabama.', 142, 'PG-13', 'Español', 'forrest_gump.jpg', 'https://youtube.com/watch?v=XHhAG-YLdk8'),
  (2, 'Titanic', 'Un romance de aristócrata y artista a bordo del lujoso e infortunado transatlántico.', 194, 'PG-13', 'Español', 'titanic.jpg', 'https://youtube.com/watch?v=LuPB43YSgCs'),
  (2, 'The Shawshank Redemption', 'Dos hombres encarcelados entablan una amistad a lo largo de los años, encontrando consuelo y redención.', 142, 'R', 'Subtitulado', 'the_shawshank_redemption.jpg', 'https://youtube.com/watch?v=PLl99DlL6b4'),

  -- 3. Comedia
  (3, 'The Hangover', 'Tres amigos buscan a su prometido perdido tras una salvaje despedida de soltero en Las Vegas.', 100, 'R', 'Español', 'the_hangover.jpg', 'https://youtube.com/watch?v=tcdUhdOlz9M'),
  (3, 'Superbad', 'Dos amigos de la escuela secundaria dependientes enfrentan una ansiedad por la separación antes de ir a la universidad.', 113, 'R', 'Subtitulado', 'superbad.jpg', 'https://youtube.com/watch?v=4eaZ_48ZYog'),
  (3, 'White Chicks', 'Dos agentes del FBI se hacen pasar por chicas ricas de la alta sociedad para resolver un caso de secuestro.', 109, 'PG-13', 'Español', 'white_chicks.jpg', 'https://youtube.com/watch?v=SUhWkzJG3hQ'),

  -- 4. Terror
  (4, 'The Conjuring', 'Investigadores paranormales trabajan para ayudar a una familia aterrorizada por una presencia oscura en su granja.', 112, 'R', 'Español', 'the_conjuring.jpg', 'https://youtube.com/watch?v=k10ETZ41q5o'),
  (4, 'It (Eso)', 'En el verano de 1989, un grupo de niños marginados se une para destruir a un monstruo que cambia de forma.', 135, 'R', 'Subtitulado', 'it.jpg', 'https://youtube.com/watch?v=I2sCbO8YREs'),
  (4, 'A Quiet Place', 'Una familia debe vivir en silencio para evadir a misteriosas criaturas ciegas que cazan por el sonido.', 90, 'PG-13', 'Subtitulado', 'a_quiet_place.jpg', 'https://youtube.com/watch?v=WR7cc5t7tv8'),

  -- 5. Ciencia Ficción
  (5, 'Inception', 'Un ladrón que roba secretos corporativos a través del uso de la tecnología de compartir sueños es encomendado con la tarea inversa.', 148, 'PG-13', 'Subtitulado', 'inception.jpg', 'https://youtube.com/watch?v=YoHD9XEInc0'),
  (5, 'The Matrix', 'Un programador de computación descubre que el mundo en el que vive es en realidad una simulación virtual controlada por máquinas.', 136, 'R', 'Español', 'the_matrix.jpg', 'https://youtube.com/watch?v=vKQi3bBA1y8'),
  (5, 'Interstellar', 'Un equipo de exploradores viaja a través de un agujero de gusano en el espacio en un intento por asegurar la supervivencia de la humanidad.', 169, 'PG-13', 'Subtitulado', 'interstellar.jpg', 'https://youtube.com/watch?v=zSWdZVtXT7E'),
  (5, 'Avatar', 'Un marino parapléjico enviado al planeta Pandora en una misión única se debate entre seguir órdenes y proteger el mundo que siente como su hogar.', 162, 'PG-13', 'Español', 'avatar.jpg', 'https://youtube.com/watch?v=5PSNL1qE6VY'),

  -- 6. Animación
  (6, 'Toy Story', 'Un muñeco vaquero de cuerda se siente profundamente amenazado y celoso cuando un nuevo juguete astronauta lo suplanta.', 81, 'G', 'Español', 'toy_story.jpg', 'https://youtube.com/watch?v=ZA0FGZQb1-w'),
  (6, 'The Lion King', 'Un joven cachorro de león huye de su reino tras la muerte de su padre, solo para aprender el verdadero significado de la responsabilidad y el destino.', 88, 'G', 'Español', 'the_lion_king.jpg', 'https://youtube.com/watch?v=lFzVJEksoDY');


-- Locales
INSERT INTO local (nombre, direccion, distrito, ciudad) VALUES
  ('CinePlex Miraflores', 'Av. Larco 345',         'Miraflores', 'Lima'),
  ('CinePlex San Miguel', 'CC Plaza San Miguel L2', 'San Miguel', 'Lima'),
  ('CinePlex Callao',     'Av. Sáenz Peña 850',    'Callao',     'Lima');

-- Salas (cada local tiene varias)
INSERT INTO sala (id_local, nombre, tipo, capacidad) VALUES
  -- Local 1: Miraflores
  (1, 'Sala 1', '2D',   80),
  (1, 'Sala 2', '3D',   60),
  (1, 'IMAX',   'IMAX', 120),
  -- Local 2: San Miguel
  (2, 'Sala A', '2D',   90),
  (2, 'Sala B', '3D',   70),
  -- Local 3: Callao
  (3, 'Sala 1', '2D',   75),
  (3, 'Sala 2', '4DX',  50);

-- Asientos para TODAS las salas
-- Sala 1 (80 asientos): 8 filas x 10 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (1,'A',1,'estandar'),(1,'A',2,'estandar'),(1,'A',3,'estandar'),(1,'A',4,'estandar'),(1,'A',5,'estandar'),(1,'A',6,'estandar'),(1,'A',7,'estandar'),(1,'A',8,'estandar'),(1,'A',9,'estandar'),(1,'A',10,'estandar'),
  (1,'B',1,'estandar'),(1,'B',2,'estandar'),(1,'B',3,'estandar'),(1,'B',4,'estandar'),(1,'B',5,'preferencial'),(1,'B',6,'preferencial'),(1,'B',7,'estandar'),(1,'B',8,'estandar'),(1,'B',9,'estandar'),(1,'B',10,'estandar'),
  (1,'C',1,'estandar'),(1,'C',2,'estandar'),(1,'C',3,'estandar'),(1,'C',4,'estandar'),(1,'C',5,'preferencial'),(1,'C',6,'preferencial'),(1,'C',7,'estandar'),(1,'C',8,'estandar'),(1,'C',9,'estandar'),(1,'C',10,'estandar'),
  (1,'D',1,'estandar'),(1,'D',2,'estandar'),(1,'D',3,'estandar'),(1,'D',4,'estandar'),(1,'D',5,'preferencial'),(1,'D',6,'preferencial'),(1,'D',7,'estandar'),(1,'D',8,'estandar'),(1,'D',9,'estandar'),(1,'D',10,'estandar'),
  (1,'E',1,'estandar'),(1,'E',2,'estandar'),(1,'E',3,'estandar'),(1,'E',4,'estandar'),(1,'E',5,'preferencial'),(1,'E',6,'preferencial'),(1,'E',7,'estandar'),(1,'E',8,'estandar'),(1,'E',9,'estandar'),(1,'E',10,'estandar'),
  (1,'F',1,'estandar'),(1,'F',2,'estandar'),(1,'F',3,'estandar'),(1,'F',4,'estandar'),(1,'F',5,'discapacitado'),(1,'F',6,'discapacitado'),(1,'F',7,'estandar'),(1,'F',8,'estandar'),(1,'F',9,'estandar'),(1,'F',10,'estandar'),
  (1,'G',1,'estandar'),(1,'G',2,'estandar'),(1,'G',3,'estandar'),(1,'G',4,'estandar'),(1,'G',5,'estandar'),(1,'G',6,'estandar'),(1,'G',7,'estandar'),(1,'G',8,'estandar'),(1,'G',9,'estandar'),(1,'G',10,'estandar'),
  (1,'H',1,'estandar'),(1,'H',2,'estandar'),(1,'H',3,'estandar'),(1,'H',4,'estandar'),(1,'H',5,'estandar'),(1,'H',6,'estandar'),(1,'H',7,'estandar'),(1,'H',8,'estandar'),(1,'H',9,'estandar'),(1,'H',10,'estandar');

-- Sala 2 (60 asientos): 6 filas x 10 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (2,'A',1,'estandar'),(2,'A',2,'estandar'),(2,'A',3,'estandar'),(2,'A',4,'estandar'),(2,'A',5,'estandar'),(2,'A',6,'estandar'),(2,'A',7,'estandar'),(2,'A',8,'estandar'),(2,'A',9,'estandar'),(2,'A',10,'estandar'),
  (2,'B',1,'estandar'),(2,'B',2,'estandar'),(2,'B',3,'estandar'),(2,'B',4,'preferencial'),(2,'B',5,'preferencial'),(2,'B',6,'preferencial'),(2,'B',7,'estandar'),(2,'B',8,'estandar'),(2,'B',9,'estandar'),(2,'B',10,'estandar'),
  (2,'C',1,'estandar'),(2,'C',2,'estandar'),(2,'C',3,'estandar'),(2,'C',4,'preferencial'),(2,'C',5,'preferencial'),(2,'C',6,'preferencial'),(2,'C',7,'estandar'),(2,'C',8,'estandar'),(2,'C',9,'estandar'),(2,'C',10,'estandar'),
  (2,'D',1,'estandar'),(2,'D',2,'estandar'),(2,'D',3,'estandar'),(2,'D',4,'preferencial'),(2,'D',5,'preferencial'),(2,'D',6,'preferencial'),(2,'D',7,'estandar'),(2,'D',8,'estandar'),(2,'D',9,'estandar'),(2,'D',10,'estandar'),
  (2,'E',1,'estandar'),(2,'E',2,'estandar'),(2,'E',3,'estandar'),(2,'E',4,'estandar'),(2,'E',5,'estandar'),(2,'E',6,'estandar'),(2,'E',7,'estandar'),(2,'E',8,'estandar'),(2,'E',9,'estandar'),(2,'E',10,'estandar'),
  (2,'F',1,'estandar'),(2,'F',2,'estandar'),(2,'F',3,'estandar'),(2,'F',4,'estandar'),(2,'F',5,'estandar'),(2,'F',6,'estandar'),(2,'F',7,'estandar'),(2,'F',8,'estandar'),(2,'F',9,'estandar'),(2,'F',10,'estandar');

-- Sala 3 IMAX (120 asientos): 10 filas x 12 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (3,'A',1,'estandar'),(3,'A',2,'estandar'),(3,'A',3,'estandar'),(3,'A',4,'estandar'),(3,'A',5,'estandar'),(3,'A',6,'estandar'),(3,'A',7,'estandar'),(3,'A',8,'estandar'),(3,'A',9,'estandar'),(3,'A',10,'estandar'),(3,'A',11,'estandar'),(3,'A',12,'estandar'),
  (3,'B',1,'estandar'),(3,'B',2,'estandar'),(3,'B',3,'estandar'),(3,'B',4,'estandar'),(3,'B',5,'estandar'),(3,'B',6,'estandar'),(3,'B',7,'estandar'),(3,'B',8,'estandar'),(3,'B',9,'estandar'),(3,'B',10,'estandar'),(3,'B',11,'estandar'),(3,'B',12,'estandar'),
  (3,'C',1,'estandar'),(3,'C',2,'estandar'),(3,'C',3,'estandar'),(3,'C',4,'preferencial'),(3,'C',5,'preferencial'),(3,'C',6,'preferencial'),(3,'C',7,'preferencial'),(3,'C',8,'estandar'),(3,'C',9,'estandar'),(3,'C',10,'estandar'),(3,'C',11,'estandar'),(3,'C',12,'estandar'),
  (3,'D',1,'estandar'),(3,'D',2,'estandar'),(3,'D',3,'estandar'),(3,'D',4,'preferencial'),(3,'D',5,'preferencial'),(3,'D',6,'preferencial'),(3,'D',7,'preferencial'),(3,'D',8,'estandar'),(3,'D',9,'estandar'),(3,'D',10,'estandar'),(3,'D',11,'estandar'),(3,'D',12,'estandar'),
  (3,'E',1,'estandar'),(3,'E',2,'estandar'),(3,'E',3,'estandar'),(3,'E',4,'preferencial'),(3,'E',5,'preferencial'),(3,'E',6,'preferencial'),(3,'E',7,'preferencial'),(3,'E',8,'estandar'),(3,'E',9,'estandar'),(3,'E',10,'estandar'),(3,'E',11,'estandar'),(3,'E',12,'estandar'),
  (3,'F',1,'estandar'),(3,'F',2,'estandar'),(3,'F',3,'estandar'),(3,'F',4,'preferencial'),(3,'F',5,'preferencial'),(3,'F',6,'preferencial'),(3,'F',7,'preferencial'),(3,'F',8,'estandar'),(3,'F',9,'estandar'),(3,'F',10,'estandar'),(3,'F',11,'estandar'),(3,'F',12,'estandar'),
  (3,'G',1,'estandar'),(3,'G',2,'estandar'),(3,'G',3,'estandar'),(3,'G',4,'estandar'),(3,'G',5,'estandar'),(3,'G',6,'estandar'),(3,'G',7,'estandar'),(3,'G',8,'estandar'),(3,'G',9,'estandar'),(3,'G',10,'estandar'),(3,'G',11,'estandar'),(3,'G',12,'estandar'),
  (3,'H',1,'estandar'),(3,'H',2,'estandar'),(3,'H',3,'estandar'),(3,'H',4,'estandar'),(3,'H',5,'estandar'),(3,'H',6,'estandar'),(3,'H',7,'estandar'),(3,'H',8,'estandar'),(3,'H',9,'estandar'),(3,'H',10,'estandar'),(3,'H',11,'estandar'),(3,'H',12,'estandar'),
  (3,'I',1,'estandar'),(3,'I',2,'estandar'),(3,'I',3,'estandar'),(3,'I',4,'estandar'),(3,'I',5,'estandar'),(3,'I',6,'estandar'),(3,'I',7,'estandar'),(3,'I',8,'estandar'),(3,'I',9,'estandar'),(3,'I',10,'estandar'),(3,'I',11,'estandar'),(3,'I',12,'estandar'),
  (3,'J',1,'estandar'),(3,'J',2,'estandar'),(3,'J',3,'estandar'),(3,'J',4,'estandar'),(3,'J',5,'estandar'),(3,'J',6,'estandar'),(3,'J',7,'estandar'),(3,'J',8,'estandar'),(3,'J',9,'estandar'),(3,'J',10,'estandar'),(3,'J',11,'estandar'),(3,'J',12,'estandar');

-- Sala 4 (90 asientos): 9 filas x 10 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (4,'A',1,'estandar'),(4,'A',2,'estandar'),(4,'A',3,'estandar'),(4,'A',4,'estandar'),(4,'A',5,'estandar'),(4,'A',6,'estandar'),(4,'A',7,'estandar'),(4,'A',8,'estandar'),(4,'A',9,'estandar'),(4,'A',10,'estandar'),
  (4,'B',1,'estandar'),(4,'B',2,'estandar'),(4,'B',3,'estandar'),(4,'B',4,'estandar'),(4,'B',5,'estandar'),(4,'B',6,'estandar'),(4,'B',7,'estandar'),(4,'B',8,'estandar'),(4,'B',9,'estandar'),(4,'B',10,'estandar'),
  (4,'C',1,'estandar'),(4,'C',2,'estandar'),(4,'C',3,'estandar'),(4,'C',4,'preferencial'),(4,'C',5,'preferencial'),(4,'C',6,'preferencial'),(4,'C',7,'estandar'),(4,'C',8,'estandar'),(4,'C',9,'estandar'),(4,'C',10,'estandar'),
  (4,'D',1,'estandar'),(4,'D',2,'estandar'),(4,'D',3,'estandar'),(4,'D',4,'preferencial'),(4,'D',5,'preferencial'),(4,'D',6,'preferencial'),(4,'D',7,'estandar'),(4,'D',8,'estandar'),(4,'D',9,'estandar'),(4,'D',10,'estandar'),
  (4,'E',1,'estandar'),(4,'E',2,'estandar'),(4,'E',3,'estandar'),(4,'E',4,'preferencial'),(4,'E',5,'preferencial'),(4,'E',6,'preferencial'),(4,'E',7,'estandar'),(4,'E',8,'estandar'),(4,'E',9,'estandar'),(4,'E',10,'estandar'),
  (4,'F',1,'estandar'),(4,'F',2,'estandar'),(4,'F',3,'estandar'),(4,'F',4,'estandar'),(4,'F',5,'estandar'),(4,'F',6,'estandar'),(4,'F',7,'estandar'),(4,'F',8,'estandar'),(4,'F',9,'estandar'),(4,'F',10,'estandar'),
  (4,'G',1,'estandar'),(4,'G',2,'estandar'),(4,'G',3,'estandar'),(4,'G',4,'estandar'),(4,'G',5,'estandar'),(4,'G',6,'estandar'),(4,'G',7,'estandar'),(4,'G',8,'estandar'),(4,'G',9,'estandar'),(4,'G',10,'estandar'),
  (4,'H',1,'estandar'),(4,'H',2,'estandar'),(4,'H',3,'estandar'),(4,'H',4,'estandar'),(4,'H',5,'discapacitado'),(4,'H',6,'discapacitado'),(4,'H',7,'estandar'),(4,'H',8,'estandar'),(4,'H',9,'estandar'),(4,'H',10,'estandar'),
  (4,'I',1,'estandar'),(4,'I',2,'estandar'),(4,'I',3,'estandar'),(4,'I',4,'estandar'),(4,'I',5,'estandar'),(4,'I',6,'estandar'),(4,'I',7,'estandar'),(4,'I',8,'estandar'),(4,'I',9,'estandar'),(4,'I',10,'estandar');

-- Sala 5 (70 asientos): 7 filas x 10 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (5,'A',1,'estandar'),(5,'A',2,'estandar'),(5,'A',3,'estandar'),(5,'A',4,'estandar'),(5,'A',5,'estandar'),(5,'A',6,'estandar'),(5,'A',7,'estandar'),(5,'A',8,'estandar'),(5,'A',9,'estandar'),(5,'A',10,'estandar'),
  (5,'B',1,'estandar'),(5,'B',2,'estandar'),(5,'B',3,'estandar'),(5,'B',4,'preferencial'),(5,'B',5,'preferencial'),(5,'B',6,'preferencial'),(5,'B',7,'estandar'),(5,'B',8,'estandar'),(5,'B',9,'estandar'),(5,'B',10,'estandar'),
  (5,'C',1,'estandar'),(5,'C',2,'estandar'),(5,'C',3,'estandar'),(5,'C',4,'preferencial'),(5,'C',5,'preferencial'),(5,'C',6,'preferencial'),(5,'C',7,'estandar'),(5,'C',8,'estandar'),(5,'C',9,'estandar'),(5,'C',10,'estandar'),
  (5,'D',1,'estandar'),(5,'D',2,'estandar'),(5,'D',3,'estandar'),(5,'D',4,'preferencial'),(5,'D',5,'preferencial'),(5,'D',6,'preferencial'),(5,'D',7,'estandar'),(5,'D',8,'estandar'),(5,'D',9,'estandar'),(5,'D',10,'estandar'),
  (5,'E',1,'estandar'),(5,'E',2,'estandar'),(5,'E',3,'estandar'),(5,'E',4,'estandar'),(5,'E',5,'estandar'),(5,'E',6,'estandar'),(5,'E',7,'estandar'),(5,'E',8,'estandar'),(5,'E',9,'estandar'),(5,'E',10,'estandar'),
  (5,'F',1,'estandar'),(5,'F',2,'estandar'),(5,'F',3,'estandar'),(5,'F',4,'estandar'),(5,'F',5,'estandar'),(5,'F',6,'estandar'),(5,'F',7,'estandar'),(5,'F',8,'estandar'),(5,'F',9,'estandar'),(5,'F',10,'estandar'),
  (5,'G',1,'estandar'),(5,'G',2,'estandar'),(5,'G',3,'estandar'),(5,'G',4,'estandar'),(5,'G',5,'estandar'),(5,'G',6,'estandar'),(5,'G',7,'estandar'),(5,'G',8,'estandar'),(5,'G',9,'estandar'),(5,'G',10,'estandar');

-- Sala 6 (75 asientos): 7 filas x 11 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (6,'A',1,'estandar'),(6,'A',2,'estandar'),(6,'A',3,'estandar'),(6,'A',4,'estandar'),(6,'A',5,'estandar'),(6,'A',6,'estandar'),(6,'A',7,'estandar'),(6,'A',8,'estandar'),(6,'A',9,'estandar'),(6,'A',10,'estandar'),(6,'A',11,'estandar'),
  (6,'B',1,'estandar'),(6,'B',2,'estandar'),(6,'B',3,'estandar'),(6,'B',4,'estandar'),(6,'B',5,'estandar'),(6,'B',6,'estandar'),(6,'B',7,'estandar'),(6,'B',8,'estandar'),(6,'B',9,'estandar'),(6,'B',10,'estandar'),(6,'B',11,'estandar'),
  (6,'C',1,'estandar'),(6,'C',2,'estandar'),(6,'C',3,'estandar'),(6,'C',4,'preferencial'),(6,'C',5,'preferencial'),(6,'C',6,'preferencial'),(6,'C',7,'preferencial'),(6,'C',8,'estandar'),(6,'C',9,'estandar'),(6,'C',10,'estandar'),(6,'C',11,'estandar'),
  (6,'D',1,'estandar'),(6,'D',2,'estandar'),(6,'D',3,'estandar'),(6,'D',4,'preferencial'),(6,'D',5,'preferencial'),(6,'D',6,'preferencial'),(6,'D',7,'preferencial'),(6,'D',8,'estandar'),(6,'D',9,'estandar'),(6,'D',10,'estandar'),(6,'D',11,'estandar'),
  (6,'E',1,'estandar'),(6,'E',2,'estandar'),(6,'E',3,'estandar'),(6,'E',4,'estandar'),(6,'E',5,'estandar'),(6,'E',6,'estandar'),(6,'E',7,'estandar'),(6,'E',8,'estandar'),(6,'E',9,'estandar'),(6,'E',10,'estandar'),(6,'E',11,'estandar'),
  (6,'F',1,'estandar'),(6,'F',2,'estandar'),(6,'F',3,'estandar'),(6,'F',4,'estandar'),(6,'F',5,'discapacitado'),(6,'F',6,'discapacitado'),(6,'F',7,'estandar'),(6,'F',8,'estandar'),(6,'F',9,'estandar'),(6,'F',10,'estandar'),(6,'F',11,'estandar'),
  (6,'G',1,'estandar'),(6,'G',2,'estandar'),(6,'G',3,'estandar'),(6,'G',4,'estandar'),(6,'G',5,'estandar'),(6,'G',6,'estandar'),(6,'G',7,'estandar'),(6,'G',8,'estandar'),(6,'G',9,'estandar'),(6,'G',10,'estandar'),(6,'G',11,'estandar');

-- Sala 7 (50 asientos): 5 filas x 10 asientos
INSERT INTO asiento (id_sala, fila, numero, tipo) VALUES
  (7,'A',1,'estandar'),(7,'A',2,'estandar'),(7,'A',3,'estandar'),(7,'A',4,'estandar'),(7,'A',5,'estandar'),(7,'A',6,'estandar'),(7,'A',7,'estandar'),(7,'A',8,'estandar'),(7,'A',9,'estandar'),(7,'A',10,'estandar'),
  (7,'B',1,'estandar'),(7,'B',2,'estandar'),(7,'B',3,'estandar'),(7,'B',4,'estandar'),(7,'B',5,'estandar'),(7,'B',6,'estandar'),(7,'B',7,'estandar'),(7,'B',8,'estandar'),(7,'B',9,'estandar'),(7,'B',10,'estandar'),
  (7,'C',1,'estandar'),(7,'C',2,'estandar'),(7,'C',3,'estandar'),(7,'C',4,'preferencial'),(7,'C',5,'preferencial'),(7,'C',6,'preferencial'),(7,'C',7,'estandar'),(7,'C',8,'estandar'),(7,'C',9,'estandar'),(7,'C',10,'estandar'),
  (7,'D',1,'estandar'),(7,'D',2,'estandar'),(7,'D',3,'estandar'),(7,'D',4,'preferencial'),(7,'D',5,'preferencial'),(7,'D',6,'preferencial'),(7,'D',7,'estandar'),(7,'D',8,'estandar'),(7,'D',9,'estandar'),(7,'D',10,'estandar'),
  (7,'E',1,'estandar'),(7,'E',2,'estandar'),(7,'E',3,'estandar'),(7,'E',4,'estandar'),(7,'E',5,'estandar'),(7,'E',6,'estandar'),(7,'E',7,'estandar'),(7,'E',8,'estandar'),(7,'E',9,'estandar'),(7,'E',10,'estandar');

-- Funciones para todas las películas - distribuyendo horarios y salas
INSERT INTO funcion (id_pelicula, id_sala, fecha, hora_inicio, hora_fin, precio_base) VALUES
  -- Película 1: Dune: Parte 3
  (1, 1, '2025-07-10', '14:00:00', '16:25:00', 22.00),
  (1, 3, '2025-07-10', '16:30:00', '18:55:00', 35.00),
  (1, 4, '2025-07-10', '19:00:00', '21:25:00', 28.00),
  (1, 5, '2025-07-11', '11:00:00', '13:25:00', 22.00),
  -- Película 2: Fast & Furious 11
  (2, 1, '2025-07-10', '16:00:00', '18:10:00', 22.00),
  (2, 2, '2025-07-10', '19:00:00', '21:10:00', 25.00),
  (2, 6, '2025-07-11', '14:00:00', '16:10:00', 22.00),
  (2, 7, '2025-07-11', '19:00:00', '21:10:00', 28.00),
  -- Película 3: Inside Out 3
  (3, 2, '2025-07-11', '11:00:00', '12:50:00', 20.00),
  (3, 5, '2025-07-11', '14:00:00', '15:50:00', 22.00),
  (3, 4, '2025-07-12', '11:00:00', '12:50:00', 20.00),
  (3, 1, '2025-07-12', '16:00:00', '17:50:00', 22.00),
  -- Película 4: Terrifier 4
  (4, 6, '2025-07-11', '21:30:00', '23:10:00', 25.00),
  (4, 7, '2025-07-12', '21:30:00', '23:10:00', 28.00),
  (4, 3, '2025-07-13', '19:00:00', '20:40:00', 35.00),
  (4, 2, '2025-07-13', '21:00:00', '22:40:00', 25.00),
  -- Película 5: The Dark Knight
  (5, 1, '2025-07-12', '19:00:00', '21:32:00', 22.00),
  (5, 3, '2025-07-12', '14:00:00', '16:32:00', 35.00),
  (5, 4, '2025-07-13', '14:00:00', '16:32:00', 28.00),
  (5, 5, '2025-07-13', '19:00:00', '21:32:00', 22.00),
  -- Película 6: Gladiator
  (6, 2, '2025-07-12', '16:00:00', '18:35:00', 25.00),
  (6, 4, '2025-07-12', '14:00:00', '16:35:00', 28.00),
  (6, 6, '2025-07-13', '16:00:00', '18:35:00', 22.00),
  (6, 7, '2025-07-14', '14:00:00', '16:35:00', 28.00),
  -- Película 7: John Wick
  (7, 1, '2025-07-13', '14:00:00', '15:41:00', 22.00),
  (7, 5, '2025-07-13', '14:00:00', '15:41:00', 22.00),
  (7, 3, '2025-07-14', '16:30:00', '18:11:00', 35.00),
  (7, 4, '2025-07-14', '19:00:00', '20:41:00', 28.00),
  -- Película 8: Mad Max: Fury Road
  (8, 2, '2025-07-13', '14:00:00', '16:00:00', 25.00),
  (8, 6, '2025-07-13', '19:00:00', '21:00:00', 22.00),
  (8, 7, '2025-07-14', '16:00:00', '18:00:00', 28.00),
  (8, 1, '2025-07-14', '16:00:00', '18:00:00', 22.00),
  -- Película 9: The Godfather
  (9, 3, '2025-07-14', '14:00:00', '16:55:00', 35.00),
  (9, 4, '2025-07-14', '16:00:00', '18:55:00', 28.00),
  (9, 5, '2025-07-15', '14:00:00', '16:55:00', 22.00),
  (9, 2, '2025-07-15', '19:00:00', '21:55:00', 25.00),
  -- Película 10: Forrest Gump
  (10, 1, '2025-07-15', '14:00:00', '16:22:00', 22.00),
  (10, 6, '2025-07-15', '11:00:00', '13:22:00', 22.00),
  (10, 4, '2025-07-15', '19:00:00', '21:22:00', 28.00),
  (10, 7, '2025-07-16', '14:00:00', '16:22:00', 28.00),
  -- Película 11: Titanic
  (11, 3, '2025-07-15', '16:00:00', '18:34:00', 35.00),
  (11, 2, '2025-07-15', '16:00:00', '18:34:00', 25.00),
  (11, 5, '2025-07-16', '11:00:00', '13:34:00', 22.00),
  (11, 6, '2025-07-16', '19:00:00', '21:34:00', 22.00),
  -- Película 12: The Shawshank Redemption
  (12, 1, '2025-07-16', '14:00:00', '16:22:00', 22.00),
  (12, 4, '2025-07-16', '14:00:00', '16:22:00', 28.00),
  (12, 7, '2025-07-16', '19:00:00', '21:22:00', 28.00),
  (12, 2, '2025-07-17', '11:00:00', '13:22:00', 25.00),
  -- Película 13: The Hangover
  (13, 6, '2025-07-16', '16:00:00', '17:40:00', 22.00),
  (13, 3, '2025-07-16', '19:00:00', '20:40:00', 35.00),
  (13, 5, '2025-07-17', '14:00:00', '15:40:00', 22.00),
  (13, 1, '2025-07-17', '19:00:00', '20:40:00', 22.00),
  -- Película 14: Superbad
  (14, 2, '2025-07-16', '19:00:00', '20:53:00', 25.00),
  (14, 4, '2025-07-17', '11:00:00', '12:53:00', 28.00),
  (14, 7, '2025-07-17', '14:00:00', '15:53:00', 28.00),
  (14, 6, '2025-07-18', '14:00:00', '15:53:00', 22.00),
  -- Película 15: White Chicks
  (15, 1, '2025-07-17', '14:00:00', '15:49:00', 22.00),
  (15, 3, '2025-07-17', '16:00:00', '17:49:00', 35.00),
  (15, 5, '2025-07-17', '19:00:00', '20:49:00', 22.00),
  (15, 4, '2025-07-18', '14:00:00', '15:49:00', 28.00),
  -- Película 16: The Conjuring
  (16, 2, '2025-07-17', '16:00:00', '17:52:00', 25.00),
  (16, 6, '2025-07-17', '19:00:00', '20:52:00', 22.00),
  (16, 7, '2025-07-18', '16:00:00', '17:52:00', 28.00),
  (16, 3, '2025-07-18', '19:00:00', '20:52:00', 35.00),
  -- Película 17: It (Eso)
  (17, 1, '2025-07-18', '14:00:00', '16:15:00', 22.00),
  (17, 4, '2025-07-18', '16:00:00', '18:15:00', 28.00),
  (17, 5, '2025-07-19', '11:00:00', '13:15:00', 22.00),
  (17, 2, '2025-07-19', '14:00:00', '16:15:00', 25.00),
  -- Película 18: A Quiet Place
  (18, 6, '2025-07-18', '11:00:00', '12:30:00', 22.00),
  (18, 3, '2025-07-18', '14:00:00', '15:30:00', 35.00),
  (18, 7, '2025-07-19', '14:00:00', '15:30:00', 28.00),
  (18, 1, '2025-07-19', '19:00:00', '20:30:00', 22.00),
  -- Película 19: Inception
  (19, 2, '2025-07-19', '16:00:00', '18:28:00', 25.00),
  (19, 4, '2025-07-19', '19:00:00', '21:28:00', 28.00),
  (19, 3, '2025-07-20', '14:00:00', '16:28:00', 35.00),
  (19, 5, '2025-07-20', '16:00:00', '18:28:00', 22.00),
  -- Película 20: The Matrix
  (20, 1, '2025-07-19', '14:00:00', '15:56:00', 22.00),
  (20, 6, '2025-07-19', '19:00:00', '20:56:00', 22.00),
  (20, 7, '2025-07-20', '11:00:00', '12:56:00', 28.00),
  (20, 2, '2025-07-20', '19:00:00', '20:56:00', 25.00),
  -- Película 21: Interstellar
  (21, 3, '2025-07-20', '16:00:00', '18:49:00', 35.00),
  (21, 4, '2025-07-20', '14:00:00', '16:49:00', 28.00),
  (21, 5, '2025-07-21', '14:00:00', '16:49:00', 22.00),
  (21, 1, '2025-07-21', '16:00:00', '18:49:00', 22.00),
  -- Película 22: Avatar
  (22, 2, '2025-07-20', '16:00:00', '18:42:00', 25.00),
  (22, 6, '2025-07-20', '14:00:00', '16:42:00', 22.00),
  (22, 7, '2025-07-21', '16:00:00', '18:42:00', 28.00),
  (22, 3, '2025-07-21', '19:00:00', '21:42:00', 35.00),
  -- Película 23: Toy Story
  (23, 1, '2025-07-21', '11:00:00', '12:21:00', 22.00),
  (23, 4, '2025-07-21', '11:00:00', '12:21:00', 28.00),
  (23, 5, '2025-07-22', '11:00:00', '12:21:00', 22.00),
  (23, 2, '2025-07-22', '14:00:00', '15:21:00', 25.00),
  -- Película 24: The Lion King
  (24, 6, '2025-07-21', '11:00:00', '12:28:00', 22.00),
  (24, 3, '2025-07-21', '14:00:00', '15:28:00', 35.00),
  (24, 7, '2025-07-22', '11:00:00', '12:28:00', 28.00),
  (24, 1, '2025-07-22', '14:00:00', '15:28:00', 22.00);


-- Reservas
INSERT INTO reserva (id_usuario, total, estado) VALUES
  (3, 44.00, 'pagada'),   -- Gianmarco: 2 entradas
  (4, 22.00, 'pagada');   -- Andrea: 1 entrada

-- Detalle de reserva  ← UNIQUE(id_funcion, id_asiento) en acción
-- Usando funciones e ids de asientos existentes en la BD
INSERT INTO detalle_reserva (id_reserva, id_funcion, id_asiento, precio_unitario) VALUES
  -- Reserva 1: 2 entradas para Dune (función 1, sala 1)
  (1, 1, 1, 22.00),
  (1, 1, 2, 22.00),
  -- Reserva 2: 1 entrada para Fast & Furious (función 5, sala 1)
  (2, 5, 10, 22.00);


  -- Productos
INSERT INTO producto (nombre, descripcion, precio, categoria, imagen_url) VALUES
-- COMBOS
  ('COMBO DUO', '2 Canchitas Grandes Saladas + 2 Gaseosas Grandes', 76.40, 'combo', 'combo_duo.jpg'),
  ('CAMBIA TU CANCHITA A DULCE', 'Convierte tu Canchita Salada en Dulce o Mixta con solo un click', 4.00, 'combo', 'cambia_tu_canchita_a_dulce.jpg'),
  ('COMBO PERSONAL', '1 Canchita Mediana Salada + 1 Gaseosa Mediana', 36.20, 'combo', 'combo_personal_doble_asterisco.jpg'),
  ('COMBO PERSONAL GASEOSA GRANDE', '1 Canchita Mediana Salada + 1 Gaseosa Grande', 26.00, 'combo', 'combo_personal_gaseosa_grande.jpg'),
  ('COMBO GIGANTE', '2 Gaseosas Medianas + 1 Cancha Gigante Salada', 67.90, 'combo', 'combo_gigante.jpg'),
  ('COMBO TRIO', '3 Canchitas Medianas Saladas + 3 Gaseosas Medianas', 97.70, 'combo', 'combo_trio.jpg'),
  ('COMBO PAREJA GASEOSA GRANDE', '2 Canchitas Medianas Saladas + 2 Gaseosas Grandes', 45.80, 'combo', 'combo_pareja_gaseosa_grande.jpg'),
  ('COMBO FRANKFURTER GASEOSA MEDIANA', '1 Frankfurter + 1 Gaseosa Mediana', 32.30, 'combo', 'combo_frankfurter_gaseosa_mediana.jpg'),
  ('COMBO PERSONAL CANCHA GRANDE', '1 Canchita Grande Salada + 1 Gaseosa Mediana', 37.20, 'combo', 'combo_personal_cancha_grande.jpg'),
  ('COMBO PERSONAL GRANDE', '1 Canchita Grande Salada + 1 Gaseosa Grande', 38.20, 'combo', 'combo_personal_grande.jpg'),
  ('COMBO DUO GASEOSA MEDIANA', '2 Canchitas Grandes Saladas + 2 Gaseosas Medianas', 74.40, 'combo', 'combo_duo_gaseosa_mediana.jpg'),
  ('COMBO INFANTIL', '1 Canchita Infantil Salada + 1 Gaseosa Chica + 1 Crismelo Malvavisco', 17.60, 'combo', 'combo_infantil.jpg'),

  -- CANCHITA
  ('CANCHITA MEDIANA SALADA', 'Porción individual de canchita salada', 18.10, 'comida', 'canchita_mediana_salada.jpg'),
  ('CANCHITA MEDIANA DULCE', 'Porción individual de canchita dulce', 22.10, 'comida', 'canchita_mediana_dulce.jpg'),
  ('CANCHITA MEDIANA MIXTA', 'Porción individual de canchita mitad dulce y mitad salada', 22.10, 'comida', 'canchita_mediana_mixta.jpg'),
  ('CANCHITA GRANDE SALADA', 'Porción grande de canchita salada', 19.10, 'comida', 'canchita_grande_salada.jpg'),

  -- BEBIDAS
  ('GASEOSA GRANDE', 'Bebida Coca-Cola Zero Azúcar o similar en tamaño grande', 19.10, 'bebida', 'gaseosa_grande.jpg'),
  ('GASEOSA MEDIANA', 'El descuento aplica al precio regular del teatro', 12.60, 'bebida', 'gaseosa_mediana.jpg'),
  ('AGUA SIN GAS', 'Botella de agua San Luis de 750ml sin gas', 6.00, 'bebida', 'agua_sin_gas.jpg'),
  ('FRUGOS X 300ML', 'Frugos del Valle sabor durazno de 300ml', 7.00, 'bebida', 'frugos_x_300ml.jpg');


-- Promociones  (se eliminó "Combo Personal + Entrada": su tipo 'COMBO+ENTRADA'
-- no existía en el ENUM de la tabla y hubiera hecho fallar el INSERT)
INSERT INTO promocion
  (nombre, descripcion, imagen, tipo, tipo_descuento, valor_descuento, cantidad_minima, aplica_tipo_sala, fecha_inicio, fecha_fin, stock, activo)
  VALUES

  ('2x1 Lunes',
  'Realiza tu compra los lunes y recibe un 2x1 en entradas para funciones elegibles.',
  'promo_2x1.jpg',
  'ENTRADA',
  'PORCENTAJE',
  50.00,
  2,          -- exige comprar 2 entradas para activarse
  NULL,
  '2026-01-01',
  '2026-12-31',
  NULL,
  1),

  ('Lunes 3D',
  'Compra tus entradas para funciones 3D los lunes y obtén 50% de descuento',
  'lunes_3d.jpg',
  'ENTRADA',
  'PORCENTAJE',
  50.00,
  1,
  '3D',       -- solo funciones en sala tipo 3D
  '2026-01-01',
  '2026-12-31',
  NULL,
  1),

  ('Combo Canchita Mediana',
  'Canchita mediana salada y gaseosa grande.',
  'combo_mediano.jpg',
  'PRODUCTO',
  'PRECIO_FIJO',
  23.00,
  1,
  NULL,
  '2026-01-01',
  '2026-12-31',
  200,
  1),

  ('Combo Familiar',
  '2 canchitas grandes, 4 gaseosas grandes.',
  'combo_familiar.jpg',
  'COMBO',
  'PRECIO_FIJO',
  59.90,
  1,
  NULL,
  '2026-01-01',
  '2026-12-31',
  150,
  1),

  ('2x1 Martes 2D',
  'Realiza tu compra los Martes y recibe un 2x1 en entradas para funciones elegibles.',
  'martes_2x1.jpg',
  'ENTRADA',
  'PORCENTAJE',
  50.00,
  2,          -- exige comprar 2 entradas para activarse
  NULL,
  '2026-01-01',
  '2026-12-31',
  NULL,
  1),

  ('Jueves 3D',
  'Compra tus entradas para funciones 3D los jueves y obtén 30% de descuento',
  'jueves_3d.jpg',
  'ENTRADA',
  'PORCENTAJE',
  30.00,
  1,
  '3D',       -- solo funciones en sala tipo 3D
  '2026-01-01',
  '2026-12-31',
  NULL,
  1);



-- IDs resultantes: 1=2x1 Sabados, 2=Sabado 3D, 3=Combo Canchita Mediana, 4=Combo Familiar

INSERT INTO promocion_dia (id_promocion, dia) VALUES
  (1,'DOMINGO'),   -- 2x1 Sabados
  (2,'DOMINGO'),  -- Sabado 3D
  (5,'MARTES'),
  (6,'JUEVES');

-- Promoción ↔ Producto
INSERT INTO promocion_producto (id_promocion, id_producto, cantidad) VALUES
  -- Combo Canchita Mediana -> COMBO PERSONAL GASEOSA GRANDE (Canchita Mediana + Gaseosa Grande)
  (3, 4, 1),
  -- Combo Familiar -> 2 Canchitas Grandes + 4 Gaseosas Grandes 
  (4, 16, 2),
  (4, 17, 4);

-- Producto en reserva
-- Antes esta fila usaba id_promocion = 2 ("Combo Personal + Entrada"), que ya no existe.
-- Se deja sin promoción asociada, vendida a precio normal de combo.
INSERT INTO reserva_producto (id_reserva, id_producto, id_promocion, cantidad, precio_unitario) VALUES
  (1, 1, NULL, 2, 38.20);  -- Reserva 1: 2 x COMBO DUO a precio normal (sin promo)

-- Pagos
INSERT INTO pago (id_reserva, monto, metodo, estado, referencia) VALUES
  (1, 70.00, 'yape',    'aprobado', 'YPE-20250710-001'),
  (2, 22.00, 'tarjeta', 'aprobado', 'VISA-20250710-042');

-- ============================================================
--  VISTA ÚTIL: disponibilidad de asientos por función
-- ============================================================
CREATE OR REPLACE VIEW v_asientos_disponibles AS
SELECT
    f.id_funcion,
    p.titulo        AS pelicula,
    s.nombre        AS sala,
    l.nombre        AS local,
    f.fecha,
    f.hora_inicio,
    a.id_asiento,
    a.fila,
    a.numero,
    a.tipo,
    CASE WHEN dr.id_asiento IS NULL THEN 'disponible' ELSE 'ocupado' END AS estado
FROM funcion f
JOIN pelicula p  ON p.id_pelicula = f.id_pelicula
JOIN sala s      ON s.id_sala     = f.id_sala
JOIN local l     ON l.id_local    = s.id_local
JOIN asiento a   ON a.id_sala     = f.id_sala
LEFT JOIN detalle_reserva dr
       ON dr.id_funcion = f.id_funcion
      AND dr.id_asiento = a.id_asiento;

-- ============================================================
--  VISTA ÚTIL: promociones vigentes hoy según día de la semana
--  (si una promo no tiene filas en promocion_dia, aplica todos los días)
-- ============================================================
CREATE OR REPLACE VIEW v_promociones_vigentes AS
SELECT p.*
FROM promocion p
WHERE p.activo = 1
  AND CURDATE() BETWEEN p.fecha_inicio AND p.fecha_fin
  AND (p.stock IS NULL OR p.stock > 0)
  AND (
        NOT EXISTS (SELECT 1 FROM promocion_dia pd WHERE pd.id_promocion = p.id_promocion)
        OR EXISTS (
             SELECT 1 FROM promocion_dia pd
             WHERE pd.id_promocion = p.id_promocion
               AND pd.dia = ELT(WEEKDAY(CURDATE()) + 1,
                                 'LUNES','MARTES','MIERCOLES','JUEVES','VIERNES','SABADO','DOMINGO')
        )
      );

-- ============================================================
--  TRIGGER: descuenta stock de la promo al usarse en reserva_producto
-- ============================================================
DELIMITER $$

CREATE TRIGGER trg_promocion_consume_stock
AFTER INSERT ON reserva_producto
FOR EACH ROW
BEGIN
    IF NEW.id_promocion IS NOT NULL THEN
        UPDATE promocion
           SET stock = stock - NEW.cantidad
         WHERE id_promocion = NEW.id_promocion
           AND stock IS NOT NULL
           AND stock >= NEW.cantidad;
    END IF;
END$$

DELIMITER ;