-- ============================================================
-- Club Palestino CRM v2.0 - ESPACIOS Y RESERVAS
-- Datos reales del club
-- ============================================================

-- ── ESPACIOS FISICOS ─────────────────────────────────────────
CREATE TABLE IF NOT EXISTS `espacios` (
    `id`          INT UNSIGNED     NOT NULL AUTO_INCREMENT,
    `nombre`      VARCHAR(100)     NOT NULL,
    `tipo`        ENUM('salon','cancha_tenis','cancha_padel','restaurant','pergola','quincho','gimnasio','sala','otro') NOT NULL,
    `capacidad`   INT UNSIGNED     NULL,
    `superficie`  INT UNSIGNED     NULL COMMENT 'm2',
    `descripcion` TEXT             NULL,
    `activo`      TINYINT(1)       NOT NULL DEFAULT 1,
    `solo_eventos` TINYINT(1)      NOT NULL DEFAULT 0 COMMENT 'Solo se puede reservar para eventos',
    `orden`       TINYINT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Datos reales del Club Palestino
INSERT INTO `espacios` (`nombre`,`tipo`,`capacidad`,`superficie`,`descripcion`,`orden`) VALUES
-- Salones
('Gran Comedor','salon',200,450,'Salon principal del club. Superficie 450 m2, altura 4 metros. Ideal para matrimonios, cumpleanos, eventos de empresa.',1),
('Salon Azul','salon',100,200,'Salon elegante. Superficie 200 m2, altura 4 metros. Versatil para todo tipo de eventos.',2),
('Pergola Alta','pergola',40,80,'Espacio semi-exterior. Superficie 80 m2, altura 3 metros. Ambiente unico y acogedor.',3),
('Pergola Baja','pergola',40,NULL,'Espacio semi-exterior con ambiente natural.',4),
('Terraza Gris','salon',60,NULL,'Terraza con capacidad para 50 a 70 personas. Espacio al aire libre con vista al club.',5),
('Prado Bajo Comedor','salon',200,NULL,'Espacio de prados con capacidad para 200 personas. Ideal para eventos al aire libre.',6),
('Quincho','quincho',40,NULL,'Quincho comodo para 40 personas. Perfecto para asados y reuniones familiares.',7),
-- Restaurant
('Restaurant Interior','restaurant',120,NULL,'Restaurant interior del club. Martes a sabado 11:00-23:30 hrs. Domingo y festivos 11:00-20:00 hrs. Lunes cerrado.',8),
-- Canchas Tenis (10)
('Cancha Tenis 1','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',10),
('Cancha Tenis 2','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',11),
('Cancha Tenis 3','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',12),
('Cancha Tenis 4','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',13),
('Cancha Tenis 5','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',14),
('Cancha Tenis 6','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',15),
('Cancha Tenis 7','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',16),
('Cancha Tenis 8','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',17),
('Cancha Tenis 9','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',18),
('Cancha Tenis 10','cancha_tenis',NULL,NULL,'Cancha de tenis profesional',19),
-- Canchas Padel (4)
('Cancha Padel 1','cancha_padel',NULL,NULL,'Cancha de padel profesional',20),
('Cancha Padel 2','cancha_padel',NULL,NULL,'Cancha de padel profesional',21),
('Cancha Padel 3','cancha_padel',NULL,NULL,'Cancha de padel profesional',22),
('Cancha Padel 4','cancha_padel',NULL,NULL,'Cancha de padel profesional',23);

-- ── RESERVAS ─────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS `reservas` (
    `id`          INT UNSIGNED     NOT NULL AUTO_INCREMENT,
    `empresa_id`  TINYINT UNSIGNED NOT NULL DEFAULT 1,
    `espacio_id`  INT UNSIGNED     NOT NULL,
    `socio_id`    INT UNSIGNED     NULL,
    `tipo`        ENUM('cancha','salon','restaurant','evento','clase','otro') NOT NULL,
    `titulo`      VARCHAR(200)     NULL COMMENT 'Para eventos y salones',
    `fecha`       DATE             NOT NULL,
    `hora_inicio` TIME             NOT NULL,
    `hora_fin`    TIME             NOT NULL,
    `personas`    INT UNSIGNED     NOT NULL DEFAULT 1,
    `invitados`   TINYINT UNSIGNED NOT NULL DEFAULT 0,
    `con_luz`     TINYINT(1)       NOT NULL DEFAULT 0,
    `notas`       TEXT             NULL,
    `estado`      ENUM('pendiente','confirmada','cancelada','completada','no_presentado') NOT NULL DEFAULT 'confirmada',
    `origen`      ENUM('admin','portal','whatsapp') NOT NULL DEFAULT 'admin',
    `cancelada_at` TIMESTAMP       NULL,
    `motivo_cancel` VARCHAR(200)   NULL,
    `created_by`  INT UNSIGNED     NULL,
    `created_at`  TIMESTAMP        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_espacio_fecha` (`espacio_id`,`fecha`,`hora_inicio`),
    KEY `idx_socio_reserva` (`socio_id`,`fecha`),
    KEY `idx_fecha_estado`  (`fecha`,`estado`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── EVENTOS DEL CLUB ──────────────────────────────────────────
CREATE TABLE IF NOT EXISTS `club_eventos` (
    `id`          INT UNSIGNED     NOT NULL AUTO_INCREMENT,
    `empresa_id`  TINYINT UNSIGNED NOT NULL DEFAULT 1,
    `espacio_id`  INT UNSIGNED     NOT NULL,
    `reserva_id`  INT UNSIGNED     NULL COMMENT 'Bloquea el espacio automaticamente',
    `titulo`      VARCHAR(200)     NOT NULL,
    `descripcion` TEXT             NULL,
    `tipo`        ENUM('cultural','deportivo','gastronomico','social','taller','otro') NOT NULL DEFAULT 'social',
    `fecha`       DATE             NOT NULL,
    `hora_inicio` TIME             NOT NULL,
    `hora_fin`    TIME             NOT NULL,
    `capacidad`   INT UNSIGNED     NULL,
    `valor_socio` DECIMAL(10,2)   NOT NULL DEFAULT 0 COMMENT '0 = gratis para socios',
    `valor_publico` DECIMAL(10,2) NOT NULL DEFAULT 0,
    `abierto_publico` TINYINT(1)  NOT NULL DEFAULT 0,
    `imagen_url`  VARCHAR(300)     NULL,
    `inscripciones_abiertas` TINYINT(1) NOT NULL DEFAULT 1,
    `activo`      TINYINT(1)       NOT NULL DEFAULT 1,
    `created_at`  TIMESTAMP        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_fecha_evento` (`fecha`,`activo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── INSCRIPCIONES A EVENTOS ───────────────────────────────────
CREATE TABLE IF NOT EXISTS `club_evento_inscripciones` (
    `id`          INT UNSIGNED     NOT NULL AUTO_INCREMENT,
    `evento_id`   INT UNSIGNED     NOT NULL,
    `socio_id`    INT UNSIGNED     NULL,
    `nombre`      VARCHAR(150)     NOT NULL,
    `telefono`    VARCHAR(20)      NULL,
    `email`       VARCHAR(100)     NULL,
    `personas`    TINYINT UNSIGNED NOT NULL DEFAULT 1,
    `estado`      ENUM('inscrito','cancelado','asistio') NOT NULL DEFAULT 'inscrito',
    `created_at`  TIMESTAMP        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_evento_insc` (`evento_id`,`estado`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ── REGLAS DE RESERVA POR ESPACIO ────────────────────────────
CREATE TABLE IF NOT EXISTS `reservas_reglas` (
    `id`          INT UNSIGNED     NOT NULL AUTO_INCREMENT,
    `tipo_espacio` VARCHAR(50)     NOT NULL,
    `tipo_socio`  VARCHAR(50)      NOT NULL DEFAULT 'general' COMMENT 'general, rama_tenis, etc.',
    `anticipacion_dias` TINYINT UNSIGNED NOT NULL DEFAULT 3,
    `max_reservas_dia` TINYINT UNSIGNED NOT NULL DEFAULT 1,
    `duracion_min` SMALLINT UNSIGNED NOT NULL DEFAULT 90,
    `anulacion_horas` TINYINT UNSIGNED NOT NULL DEFAULT 6,
    `invitados_max` TINYINT UNSIGNED NOT NULL DEFAULT 1,
    `hora_inicio` TIME             NOT NULL DEFAULT '07:00:00',
    `hora_fin`    TIME             NOT NULL DEFAULT '22:00:00',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_tipo_socio` (`tipo_espacio`,`tipo_socio`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Reglas canchas tenis
INSERT INTO `reservas_reglas` (`tipo_espacio`,`tipo_socio`,`anticipacion_dias`,`max_reservas_dia`,`duracion_min`,`anulacion_horas`,`invitados_max`,`hora_inicio`,`hora_fin`) VALUES
('cancha_tenis','general',3,1,90,6,1,'07:00:00','22:00:00'),
('cancha_tenis','rama_tenis',7,2,90,4,3,'07:00:00','22:00:00'),
('cancha_padel','general',3,1,90,6,1,'08:00:00','21:00:00'),
('cancha_padel','rama_padel',7,2,90,4,3,'08:00:00','21:00:00');

-- ── ACTUALIZAR PARAMETROS DE INCORPORACION ────────────────────
INSERT INTO `config_sistema` (`clave`,`valor`,`descripcion`) VALUES
('incorporacion_uf_arabe','50','Cuota de incorporacion para socios arabes (por grupo familiar)'),
('incorporacion_uf_no_arabe','150','Cuota de incorporacion para socios no arabes (por grupo familiar)'),
('incorporacion_anos_reingreso','2','Anos sin pagar para volver a cobrar incorporacion'),
('restaurant_email','reservasrestaurante@clubpalestino.cl','Email reservas restaurant'),
('restaurant_whatsapp','+56979695737','WhatsApp reservas restaurant'),
('eventos_email','eventos@clubpalestino.cl','Email reservas eventos y salones'),
('eventos_whatsapp','+56978856355','WhatsApp eventos y salones')
ON DUPLICATE KEY UPDATE valor=VALUES(valor),descripcion=VALUES(descripcion);

