
CREATE DATABASE IF NOT EXISTS `lbodev_bd_2026_crmvenasabiertas` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE `lbodev_bd_2026_crmvenasabiertas`;

CREATE TABLE IF NOT EXISTS `activity_logs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `subject_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `subject_id` bigint unsigned NOT NULL,
  `causer_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `causer_id` bigint unsigned DEFAULT NULL,
  `event` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `properties` json DEFAULT NULL,
  `old_attributes` json DEFAULT NULL,
  `new_attributes` json DEFAULT NULL,
  `created_at` timestamp NOT NULL,
  PRIMARY KEY (`id`),
  KEY `activity_logs_subject_type_subject_id_index` (`subject_type`,`subject_id`),
  KEY `activity_logs_causer_type_causer_id_index` (`causer_type`,`causer_id`),
  KEY `activity_logs_event_index` (`event`),
  KEY `activity_logs_created_at_index` (`created_at`),
  KEY `activity_logs_tenant_id_index` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `activity_logs`;

CREATE TABLE IF NOT EXISTS `contract_data` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL,
  `contract_id` bigint unsigned NOT NULL,
  `payload` json NOT NULL,
  `type` enum('client_update','admin_update') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'client_update',
  `updated_by` bigint unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `contract_data_updated_by_foreign` (`updated_by`),
  KEY `contract_data_contract_id_index` (`contract_id`),
  KEY `contract_data_user_id_index` (`user_id`),
  CONSTRAINT `contract_data_contract_id_foreign` FOREIGN KEY (`contract_id`) REFERENCES `contracts` (`id`) ON DELETE CASCADE,
  CONSTRAINT `contract_data_updated_by_foreign` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `contract_data_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `contract_data`;

CREATE TABLE IF NOT EXISTS `contract_status_logs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL,
  `contract_id` bigint unsigned NOT NULL,
  `old_status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `new_status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `changed_by` bigint unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `contract_status_logs_contract_id_foreign` (`contract_id`),
  KEY `contract_status_logs_changed_by_foreign` (`changed_by`),
  KEY `contract_status_logs_user_id_index` (`user_id`),
  CONSTRAINT `contract_status_logs_changed_by_foreign` FOREIGN KEY (`changed_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `contract_status_logs_contract_id_foreign` FOREIGN KEY (`contract_id`) REFERENCES `contracts` (`id`) ON DELETE CASCADE,
  CONSTRAINT `contract_status_logs_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `contract_status_logs`;

CREATE TABLE IF NOT EXISTS `contract_templates` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `content` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `used_variables` json DEFAULT NULL,
  `description` text COLLATE utf8mb4_unicode_ci,
  `version` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `status` enum('active','inactive') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active',
  `is_public` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `contract_templates_created_by_foreign` (`user_id`),
  KEY `contract_templates_status_index` (`status`),
  KEY `contract_templates_tenant_id_index` (`tenant_id`),
  CONSTRAINT `contract_templates_created_by_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `contract_templates`;

CREATE TABLE IF NOT EXISTS `contratadas` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `client_user_id` bigint unsigned DEFAULT NULL,
  `client_type` enum('pj','pf') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pj',
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `individual_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `cnpj` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `cpf` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `birth_date` date DEFAULT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_secundario` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `phone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `alternative_phone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `complement` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `district` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `city` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `state` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `zip_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `notes` text COLLATE utf8mb4_unicode_ci,
  `status` enum('active','inactive') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `contratadas_status_index` (`status`),
  KEY `contratadas_contratada_type_index` (`client_type`),
  KEY `contratadas_cnpj_index` (`cnpj`),
  KEY `contratadas_cpf_index` (`cpf`),
  KEY `contratadas_contratada_user_id_foreign` (`client_user_id`),
  KEY `contratadas_user_id_foreign` (`user_id`),
  KEY `contratadas_tenant_id_index` (`tenant_id`),
  CONSTRAINT `contratadas_contratada_user_id_foreign` FOREIGN KEY (`client_user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `contratadas_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `contratadas`;

CREATE TABLE IF NOT EXISTS `contratantes` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `client_user_id` bigint unsigned DEFAULT NULL,
  `client_type` enum('pj','pf') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pj',
  `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `individual_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `cnpj` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `cpf` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `birth_date` date DEFAULT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email_secundario` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `phone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `alternative_phone` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `complement` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `district` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `city` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `state` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `zip_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `notes` text COLLATE utf8mb4_unicode_ci,
  `status` enum('active','inactive') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `contratantes_user_id_foreign` (`user_id`),
  KEY `contratantes_client_user_id_foreign` (`client_user_id`),
  KEY `contratantes_client_type_index` (`client_type`),
  KEY `contratantes_status_index` (`status`),
  KEY `contratantes_tenant_id_index` (`tenant_id`),
  CONSTRAINT `contratantes_client_user_id_foreign` FOREIGN KEY (`client_user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `contratantes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `contratantes`;

CREATE TABLE IF NOT EXISTS `failed_jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `uuid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `connection` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `queue` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `failed_jobs`;

CREATE TABLE IF NOT EXISTS `jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `queue` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `attempts` tinyint unsigned NOT NULL,
  `reserved_at` int unsigned DEFAULT NULL,
  `available_at` int unsigned NOT NULL,
  `created_at` int unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `jobs_queue_index` (`queue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `jobs`;

CREATE TABLE IF NOT EXISTS `job_batches` (
  `id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `total_jobs` int NOT NULL,
  `pending_jobs` int NOT NULL,
  `failed_jobs` int NOT NULL,
  `failed_job_ids` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `options` mediumtext COLLATE utf8mb4_unicode_ci,
  `cancelled_at` int DEFAULT NULL,
  `created_at` int NOT NULL,
  `finished_at` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `job_batches`;

CREATE TABLE IF NOT EXISTS `lbodev_marketing_campaigns` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `channel` enum('email','whatsapp','both') COLLATE utf8mb4_unicode_ci NOT NULL,
  `audience_type` enum('contacts','contratadas','contratantes','all') COLLATE utf8mb4_unicode_ci NOT NULL,
  `filters` json DEFAULT NULL,
  `message_subject` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `message_body` text COLLATE utf8mb4_unicode_ci,
  `email_header` text COLLATE utf8mb4_unicode_ci,
  `email_footer` text COLLATE utf8mb4_unicode_ci,
  `status` enum('draft','scheduled','processing','completed','failed') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'draft',
  `scheduled_at` timestamp NULL DEFAULT NULL,
  `total_recipients` int NOT NULL DEFAULT '0',
  `sent_count` int NOT NULL DEFAULT '0',
  `failed_count` int NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `lbodev_marketing_campaigns_tenant_id_index` (`tenant_id`),
  KEY `lbodev_marketing_campaigns_status_index` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `lbodev_marketing_campaigns`;

CREATE TABLE IF NOT EXISTS `lbodev_marketing_opt_outs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `phone` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `channel` enum('email','whatsapp','all') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'all',
  `reason` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `lbodev_marketing_opt_outs_tenant_id_email_channel_unique` (`tenant_id`,`email`,`channel`),
  KEY `lbodev_marketing_opt_outs_tenant_id_index` (`tenant_id`),
  KEY `lbodev_marketing_opt_outs_email_index` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `lbodev_marketing_opt_outs`;

CREATE TABLE IF NOT EXISTS `lbodev_notification_email_layouts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `header` text COLLATE utf8mb4_unicode_ci,
  `footer` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `lbodev_notification_email_layouts_tenant_id_unique` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `lbodev_notification_email_layouts`;

CREATE TABLE IF NOT EXISTS `lbodev_notification_event_settings` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `event_key` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `send_email` tinyint(1) NOT NULL DEFAULT '1',
  `send_whatsapp` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `lbodev_notification_event_settings_tenant_id_event_key_unique` (`tenant_id`,`event_key`),
  KEY `lbodev_notification_event_settings_tenant_id_index` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `lbodev_notification_event_settings`;

CREATE TABLE IF NOT EXISTS `lbodev_notification_templates` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned NOT NULL,
  `channel` enum('email','whatsapp') COLLATE utf8mb4_unicode_ci NOT NULL,
  `event_key` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `subject` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `header` text COLLATE utf8mb4_unicode_ci,
  `body` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `footer` text COLLATE utf8mb4_unicode_ci,
  `is_active` tinyint(1) NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `lbodev_notification_templates_tenant_id_channel_event_key_unique` (`tenant_id`,`channel`,`event_key`),
  KEY `lbodev_notification_templates_tenant_id_index` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `lbodev_notification_templates`;

CREATE TABLE IF NOT EXISTS `migrations` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `batch` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `migrations`;
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES
	(1, '0001_01_01_000000_create_users_table', 1),
	(2, '0001_01_01_000001_create_cache_table', 1),
	(3, '0001_01_01_000002_create_jobs_table', 1),
	(4, '2026_02_04_141250_create_clients_table', 1),
	(5, '2026_02_04_141251_create_contract_templates_table', 1),
	(6, '2026_02_04_141251_create_contracts_table', 1),
	(7, '2026_02_04_141252_create_contract_data_table', 1),
	(8, '2026_02_04_141252_create_contract_status_logs_table', 1),
	(9, '2026_02_04_141253_create_notifications_table', 1),
	(10, '2026_02_04_150000_create_sessions_table', 1),
	(11, '2026_02_04_151000_create_options_table', 2),
	(12, '2026_02_04_183155_create_company_settings_table', 3),
	(13, '2026_02_05_095228_add_client_type_to_clients_table', 4),
	(14, '2026_02_05_112745_update_clients_table_structure', 5),
	(15, '2026_02_05_112808_fix_clients_unique_indexes', 6),
	(16, '2026_02_05_121329_add_username_to_users_table', 7),
	(17, '2026_02_05_121419_add_client_user_id_to_clients_table', 8),
	(18, '2026_02_05_132915_create_password_reset_tokens_table', 9),
	(19, '2026_02_06_113500_add_whatsapp_settings_to_company_settings_table', 10),
	(20, '2026_02_06_000000_create_activity_logs_table', 11),
	(21, '2026_02_06_123956_create_client_update_tokens_table', 12),
	(22, '2026_02_06_120000_create_whatsapp_contacts_table', 13),
	(23, '2026_02_07_120000_add_status_to_whatsapp_contacts_table', 14),
	(24, '2026_02_11_000000_add_origin_to_whatsapp_contacts_table', 15),
	(25, '2026_02_12_000001_rename_created_by_to_user_id_in_contract_templates', 16),
	(27, '2026_02_12_000004_add_user_id_to_company_settings_table', 17),
	(28, '2026_02_12_000000_update_contract_templates_created_by', 18),
	(29, '2026_02_12_000005_remove_unique_constraint_from_company_document', 19),
	(30, '2026_02_12_000010_add_user_id_to_whatsapp_contacts_table', 20),
	(31, '2026_02_12_000011_add_user_id_to_contracts_table', 21),
	(32, '2026_02_12_000013_add_user_id_to_contract_status_logs_table', 22),
	(33, '2026_02_12_000012_add_user_id_to_contract_data_table', 23),
	(34, '2026_02_12_115020_update_contracts_status_enum', 24),
	(35, '2026_02_12_125908_add_contract_id_to_client_update_tokens_table', 25),
	(36, '2026_02_13_070912_update_contracts_status_enum_to_new_flow', 26),
	(37, '2026_02_13_091848_create_contract_approval_tokens_table', 27),
	(38, '2026_02_13_112019_create_system_settings_table', 28),
	(39, '2026_02_16_000000_rename_clients_to_contratadas', 29),
	(40, '2026_02_16_102120_rename_client_id_to_contratada_id_in_contracts_table', 30),
	(41, '2026_02_18_000000_rename_client_tables_to_contratadas', 31),
	(42, '2026_02_18_010000_rename_legacy_indexes_and_constraints', 32),
	(43, '2026_02_18_020000_normalize_client_named_constraints', 33),
	(44, '2026_02_18_030000_remove_leftover_client_constraints', 34),
	(45, '2026_02_21_000000_create_contratantes_table', 35),
	(46, '2026_02_21_010000_add_contratante_id_to_contracts_table', 36),
	(47, '2026_06_02_120000_add_tenant_id_foundation', 37),
	(48, '2026_06_02_121500_add_contract_body_and_template_variable_tracking', 37),
	(49, '2026_06_02_123500_create_notification_and_marketing_tables', 37),
	(50, '2026_06_02_124500_expand_user_roles_for_tenant_rbac', 37),
	(51, '2026_06_02_160000_lbodev_marco5_marketing_compliance', 38),
	(52, '2026_06_05_000001_lbodev_marketing_contacts_opt_out', 39),
	(53, '2026_06_05_120000_lbodev_add_email_template_fields_to_marketing_campaigns', 40),
	(54, '2026_06_06_140000_lbodev_notification_template_redesign', 41),
	(55, '2026_06_06_120000_add_phase_events_to_contracts_table', 42),
	(56, '2026_06_06_140000_create_notas_fiscais_table', 43),
	(57, '2026_06_06_150000_add_amount_to_contracts_table', 44),
	(58, '2026_06_07_180000_backfill_paid_at_on_notas_fiscais', 45),
	(59, '2026_06_07_190000_backfill_signed_at_on_contracts', 46),
	(60, '2026_06_07_000000_simplify_user_roles_single_install', 47);

CREATE TABLE IF NOT EXISTS `notifications` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `channel` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'mail',
  `subject` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `message` text COLLATE utf8mb4_unicode_ci,
  `data` json DEFAULT NULL,
  `status` enum('pending','sent','failed') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'pending',
  `sent_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `notifications_user_id_foreign` (`user_id`),
  CONSTRAINT `notifications_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `notifications`;

CREATE TABLE IF NOT EXISTS `sessions` (
  `id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `ip_address` varchar(45) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `user_agent` text COLLATE utf8mb4_unicode_ci,
  `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `last_activity` int NOT NULL,
  PRIMARY KEY (`id`),
  KEY `sessions_user_id_index` (`user_id`),
  KEY `sessions_last_activity_index` (`last_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `sessions`;

CREATE TABLE IF NOT EXISTS `whatsapp_contacts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `phone` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `city` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `state` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'active',
  `marketing_opt_out_at` timestamp NULL DEFAULT NULL,
  `origin` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT 'manual',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `whatsapp_contacts_status_index` (`status`),
  KEY `whatsapp_contacts_user_id_foreign` (`user_id`),
  KEY `whatsapp_contacts_tenant_id_index` (`tenant_id`),
  CONSTRAINT `whatsapp_contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

DELETE FROM `whatsapp_contacts`;

