/*M!999999\- enable the sandbox mode */ 

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `audit_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `usuario_id` int(11) DEFAULT NULL,
  `empresa_id` int(11) DEFAULT NULL,
  `accion` varchar(100) NOT NULL,
  `detalles` text DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_usuario` (`usuario_id`),
  KEY `idx_empresa` (`empresa_id`),
  KEY `idx_fecha` (`created_at`)
) ENGINE=InnoDB AUTO_INCREMENT=1578 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `backup_log` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `empresa_id` int(10) unsigned DEFAULT NULL,
  `tipo` enum('auto','manual') NOT NULL DEFAULT 'auto',
  `archivo` varchar(255) NOT NULL,
  `tamano_bytes` bigint(20) unsigned DEFAULT NULL,
  `estado` enum('success','failed') NOT NULL DEFAULT 'success',
  `creado_en` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_empresa` (`empresa_id`),
  KEY `idx_creado` (`creado_en`)
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `company_info` (
  `id` int(11) NOT NULL DEFAULT 1,
  `company_name` varchar(100) NOT NULL DEFAULT 'CSN Apps',
  `company_address` varchar(100) NOT NULL DEFAULT '',
  `company_city` varchar(50) NOT NULL DEFAULT '',
  `company_state` varchar(10) NOT NULL DEFAULT '',
  `company_zip` varchar(10) NOT NULL DEFAULT '',
  `company_phone` varchar(20) NOT NULL DEFAULT '',
  `company_email` varchar(100) NOT NULL DEFAULT '',
  `company_website` varchar(100) NOT NULL DEFAULT 'csnapps.com',
  `company_tax_id` varchar(30) NOT NULL DEFAULT '',
  `company_logo` varchar(255) DEFAULT NULL,
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_by` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `cron_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `cron_installed` tinyint(1) NOT NULL DEFAULT 0,
  `cron_schedule` varchar(50) DEFAULT NULL COMMENT 'Ej: 0 9 * * *',
  `cron_schedule_label` varchar(100) DEFAULT NULL COMMENT 'Ej: Daily at 9 AM EDT',
  `cron_script_path` varchar(500) DEFAULT NULL,
  `installed_at` datetime DEFAULT NULL,
  `installed_by_user_id` int(11) DEFAULT NULL,
  `last_check_at` datetime DEFAULT NULL,
  `send_admin_summary` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'Enviar resumen al admin tras cada ejecución',
  `admin_summary_email` varchar(150) DEFAULT NULL,
  `notes` text DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `cron_executions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `started_at` datetime NOT NULL,
  `finished_at` datetime DEFAULT NULL,
  `execution_mode` enum('auto','manual','dry_run') NOT NULL DEFAULT 'auto',
  `triggered_by` varchar(100) DEFAULT NULL COMMENT 'system | super_admin email',
  `subscriptions_checked` int(11) NOT NULL DEFAULT 0,
  `reminders_7days` int(11) NOT NULL DEFAULT 0,
  `reminders_due` int(11) NOT NULL DEFAULT 0,
  `reminders_final` int(11) NOT NULL DEFAULT 0,
  `suspensions` int(11) NOT NULL DEFAULT 0,
  `reactivations` int(11) NOT NULL DEFAULT 0,
  `emails_sent` int(11) NOT NULL DEFAULT 0,
  `errors` int(11) NOT NULL DEFAULT 0,
  `duration_seconds` decimal(8,3) DEFAULT NULL,
  `status` enum('running','success','failed','partial') NOT NULL DEFAULT 'running',
  `notes` text DEFAULT NULL COMMENT 'JSON con detalles por subscription',
  PRIMARY KEY (`id`),
  KEY `idx_started_at` (`started_at`),
  KEY `idx_mode` (`execution_mode`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `email_config` (
  `id` int(11) NOT NULL DEFAULT 1,
  `smtp_host` varchar(100) NOT NULL DEFAULT 'smtp.gmail.com',
  `smtp_port` int(5) NOT NULL DEFAULT 587,
  `smtp_username` varchar(150) NOT NULL DEFAULT '',
  `smtp_password` varchar(255) NOT NULL DEFAULT '',
  `smtp_from_email` varchar(150) NOT NULL DEFAULT '',
  `smtp_from_name` varchar(150) NOT NULL DEFAULT 'CSN Apps',
  `smtp_encryption` varchar(10) NOT NULL DEFAULT 'tls',
  `send_bcc_admin` tinyint(1) NOT NULL DEFAULT 1,
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_by` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `empresas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `codigo` varchar(60) NOT NULL,
  `nombre` varchar(150) NOT NULL,
  `rfc` varchar(20) DEFAULT NULL,
  `direccion` text DEFAULT NULL,
  `telefono` varchar(20) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `db_name` varchar(100) NOT NULL,
  `db_host` varchar(100) DEFAULT 'localhost',
  `storage_path` varchar(255) NOT NULL,
  `logo` varchar(255) DEFAULT NULL,
  `moneda` varchar(10) DEFAULT 'MXN',
  `zona_horaria` varchar(50) DEFAULT 'America/Mexico_City',
  `activa` tinyint(1) DEFAULT 1,
  `exit_count` int(10) unsigned NOT NULL DEFAULT 0,
  `creada_por` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `codigo` (`codigo`),
  UNIQUE KEY `db_name` (`db_name`),
  UNIQUE KEY `codigo_2` (`codigo`),
  KEY `creada_por` (`creada_por`),
  CONSTRAINT `empresas_ibfk_1` FOREIGN KEY (`creada_por`) REFERENCES `usuarios` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `empresas_ia_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `empresa_id` int(11) NOT NULL,
  `proveedor` varchar(20) NOT NULL DEFAULT 'anthropic',
  `api_key_encrypted` text NOT NULL COMMENT 'Cifrado AES-256 con MASTER_ENCRYPTION_KEY',
  `key_prefix` varchar(30) DEFAULT NULL COMMENT 'Primeros chars para mostrar en UI (sin descifrar)',
  `key_suffix` varchar(10) DEFAULT NULL COMMENT 'Ultimos chars para mostrar en UI',
  `modelo` varchar(50) NOT NULL DEFAULT 'claude-haiku-4-5-20251001',
  `validada` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 si el ping a Anthropic funciono',
  `validated_at` timestamp NULL DEFAULT NULL COMMENT 'Fecha del ultimo ping exitoso',
  `last_used_at` timestamp NULL DEFAULT NULL COMMENT 'Ultima vez que se uso la key',
  `total_requests` int(11) NOT NULL DEFAULT 0 COMMENT 'Contador de llamadas totales',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `empresa_id` (`empresa_id`),
  KEY `idx_empresa` (`empresa_id`),
  KEY `idx_validada` (`validada`),
  KEY `idx_last_used` (`last_used_at`),
  CONSTRAINT `fk_empresas_ia_config_empresa` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `payments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `subscription_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL COMMENT 'Cliente (owner) que realiza el pago',
  `payment_for` enum('software','hosting') NOT NULL COMMENT 'Software o hosting (recibos separados)',
  `amount` decimal(10,2) NOT NULL,
  `payment_date` date NOT NULL,
  `payment_method` enum('cash','zelle','transfer','check','card') NOT NULL,
  `reference` varchar(100) DEFAULT NULL,
  `covers_from` date DEFAULT NULL,
  `covers_to` date DEFAULT NULL,
  `receipt_number` varchar(25) NOT NULL COMMENT 'REC-SW-YYYY-NNNNN o REC-HOST-YYYY-NNNNN',
  `receipt_pdf_path` varchar(255) DEFAULT NULL,
  `email_sent` tinyint(1) NOT NULL DEFAULT 0,
  `email_sent_at` timestamp NULL DEFAULT NULL,
  `email_sent_to` varchar(255) DEFAULT NULL,
  `notes` text DEFAULT NULL,
  `created_by` int(11) DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `receipt_number` (`receipt_number`),
  KEY `fk_payments_subscription` (`subscription_id`),
  KEY `idx_user_date` (`user_id`,`payment_date`),
  KEY `idx_payment_date` (`payment_date`),
  KEY `idx_payment_for` (`payment_for`),
  KEY `idx_method` (`payment_method`),
  CONSTRAINT `fk_payments_subscription` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_payments_user` FOREIGN KEY (`user_id`) REFERENCES `usuarios` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Historial de pagos POR CLIENTE (software y hosting, recibos separados)';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `sesiones` (
  `id` varchar(128) NOT NULL,
  `usuario_id` int(11) NOT NULL,
  `empresa_id` int(11) DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `expira` datetime NOT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `usuario_id` (`usuario_id`),
  CONSTRAINT `sesiones_ibfk_1` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `subscription_events` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `subscription_id` int(11) NOT NULL,
  `category` enum('software','hosting','general') NOT NULL DEFAULT 'general' COMMENT 'Que aspecto involucra el evento',
  `event_type` varchar(50) NOT NULL COMMENT 'created, payment_registered, reminder_sent, suspended, reactivated, etc.',
  `details` text DEFAULT NULL,
  `email_sent_to` varchar(255) DEFAULT NULL,
  `performed_by` varchar(50) NOT NULL DEFAULT 'system',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_subscription` (`subscription_id`),
  KEY `idx_category` (`category`),
  KEY `idx_event_type` (`event_type`),
  KEY `idx_created` (`created_at`),
  CONSTRAINT `fk_events_subscription` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Audit trail de cambios (software + hosting)';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `subscriptions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL COMMENT 'Cliente (owner) que contrata el servicio',
  `plan_type` enum('one_time','lease','monthly_indefinite') NOT NULL COMMENT 'Tipo de plan del software',
  `amount` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT 'Monto mensual (o total si one_time)',
  `currency` varchar(3) NOT NULL DEFAULT 'USD',
  `billing_day` tinyint(3) unsigned DEFAULT NULL COMMENT 'Dia del mes 1-28 (NULL para one_time)',
  `start_date` date NOT NULL,
  `end_date` date DEFAULT NULL COMMENT 'Solo para lease',
  `next_due_date` date DEFAULT NULL COMMENT 'Proximo vencimiento de software',
  `last_payment_date` date DEFAULT NULL,
  `total_months_paid` int(10) unsigned NOT NULL DEFAULT 0,
  `total_amount_paid` decimal(12,2) NOT NULL DEFAULT 0.00,
  `status` enum('active','due','grace_period','suspended','cancelled','paid_full') NOT NULL DEFAULT 'active',
  `suspended_at` timestamp NULL DEFAULT NULL,
  `suspended_reason` varchar(255) DEFAULT NULL,
  `reminder_7days_sent` tinyint(1) NOT NULL DEFAULT 0,
  `reminder_due_sent` tinyint(1) NOT NULL DEFAULT 0,
  `reminder_final_sent` tinyint(1) NOT NULL DEFAULT 0,
  `hosting_amount` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT 'Monto anual de hosting',
  `hosting_start_date` date DEFAULT NULL COMMENT 'Fecha del primer pago de hosting (empieza cuando paga)',
  `hosting_next_due_date` date DEFAULT NULL COMMENT 'Proximo vencimiento (anual desde primer pago)',
  `hosting_last_payment` date DEFAULT NULL,
  `hosting_total_paid` decimal(12,2) NOT NULL DEFAULT 0.00,
  `hosting_years_paid` int(10) unsigned NOT NULL DEFAULT 0,
  `hosting_status` enum('pending_first_payment','active','due','grace_period','suspended','cancelled') NOT NULL DEFAULT 'pending_first_payment' COMMENT 'pending_first_payment = no ha pagado el primero nunca',
  `hosting_suspended_at` timestamp NULL DEFAULT NULL,
  `hosting_suspended_reason` varchar(255) DEFAULT NULL,
  `hosting_reminder_30d_sent` tinyint(1) NOT NULL DEFAULT 0,
  `hosting_reminder_7d_sent` tinyint(1) NOT NULL DEFAULT 0,
  `hosting_reminder_due_sent` tinyint(1) NOT NULL DEFAULT 0,
  `hosting_reminder_final_sent` tinyint(1) NOT NULL DEFAULT 0,
  `notes` text DEFAULT NULL COMMENT 'Notas privadas del admin',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `auto_suspended_at` datetime DEFAULT NULL COMMENT 'Cuándo fue suspendida automáticamente por el cron',
  `suspension_notice_sent` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 si ya se envió email de suspensión',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_id` (`user_id`),
  KEY `idx_status` (`status`),
  KEY `idx_hosting_status` (`hosting_status`),
  KEY `idx_next_due` (`next_due_date`),
  KEY `idx_hosting_next_due` (`hosting_next_due_date`),
  KEY `idx_plan_type` (`plan_type`),
  CONSTRAINT `fk_subscriptions_user` FOREIGN KEY (`user_id`) REFERENCES `usuarios` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Suscripciones: software + hosting POR CLIENTE (no por empresa)';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `usuarios` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(150) NOT NULL,
  `tipo` enum('super_admin','owner') NOT NULL DEFAULT 'owner',
  `plan_tier` int(11) NOT NULL DEFAULT 10,
  `upgrade_count` int(11) NOT NULL DEFAULT 0 COMMENT 'Numero total de upgrades realizados para este cliente',
  `last_upgrade_at` datetime DEFAULT NULL COMMENT 'Fecha/hora del ultimo upgrade',
  `last_upgrade_from` int(11) DEFAULT NULL COMMENT 'Tier anterior en el ultimo upgrade (ej: 10)',
  `last_upgrade_to` int(11) DEFAULT NULL COMMENT 'Tier nuevo en el ultimo upgrade (ej: 20)',
  `upgrade_notice_pending` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 = mostrar banner en el proximo login del cliente',
  `upgrade_history` longtext DEFAULT NULL COMMENT 'Historial completo de upgrades en formato JSON',
  `license_type` enum('buy','lease','trial') NOT NULL DEFAULT 'trial',
  `estado` enum('pending_email','active','trial_expired','suspended') NOT NULL DEFAULT 'pending_email',
  `email_verified_at` datetime DEFAULT NULL,
  `email_verification_token` varchar(64) DEFAULT NULL,
  `email_token_expires_at` datetime DEFAULT NULL,
  `trial_ends_at` datetime DEFAULT NULL,
  `expiry_notice_27_sent` tinyint(1) NOT NULL DEFAULT 0,
  `expiry_notice_28_sent` tinyint(1) NOT NULL DEFAULT 0,
  `expiry_notice_29_sent` tinyint(1) NOT NULL DEFAULT 0,
  `registered_at` datetime NOT NULL DEFAULT current_timestamp(),
  `password_hash` varchar(255) NOT NULL,
  `nombre` varchar(100) NOT NULL,
  `telefono` varchar(20) DEFAULT NULL,
  `activo` tinyint(1) DEFAULT 1,
  `ultimo_login` datetime DEFAULT NULL,
  `intentos_fallidos` int(11) DEFAULT 0,
  `bloqueado_hasta` datetime DEFAULT NULL,
  `password_reset_token` varchar(64) DEFAULT NULL,
  `password_reset_expires_at` datetime DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  KEY `idx_email_token` (`email_verification_token`),
  KEY `idx_estado` (`estado`),
  KEY `idx_tipo` (`tipo`),
  KEY `idx_password_reset_token` (`password_reset_token`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `usuarios_empresas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `usuario_id` int(11) NOT NULL,
  `empresa_id` int(11) NOT NULL,
  `rol` varchar(50) DEFAULT 'admin',
  `activo` tinyint(1) DEFAULT 1,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_empresa` (`usuario_id`,`empresa_id`),
  KEY `empresa_id` (`empresa_id`),
  CONSTRAINT `usuarios_empresas_ibfk_1` FOREIGN KEY (`usuario_id`) REFERENCES `usuarios` (`id`) ON DELETE CASCADE,
  CONSTRAINT `usuarios_empresas_ibfk_2` FOREIGN KEY (`empresa_id`) REFERENCES `empresas` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

