-- Auto-generated from legacy schema. Do not edit manually.

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `pagos_suscripcion` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `suscripcion_id` bigint(20) unsigned NOT NULL,
  `pasarela` varchar(80) NOT NULL,
  `referencia` varchar(120) DEFAULT NULL,
  `monto` decimal(12,2) NOT NULL,
  `moneda` varchar(10) NOT NULL DEFAULT 'COP',
  `estado` enum('pendiente','pagado','fallido','reembolsado') NOT NULL DEFAULT 'pagado',
  `fecha_pago` datetime NOT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_pagos_suscripcion_empresa` (`empresa_id`),
  KEY `fk_pagos_suscripcion` (`suscripcion_id`),
  CONSTRAINT `fk_pagos_suscripcion` FOREIGN KEY (`suscripcion_id`) REFERENCES `suscripciones` (`id`),
  CONSTRAINT `fk_pagos_suscripcion_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `avisos_cobro_suscripcion` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `suscripcion_id` bigint(20) unsigned NOT NULL,
  `tipo` varchar(80) NOT NULL,
  `fecha_referencia` date NOT NULL,
  `canal` enum('email','whatsapp','log') NOT NULL DEFAULT 'email',
  `destinatario` varchar(180) DEFAULT NULL,
  `asunto` varchar(180) DEFAULT NULL,
  `mensaje` text DEFAULT NULL,
  `estado` enum('pendiente','enviado','error') NOT NULL DEFAULT 'pendiente',
  `intentos` tinyint(3) unsigned NOT NULL DEFAULT 0,
  `max_intentos` tinyint(3) unsigned NOT NULL DEFAULT 3,
  `ultimo_intento_at` datetime DEFAULT NULL,
  `proximo_intento_at` datetime DEFAULT NULL,
  `enviado_at` datetime DEFAULT NULL,
  `error_detalle` varchar(255) DEFAULT NULL,
  `metadata_json` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_aviso_cobro_dia_canal` (`suscripcion_id`,`tipo`,`fecha_referencia`,`canal`),
  KEY `idx_aviso_cobro_empresa_fecha` (`empresa_id`,`fecha_referencia`),
  KEY `idx_aviso_cobro_estado_retry` (`estado`,`proximo_intento_at`),
  KEY `idx_aviso_cobro_intentos` (`intentos`,`max_intentos`),
  CONSTRAINT `fk_aviso_cobro_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_aviso_cobro_suscripcion` FOREIGN KEY (`suscripcion_id`) REFERENCES `suscripciones` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `saas_checkout_intents` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `reference` varchar(80) NOT NULL,
  `plan_id` bigint(20) unsigned NOT NULL,
  `empresa_nombre` varchar(150) NOT NULL,
  `empresa_slug` varchar(120) NOT NULL,
  `empresa_email` varchar(180) DEFAULT NULL,
  `admin_nombre` varchar(150) NOT NULL,
  `admin_email` varchar(180) NOT NULL,
  `admin_password_hash` varchar(255) NOT NULL,
  `currency` varchar(10) NOT NULL DEFAULT 'COP',
  `amount_in_cents` int(10) unsigned NOT NULL,
  `estado` enum('pending','approved','declined','error','voided','provisioned','failed') NOT NULL DEFAULT 'pending',
  `wompi_transaction_id` varchar(80) DEFAULT NULL,
  `wompi_status` varchar(40) DEFAULT NULL,
  `last_event_at` datetime DEFAULT NULL,
  `paid_at` datetime DEFAULT NULL,
  `empresa_id` bigint(20) unsigned DEFAULT NULL,
  `suscripcion_id` bigint(20) unsigned DEFAULT NULL,
  `provisioned_at` datetime DEFAULT NULL,
  `failure_reason` varchar(255) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_saas_checkout_reference` (`reference`),
  KEY `idx_saas_checkout_plan` (`plan_id`),
  KEY `idx_saas_checkout_admin_email` (`admin_email`),
  KEY `idx_saas_checkout_status` (`estado`),
  KEY `fk_saas_checkout_empresa` (`empresa_id`),
  KEY `fk_saas_checkout_suscripcion` (`suscripcion_id`),
  CONSTRAINT `fk_saas_checkout_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_saas_checkout_plan` FOREIGN KEY (`plan_id`) REFERENCES `planes` (`id`),
  CONSTRAINT `fk_saas_checkout_suscripcion` FOREIGN KEY (`suscripcion_id`) REFERENCES `suscripciones` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `whatsapp_meta_templates` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `nombre` varchar(120) NOT NULL,
  `idioma` varchar(10) NOT NULL DEFAULT 'es_CO',
  `categoria` enum('UTILITY','MARKETING','AUTHENTICATION') NOT NULL DEFAULT 'UTILITY',
  `uso` varchar(80) NOT NULL DEFAULT 'libre',
  `encabezado_texto` varchar(120) DEFAULT NULL,
  `cuerpo_texto` text NOT NULL,
  `pie_texto` varchar(120) DEFAULT NULL,
  `boton_texto` varchar(30) DEFAULT NULL,
  `boton_url` varchar(255) DEFAULT NULL,
  `ejemplos_variables` text DEFAULT NULL,
  `meta_template_id` varchar(120) DEFAULT NULL,
  `meta_estado` enum('pendiente','aprobada','rechazada','error') NOT NULL DEFAULT 'pendiente',
  `meta_estado_detalle` varchar(255) DEFAULT NULL,
  `meta_last_sync_at` datetime DEFAULT NULL,
  `meta_response_json` longtext DEFAULT NULL,
  `creado_por_usuario_id` bigint(20) unsigned DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_whatsapp_meta_templates_nombre` (`nombre`),
  KEY `idx_whatsapp_meta_templates_estado` (`meta_estado`,`meta_last_sync_at`),
  KEY `idx_whatsapp_meta_templates_uso` (`uso`),
  KEY `fk_whatsapp_meta_templates_usuario` (`creado_por_usuario_id`),
  CONSTRAINT `fk_whatsapp_meta_templates_usuario` FOREIGN KEY (`creado_por_usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `ai_help_sessions` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `titulo` varchar(160) NOT NULL,
  `estado` enum('activa','cerrada') NOT NULL DEFAULT 'activa',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `last_activity_at` datetime DEFAULT NULL,
  `closed_at` datetime DEFAULT NULL,
  `reopened_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_ai_help_sessions_empresa_usuario_estado` (`empresa_id`,`usuario_id`,`estado`,`id`),
  KEY `idx_ai_help_sessions_closed_at` (`closed_at`),
  KEY `fk_ai_help_sessions_usuario` (`usuario_id`),
  CONSTRAINT `fk_ai_help_sessions_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_ai_help_sessions_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `ai_help_messages` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `session_id` bigint(20) unsigned DEFAULT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `rol` enum('user','assistant','system') NOT NULL DEFAULT 'assistant',
  `contenido` text NOT NULL,
  `modelo` varchar(120) DEFAULT NULL,
  `metadata_json` text DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_ai_help_messages_empresa_usuario` (`empresa_id`,`usuario_id`,`id`),
  KEY `idx_ai_help_messages_created_at` (`created_at`),
  KEY `fk_ai_help_messages_usuario` (`usuario_id`),
  KEY `idx_ai_help_messages_session` (`session_id`,`id`),
  CONSTRAINT `fk_ai_help_messages_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_ai_help_messages_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `soporte_tickets` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned NOT NULL,
  `creado_por_usuario_id` bigint(20) unsigned DEFAULT NULL,
  `asignado_a_usuario_id` bigint(20) unsigned DEFAULT NULL,
  `asunto` varchar(180) NOT NULL,
  `descripcion` text NOT NULL,
  `prioridad` enum('baja','media','alta','critica') NOT NULL DEFAULT 'media',
  `estado` enum('abierto','en_proceso','pendiente_cliente','resuelto','cerrado') NOT NULL DEFAULT 'abierto',
  `canal` varchar(40) NOT NULL DEFAULT 'panel',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `primera_respuesta_at` datetime DEFAULT NULL,
  `primera_respuesta_limite_at` datetime DEFAULT NULL,
  `resolucion_limite_at` datetime DEFAULT NULL,
  `objetivo_primera_respuesta_min` smallint(5) unsigned NOT NULL DEFAULT 120,
  `objetivo_resolucion_horas` smallint(5) unsigned NOT NULL DEFAULT 24,
  `sla_primera_respuesta_estado` enum('pendiente','cumplido','incumplido') NOT NULL DEFAULT 'pendiente',
  `sla_resolucion_estado` enum('pendiente','cumplido','incumplido') NOT NULL DEFAULT 'pendiente',
  `cerrado_at` datetime DEFAULT NULL,
  `resuelto_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_soporte_tickets_empresa_estado` (`empresa_id`,`estado`),
  KEY `idx_soporte_tickets_asignado` (`asignado_a_usuario_id`),
  KEY `fk_soporte_tickets_creado_por` (`creado_por_usuario_id`),
  KEY `idx_soporte_sla_primer` (`sla_primera_respuesta_estado`,`primera_respuesta_limite_at`),
  KEY `idx_soporte_sla_resolucion` (`sla_resolucion_estado`,`resolucion_limite_at`),
  KEY `idx_soporte_created_at` (`created_at`),
  CONSTRAINT `fk_soporte_tickets_asignado` FOREIGN KEY (`asignado_a_usuario_id`) REFERENCES `usuarios` (`id`),
  CONSTRAINT `fk_soporte_tickets_creado_por` FOREIGN KEY (`creado_por_usuario_id`) REFERENCES `usuarios` (`id`),
  CONSTRAINT `fk_soporte_tickets_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `soporte_ticket_eventos` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `ticket_id` bigint(20) unsigned NOT NULL,
  `usuario_id` bigint(20) unsigned DEFAULT NULL,
  `tipo` enum('creacion','comentario','estado','asignacion','interno') NOT NULL DEFAULT 'comentario',
  `mensaje` text NOT NULL,
  `estado_anterior` varchar(40) DEFAULT NULL,
  `estado_nuevo` varchar(40) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_soporte_eventos_ticket` (`ticket_id`,`created_at`),
  KEY `fk_soporte_eventos_usuario` (`usuario_id`),
  CONSTRAINT `fk_soporte_eventos_ticket` FOREIGN KEY (`ticket_id`) REFERENCES `soporte_tickets` (`id`),
  CONSTRAINT `fk_soporte_eventos_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `notificacion_vistas` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `usuario_id` bigint(20) unsigned NOT NULL,
  `empresa_id` bigint(20) unsigned NOT NULL DEFAULT 0,
  `notification_key` varchar(255) NOT NULL,
  `leido_at` datetime NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_notificacion_vistas_usuario_empresa_key` (`usuario_id`,`empresa_id`,`notification_key`),
  KEY `idx_notificacion_vistas_empresa_usuario` (`empresa_id`,`usuario_id`),
  KEY `idx_notificacion_vistas_leido_at` (`leido_at`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `logs_actividad` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` bigint(20) unsigned DEFAULT NULL,
  `usuario_id` bigint(20) unsigned DEFAULT NULL,
  `accion` varchar(120) NOT NULL,
  `descripcion` text DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_logs_empresa_fecha` (`empresa_id`,`created_at`),
  KEY `fk_logs_usuario` (`usuario_id`),
  CONSTRAINT `fk_logs_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`),
  CONSTRAINT `fk_logs_usuario` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=308 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
