-- ============================================================================
-- Glastree — Database schema + seed data per fresh install (MySQL)
-- Generato da: 58 migration file consolidati
-- Attenzione: NON contiene l'utente admin (lo crea install.php via PDO)
-- ============================================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ============================================================================
-- TABELLE
-- ============================================================================

DROP TABLE IF EXISTS `calendario_connessioni`;
CREATE TABLE `calendario_connessioni` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `tipo` varchar(255) NOT NULL,
  `config` text NOT NULL,
  `stato` varchar(255) DEFAULT 'disconnesso',
  `sync_direction` varchar(255) DEFAULT 'bidirectional',
  `sync_interval_minutes` int DEFAULT '60',
  `last_sync_at` timestamp NULL DEFAULT NULL,
  `last_error_at` timestamp NULL DEFAULT NULL,
  `last_error_message` text,
  `is_active` tinyint(1) DEFAULT '1',
  `ordine` int DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `storage_repositories`;
CREATE TABLE `storage_repositories` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `tipo` varchar(50) NOT NULL,
  `config` text NOT NULL,
  `is_active` tinyint(1) DEFAULT '1',
  `ordine` int DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `report_custom`;
CREATE TABLE `report_custom` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL,
  `nome` varchar(255) NOT NULL,
  `descrizione` text,
  `tipo_report` varchar(255) NOT NULL,
  `config` json DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `report_custom_user_id_foreign` (`user_id`),
  CONSTRAINT `report_custom_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `sender_accounts`;
CREATE TABLE `sender_accounts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `email_address` varchar(255) NOT NULL,
  `email_name` varchar(255) DEFAULT NULL,
  `smtp_host` varchar(255) DEFAULT NULL,
  `smtp_port` int DEFAULT '587',
  `smtp_encryption` varchar(255) DEFAULT 'tls',
  `smtp_username` varchar(255) DEFAULT NULL,
  `smtp_password` text,
  `reply_to` varchar(255) DEFAULT NULL,
  `verify_email` varchar(255) DEFAULT NULL,
  `note` text,
  `is_active` tinyint(1) DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `sender_accounts_email_address_unique` (`email_address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `tipologie_eventi`;
CREATE TABLE `tipologie_eventi` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(50) NOT NULL,
  `descrizione` varchar(255) DEFAULT NULL,
  `ordine` int DEFAULT '0',
  `attiva` tinyint(1) DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tipologie_eventi_nome_unique` (`nome`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `ruoli`;
CREATE TABLE `ruoli` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(50) NOT NULL,
  `descrizione` varchar(255) DEFAULT NULL,
  `ordine` int DEFAULT '0',
  `attiva` tinyint(1) DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ruoli_nome_unique` (`nome`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `tipologie_documenti`;
CREATE TABLE `tipologie_documenti` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(50) NOT NULL,
  `descrizione` varchar(255) DEFAULT NULL,
  `ordine` int DEFAULT '0',
  `attiva` tinyint(1) DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tipologie_documenti_nome_unique` (`nome`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `documenti_cartelle`;
CREATE TABLE `documenti_cartelle` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` bigint unsigned DEFAULT NULL,
  `nome` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `documenti_cartelle_parent_id_foreign` (`parent_id`),
  CONSTRAINT `documenti_cartelle_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `documenti_cartelle` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `viste_report`;
CREATE TABLE `viste_report` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL,
  `nome` varchar(255) NOT NULL,
  `is_default` tinyint(1) DEFAULT '0',
  `tipo` varchar(255) NOT NULL,
  `colonne_visibili` json DEFAULT NULL,
  `colonne_ordinamento` json DEFAULT NULL,
  `filtri` json DEFAULT NULL,
  `ricerca` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `viste_report_tipo_index` (`tipo`),
  KEY `viste_report_user_id_index` (`user_id`),
  KEY `viste_report_is_default_index` (`is_default`),
  CONSTRAINT `viste_report_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `eventi_documenti`;
CREATE TABLE `eventi_documenti` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `evento_id` bigint unsigned NOT NULL,
  `documento_id` bigint unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `eventi_documenti_evento_id_foreign` (`evento_id`),
  KEY `eventi_documenti_documento_id_foreign` (`documento_id`),
  CONSTRAINT `eventi_documenti_documento_id_foreign` FOREIGN KEY (`documento_id`) REFERENCES `documenti` (`id`) ON DELETE CASCADE,
  CONSTRAINT `eventi_documenti_evento_id_foreign` FOREIGN KEY (`evento_id`) REFERENCES `eventi` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `eventi_responsabili`;
CREATE TABLE `eventi_responsabili` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `evento_id` bigint unsigned NOT NULL,
  `individuo_id` bigint unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `eventi_responsabili_evento_id_foreign` (`evento_id`),
  KEY `eventi_responsabili_individuo_id_foreign` (`individuo_id`),
  CONSTRAINT `eventi_responsabili_evento_id_foreign` FOREIGN KEY (`evento_id`) REFERENCES `eventi` (`id`) ON DELETE CASCADE,
  CONSTRAINT `eventi_responsabili_individuo_id_foreign` FOREIGN KEY (`individuo_id`) REFERENCES `individui` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `eventi_gruppi`;
CREATE TABLE `eventi_gruppi` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `evento_id` bigint unsigned NOT NULL,
  `gruppo_id` bigint unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `eventi_gruppi_evento_id_foreign` (`evento_id`),
  KEY `eventi_gruppi_gruppo_id_foreign` (`gruppo_id`),
  CONSTRAINT `eventi_gruppi_evento_id_foreign` FOREIGN KEY (`evento_id`) REFERENCES `eventi` (`id`) ON DELETE CASCADE,
  CONSTRAINT `eventi_gruppi_gruppo_id_foreign` FOREIGN KEY (`gruppo_id`) REFERENCES `gruppi` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `gruppo_individuo`;
CREATE TABLE `gruppo_individuo` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `gruppo_id` bigint unsigned NOT NULL,
  `individuo_id` bigint unsigned NOT NULL,
  `ruolo_ids` json DEFAULT NULL,
  `ruolo_nel_gruppo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `data_adesione` date DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `gruppo_individuo_gruppo_id_individuo_id_unique` (`gruppo_id`,`individuo_id`),
  KEY `gruppo_individuo_gruppo_id_foreign` (`gruppo_id`),
  KEY `gruppo_individuo_individuo_id_foreign` (`individuo_id`),
  CONSTRAINT `gruppo_individuo_gruppo_id_foreign` FOREIGN KEY (`gruppo_id`) REFERENCES `gruppi` (`id`) ON DELETE CASCADE,
  CONSTRAINT `gruppo_individuo_individuo_id_foreign` FOREIGN KEY (`individuo_id`) REFERENCES `individui` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `contatti`;
CREATE TABLE `contatti` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `individuo_id` bigint unsigned NOT NULL,
  `tipo` enum('telefono','cellulare','email','fax','web','telegram','whatsapp','altro') NOT NULL,
  `valore` varchar(255) NOT NULL,
  `etichetta` varchar(255) DEFAULT NULL,
  `is_primary` tinyint(1) DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `contatti_individuo_id_index` (`individuo_id`),
  CONSTRAINT `contatti_individuo_id_foreign` FOREIGN KEY (`individuo_id`) REFERENCES `individui` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `email_attachments`;
CREATE TABLE `email_attachments` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `email_message_id` bigint unsigned NOT NULL,
  `filename` varchar(255) NOT NULL,
  `mime_type` varchar(255) DEFAULT NULL,
  `size` bigint DEFAULT '0',
  `file_path` varchar(255) NOT NULL,
  `is_saved_to_documenti` tinyint(1) DEFAULT '0',
  `documenti_id` bigint unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `email_attachments_email_message_id_foreign` (`email_message_id`),
  KEY `email_attachments_documenti_id_foreign` (`documenti_id`),
  CONSTRAINT `email_attachments_documenti_id_foreign` FOREIGN KEY (`documenti_id`) REFERENCES `documenti` (`id`) ON DELETE SET NULL,
  CONSTRAINT `email_attachments_email_message_id_foreign` FOREIGN KEY (`email_message_id`) REFERENCES `email_messages` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `email_messages`;
CREATE TABLE `email_messages` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `email_folder_id` bigint unsigned NOT NULL,
  `message_id` varchar(255) DEFAULT NULL,
  `subject` varchar(255) DEFAULT NULL,
  `from_name` varchar(255) DEFAULT NULL,
  `from_email` varchar(255) DEFAULT NULL,
  `to_name` varchar(255) DEFAULT NULL,
  `to_email` varchar(255) DEFAULT NULL,
  `cc` text,
  `bcc` text,
  `body_text` longtext,
  `body_html` longtext,
  `is_read` tinyint(1) DEFAULT '0',
  `is_starred` tinyint(1) DEFAULT '0',
  `is_important` tinyint(1) DEFAULT '0',
  `is_sent` tinyint(1) DEFAULT '0',
  `is_draft` tinyint(1) DEFAULT '0',
  `is_trash` tinyint(1) DEFAULT '0',
  `received_at` timestamp NULL DEFAULT NULL,
  `sent_at` timestamp NULL DEFAULT NULL,
  `imap_uid` bigint unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `email_messages_message_id_index` (`message_id`),
  KEY `email_messages_is_read_index` (`is_read`),
  KEY `email_messages_is_starred_index` (`is_starred`),
  KEY `email_messages_is_sent_index` (`is_sent`),
  KEY `email_messages_received_at_index` (`received_at`),
  KEY `email_messages_email_folder_id_foreign` (`email_folder_id`),
  CONSTRAINT `email_messages_email_folder_id_foreign` FOREIGN KEY (`email_folder_id`) REFERENCES `email_folders` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `email_folders`;
CREATE TABLE `email_folders` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `imap_folder_name` varchar(255) DEFAULT NULL,
  `type` varchar(255) DEFAULT 'custom',
  `icon` varchar(255) DEFAULT 'fa-folder',
  `color` varchar(255) DEFAULT NULL,
  `sort_order` int DEFAULT '0',
  `is_system` tinyint(1) DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `email_settings`;
CREATE TABLE `email_settings` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `imap_host` varchar(255) DEFAULT NULL,
  `imap_port` int DEFAULT '993',
  `imap_encryption` varchar(255) DEFAULT 'ssl',
  `imap_username` varchar(255) DEFAULT NULL,
  `imap_password` varchar(255) DEFAULT NULL,
  `smtp_host` varchar(255) DEFAULT NULL,
  `smtp_port` int DEFAULT '587',
  `smtp_encryption` varchar(255) DEFAULT 'tls',
  `smtp_username` varchar(255) DEFAULT NULL,
  `smtp_password` varchar(255) DEFAULT NULL,
  `email_address` varchar(255) DEFAULT NULL,
  `email_name` varchar(255) DEFAULT NULL,
  `reply_to` varchar(255) DEFAULT NULL,
  `signature` longtext,
  `signature_enabled` tinyint(1) DEFAULT '1',
  `sync_interval_minutes` int DEFAULT '5',
  `last_sync_at` timestamp NULL DEFAULT NULL,
  `is_active` tinyint(1) DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `role_has_permissions`;
CREATE TABLE `role_has_permissions` (
  `permission_id` bigint unsigned NOT NULL,
  `role_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`permission_id`,`role_id`),
  KEY `role_has_permissions_role_id_foreign` (`role_id`),
  CONSTRAINT `role_has_permissions_permission_id_foreign` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE,
  CONSTRAINT `role_has_permissions_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `model_has_roles`;
CREATE TABLE `model_has_roles` (
  `role_id` bigint unsigned NOT NULL,
  `model_type` varchar(255) NOT NULL,
  `model_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`role_id`,`model_id`,`model_type`),
  KEY `model_has_roles_model_id_model_type_index` (`model_id`,`model_type`),
  CONSTRAINT `model_has_roles_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `model_has_permissions`;
CREATE TABLE `model_has_permissions` (
  `permission_id` bigint unsigned NOT NULL,
  `model_type` varchar(255) NOT NULL,
  `model_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`permission_id`,`model_id`,`model_type`),
  KEY `model_has_permissions_model_id_model_type_index` (`model_id`,`model_type`),
  CONSTRAINT `model_has_permissions_permission_id_foreign` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `roles`;
CREATE TABLE `roles` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `guard_name` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `roles_name_guard_name_unique` (`name`,`guard_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `permissions`;
CREATE TABLE `permissions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `guard_name` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `permissions_name_guard_name_unique` (`name`,`guard_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `activity_logs`;
CREATE TABLE `activity_logs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL,
  `action` varchar(255) NOT NULL,
  `module` varchar(255) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `details` json DEFAULT NULL,
  `ip_address` varchar(45) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `activity_logs_user_id_module_index` (`user_id`,`module`),
  KEY `activity_logs_created_at_index` (`created_at`),
  KEY `activity_logs_action_index` (`action`),
  CONSTRAINT `activity_logs_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `role_presets`;
CREATE TABLE `role_presets` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `permissions` json NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `role_presets_name_unique` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `notifiche`;
CREATE TABLE `notifiche` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned DEFAULT NULL,
  `tipo` varchar(255) NOT NULL,
  `titolo` varchar(255) NOT NULL,
  `messaggio` text NOT NULL,
  `link` varchar(255) DEFAULT NULL,
  `is_read` tinyint(1) DEFAULT '0',
  `read_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `notifiche_user_id_index` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `mailing_messaggi`;
CREATE TABLE `mailing_messaggi` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `mailing_list_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `oggetto` varchar(255) NOT NULL,
  `corpo` text,
  `canale` enum('email','telegram') DEFAULT 'email',
  `stato` enum('bozza','in_coda','inviato','fallito') DEFAULT 'bozza',
  `inviato_al` timestamp NULL DEFAULT NULL,
  `totale_destinatari` int DEFAULT '0',
  `invii_ok` int DEFAULT '0',
  `invii_falliti` int DEFAULT '0',
  `log_falliti` json DEFAULT NULL,
  `mittente_nome` varchar(255) DEFAULT NULL,
  `mittente_email` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `mailing_messaggi_tenant_id_index` (`tenant_id`),
  CONSTRAINT `mailing_messaggi_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL,
  CONSTRAINT `mailing_messaggi_mailing_list_id_foreign` FOREIGN KEY (`mailing_list_id`) REFERENCES `mailing_lists` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `mailing_contacts`;
CREATE TABLE `mailing_contacts` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `mailing_list_id` bigint unsigned NOT NULL,
  `individuo_id` bigint unsigned NOT NULL,
  `opt_in` tinyint(1) DEFAULT '1',
  `opt_in_data` datetime DEFAULT NULL,
  `opt_out_data` datetime DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mailing_contacts_mailing_list_id_individuo_id_unique` (`mailing_list_id`,`individuo_id`),
  KEY `mailing_contacts_mailing_list_id_foreign` (`mailing_list_id`),
  KEY `mailing_contacts_individuo_id_foreign` (`individuo_id`),
  CONSTRAINT `mailing_contacts_mailing_list_id_foreign` FOREIGN KEY (`mailing_list_id`) REFERENCES `mailing_lists` (`id`) ON DELETE CASCADE,
  CONSTRAINT `mailing_contacts_individuo_id_foreign` FOREIGN KEY (`individuo_id`) REFERENCES `individui` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `mailing_lists`;
CREATE TABLE `mailing_lists` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `nome` varchar(255) NOT NULL,
  `descrizione` text,
  `attiva` tinyint(1) DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `mailing_lists_tenant_id_index` (`tenant_id`),
  KEY `mailing_lists_user_id_index` (`user_id`),
  CONSTRAINT `mailing_lists_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL,
  CONSTRAINT `mailing_lists_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `documenti`;
CREATE TABLE `documenti` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `cartella_id` bigint unsigned DEFAULT NULL,
  `repository_id` bigint unsigned DEFAULT NULL,
  `storage_disk` varchar(50) DEFAULT NULL,
  `nome_file` varchar(255) NOT NULL,
  `file_path` varchar(255) NOT NULL,
  `tipo` varchar(255) DEFAULT NULL,
  `tipologia` enum('avatar','galleria','documento','statuto','altro','programma','locandina','email_attachment') DEFAULT 'documento',
  `visibilita` enum('pubblico','gruppo','individuo','associazione','federazione','evento') DEFAULT 'gruppo',
  `visibilita_target_id` bigint unsigned DEFAULT NULL,
  `visibilita_target_type` varchar(255) DEFAULT NULL,
  `mime_type` varchar(255) DEFAULT NULL,
  `dimensione` bigint unsigned DEFAULT NULL,
  `note` text,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `documenti_tenant_id_index` (`tenant_id`),
  KEY `documenti_user_id_foreign` (`user_id`),
  KEY `documenti_cartella_id_foreign` (`cartella_id`),
  KEY `documenti_repository_id_foreign` (`repository_id`),
  CONSTRAINT `documenti_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL,
  CONSTRAINT `documenti_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `documenti_cartella_id_foreign` FOREIGN KEY (`cartella_id`) REFERENCES `documenti_cartelle` (`id`) ON DELETE SET NULL,
  CONSTRAINT `documenti_repository_id_foreign` FOREIGN KEY (`repository_id`) REFERENCES `storage_repositories` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `eventi`;
CREATE TABLE `eventi` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `nome_evento` varchar(255) NOT NULL,
  `tipo_evento` varchar(255) DEFAULT NULL,
  `tipo_recorrenza` enum('singolo','settimanale','mensile','annuale','altro') DEFAULT 'singolo',
  `giorno_settimana` tinyint unsigned DEFAULT NULL,
  `giorno_mese` varchar(255) DEFAULT NULL,
  `occorrenza_mese` varchar(10) DEFAULT NULL,
  `mesi_recorrenza` varchar(255) DEFAULT NULL,
  `mese_annuale` varchar(255) DEFAULT NULL,
  `ora_inizio` time DEFAULT NULL,
  `data_specifica` date DEFAULT NULL,
  `durata_minuti` int unsigned DEFAULT NULL,
  `descrizione` text,
  `note` text,
  `luogo_indirizzo` varchar(500) DEFAULT NULL,
  `luogo_url_maps` text,
  `uid_esterno` varchar(500) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `eventi_tenant_id_index` (`tenant_id`),
  KEY `eventi_uid_esterno_index` (`uid_esterno`),
  CONSTRAINT `eventi_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `gruppi`;
CREATE TABLE `gruppi` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `parent_id` bigint unsigned DEFAULT NULL,
  `diocesi_id` bigint unsigned DEFAULT NULL,
  `responsabile_ids` json DEFAULT NULL,
  `nome` varchar(255) NOT NULL,
  `descrizione` text,
  `indirizzo_incontro` varchar(255) DEFAULT NULL,
  `cap_incontro` varchar(10) DEFAULT NULL,
  `città_incontro` varchar(255) DEFAULT NULL,
  `sigla_provincia_incontro` varchar(2) DEFAULT NULL,
  `mappa_posizione` varchar(255) DEFAULT NULL,
  `metadata` json DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `gruppi_tenant_id_index` (`tenant_id`),
  KEY `gruppi_parent_id_index` (`parent_id`),
  KEY `gruppi_diocesi_id_foreign` (`diocesi_id`),
  CONSTRAINT `gruppi_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL,
  CONSTRAINT `gruppi_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `gruppi` (`id`) ON DELETE SET NULL,
  CONSTRAINT `gruppi_diocesi_id_foreign` FOREIGN KEY (`diocesi_id`) REFERENCES `diocesi` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `individui`;
CREATE TABLE `individui` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `codice_id` varchar(5) NOT NULL,
  `cognome` varchar(255) NOT NULL,
  `nome` varchar(255) NOT NULL,
  `data_nascita` date DEFAULT NULL,
  `indirizzo` varchar(255) DEFAULT NULL,
  `cap` varchar(10) DEFAULT NULL,
  `città` varchar(255) DEFAULT NULL,
  `sigla_provincia` varchar(2) DEFAULT NULL,
  `genere` enum('M','F') DEFAULT NULL,
  `tipo_documento` enum('carta_identita','patente') DEFAULT NULL,
  `numero_documento` varchar(50) DEFAULT NULL,
  `scadenza_documento` date DEFAULT NULL,
  `note` text,
  `avatar_path` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `individui_codice_id_unique` (`codice_id`),
  KEY `individui_tenant_id_index` (`tenant_id`),
  KEY `individui_codice_id_index` (`codice_id`),
  KEY `individui_cognome_index` (`cognome`),
  CONSTRAINT `individui_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `comuni`;
CREATE TABLE `comuni` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `codice_istat` varchar(10) DEFAULT NULL,
  `cap` varchar(255) DEFAULT NULL,
  `sigla_provincia` varchar(2) DEFAULT NULL,
  `regione` varchar(255) DEFAULT NULL,
  `latitudine` double DEFAULT NULL,
  `longitudine` double DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `comuni_nome_index` (`nome`),
  KEY `comuni_sigla_provincia_index` (`sigla_provincia`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `diocesi`;
CREATE TABLE `diocesi` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `regione` varchar(255) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `tenants`;
CREATE TABLE `tenants` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL,
  `email` varchar(255) DEFAULT NULL,
  `telefono` varchar(255) DEFAULT NULL,
  `note` text,
  `attivo` tinyint(1) DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tenants_slug_unique` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tenant_id` bigint unsigned DEFAULT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) NOT NULL,
  `is_admin` tinyint(1) DEFAULT '0',
  `status` varchar(20) DEFAULT 'active',
  `permissions` json DEFAULT NULL,
  `role_preset_id` bigint unsigned DEFAULT NULL,
  `remember_token` varchar(100) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`),
  KEY `users_tenant_id_index` (`tenant_id`),
  KEY `users_email_index` (`email`),
  CONSTRAINT `users_tenant_id_foreign` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `password_reset_tokens`;
CREATE TABLE `password_reset_tokens` (
  `email` varchar(255) NOT NULL,
  `token` varchar(255) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `sessions`;
CREATE TABLE `sessions` (
  `id` varchar(255) NOT NULL,
  `user_id` bigint unsigned DEFAULT NULL,
  `ip_address` varchar(45) DEFAULT NULL,
  `user_agent` text,
  `payload` longtext 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;

DROP TABLE IF EXISTS `cache`;
CREATE TABLE `cache` (
  `key` varchar(255) NOT NULL,
  `value` mediumtext NOT NULL,
  `expiration` int NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `cache_locks`;
CREATE TABLE `cache_locks` (
  `key` varchar(255) NOT NULL,
  `owner` varchar(255) NOT NULL,
  `expiration` int NOT NULL,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `jobs`;
CREATE TABLE `jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `queue` varchar(255) NOT NULL,
  `payload` longtext 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;

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

DROP TABLE IF EXISTS `failed_jobs`;
CREATE TABLE `failed_jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `uuid` varchar(255) NOT NULL,
  `connection` text NOT NULL,
  `queue` text NOT NULL,
  `payload` longtext NOT NULL,
  `exception` longtext 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;

DROP TABLE IF EXISTS `app_settings`;
CREATE TABLE `app_settings` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `nome_applicazione` varchar(255) DEFAULT NULL,
  `nome_organizzazione` varchar(255) DEFAULT NULL,
  `logo` varchar(255) DEFAULT NULL,
  `logo_small` varchar(255) DEFAULT NULL,
  `logo_path` varchar(255) DEFAULT NULL,
  `logo_small_path` varchar(255) DEFAULT NULL,
  `footer_text` varchar(255) DEFAULT NULL,
  `app_version` varchar(255) DEFAULT NULL,
  `dashboard_welcome` varchar(255) DEFAULT NULL,
  `show_version` tinyint(1) DEFAULT '0',
  `documenti_storage_disk` varchar(255) DEFAULT 'local',
  `documenti_storage_path` varchar(255) DEFAULT 'documenti',
  `backup_path` varchar(255) DEFAULT 'backups',
  `backup_retention_days` int DEFAULT '30',
  `backup_include_files` tinyint(1) DEFAULT '1',
  `backup_include_env` tinyint(1) DEFAULT '1',
  `backup_auto_enabled` tinyint(1) DEFAULT '0',
  `backup_auto_frequency` varchar(20) DEFAULT 'daily',
  `backup_auto_hour` int DEFAULT '3',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ============================================================================
-- SEED DATA
-- ============================================================================

-- Diocesi
INSERT INTO `diocesi` (`nome`, `regione`, `created_at`, `updated_at`) VALUES
('Acqui Terme', 'Piemonte', NOW(), NOW()),
('Afragola', 'Campania', NOW(), NOW()),
('Agrigento', 'Sicilia', NOW(), NOW()),
('Alba', 'Piemonte', NOW(), NOW()),
('Alessandria', 'Piemonte', NOW(), NOW()),
('Ancona', 'Marche', NOW(), NOW()),
('Aosta', 'Valle d''Aosta', NOW(), NOW()),
('Aquila', 'Abruzzo', NOW(), NOW()),
('Arezzo', 'Toscana', NOW(), NOW()),
('Ascoli Piceno', 'Marche', NOW(), NOW()),
('Assisi', 'Umbria', NOW(), NOW()),
('Asti', 'Piemonte', NOW(), NOW()),
('Avellino', 'Campania', NOW(), NOW()),
('Bari', 'Puglia', NOW(), NOW()),
('Barletta', 'Puglia', NOW(), NOW()),
('Belluno', 'Veneto', NOW(), NOW()),
('Benevento', 'Campania', NOW(), NOW()),
('Bergamo', 'Lombardia', NOW(), NOW()),
('Biella', 'Piemonte', NOW(), NOW()),
('Bologna', 'Emilia-Romagna', NOW(), NOW()),
('Bolzano', 'Trentino-Alto Adige', NOW(), NOW()),
('Brescia', 'Lombardia', NOW(), NOW()),
('Brindisi', 'Puglia', NOW(), NOW()),
('Cagliari', 'Sardegna', NOW(), NOW()),
('Caltanissetta', 'Sicilia', NOW(), NOW()),
('Campobasso', 'Molise', NOW(), NOW()),
('Caserta', 'Campania', NOW(), NOW()),
('Cassano allo Ionio', 'Calabria', NOW(), NOW()),
('Castelvetrano', 'Sicilia', NOW(), NOW()),
('Catania', 'Sicilia', NOW(), NOW()),
('Catanzaro', 'Calabria', NOW(), NOW()),
('Cefalù', 'Sicilia', NOW(), NOW()),
('Cerro al Lambro', 'Lombardia', NOW(), NOW()),
('Cesena', 'Emilia-Romagna', NOW(), NOW()),
('Chieti', 'Abruzzo', NOW(), NOW()),
('Civitavecchia', 'Lazio', NOW(), NOW()),
('Cosenza', 'Calabria', NOW(), NOW()),
('Cremona', 'Lombardia', NOW(), NOW()),
('Crotone', 'Calabria', NOW(), NOW()),
('Cuneo', 'Piemonte', NOW(), NOW()),
('Donegal', 'Irlanda', NOW(), NOW()),
('Enna', 'Sicilia', NOW(), NOW()),
('Fano', 'Marche', NOW(), NOW()),
('Fermo', 'Marche', NOW(), NOW()),
('Ferrara', 'Emilia-Romagna', NOW(), NOW()),
('Firenze', 'Toscana', NOW(), NOW()),
('Foggia', 'Puglia', NOW(), NOW()),
('Foligno', 'Umbria', NOW(), NOW()),
('Forlì', 'Emilia-Romagna', NOW(), NOW()),
('Frosinone', 'Lazio', NOW(), NOW()),
('Genova', 'Liguria', NOW(), NOW()),
('Gorizia', 'Friuli-Venezia Giulia', NOW(), NOW()),
('Grosseto', 'Toscana', NOW(), NOW()),
('Gubbio', 'Umbria', NOW(), NOW()),
('Imola', 'Emilia-Romagna', NOW(), NOW()),
('Isernia', 'Molise', NOW(), NOW()),
('L''Aquila', 'Abruzzo', NOW(), NOW()),
('La Spezia', 'Liguria', NOW(), NOW()),
('Lamezia Terme', 'Calabria', NOW(), NOW()),
('Latina', 'Lazio', NOW(), NOW()),
('Lecce', 'Puglia', NOW(), NOW()),
('Lecco', 'Lombardia', NOW(), NOW()),
('Livorno', 'Toscana', NOW(), NOW()),
('Lodi', 'Lombardia', NOW(), NOW()),
('Lucca', 'Toscana', NOW(), NOW()),
('Macerata', 'Marche', NOW(), NOW()),
('Mantova', 'Lombardia', NOW(), NOW()),
('Massa', 'Toscana', NOW(), NOW()),
('Matera', 'Basilicata', NOW(), NOW()),
('Messina', 'Sicilia', NOW(), NOW()),
('Milano', 'Lombardia', NOW(), NOW()),
('Modena', 'Emilia-Romagna', NOW(), NOW()),
('Monaco', 'Germania', NOW(), NOW()),
('Mongolia', 'Asia', NOW(), NOW()),
('Monreale', 'Sicilia', NOW(), NOW()),
('Montefeltro', 'Marche', NOW(), NOW()),
('Montepulciano', 'Toscana', NOW(), NOW()),
('Monza', 'Lombardia', NOW(), NOW()),
('Napoli', 'Campania', NOW(), NOW()),
('Novara', 'Piemonte', NOW(), NOW()),
('Nuoro', 'Sardegna', NOW(), NOW()),
('Oristano', 'Sardegna', NOW(), NOW()),
('Otranto', 'Puglia', NOW(), NOW()),
('Padova', 'Veneto', NOW(), NOW()),
('Palermo', 'Sicilia', NOW(), NOW()),
('Parma', 'Emilia-Romagna', NOW(), NOW()),
('Pavia', 'Lombardia', NOW(), NOW()),
('Perugia', 'Umbria', NOW(), NOW()),
('Pesaro', 'Marche', NOW(), NOW()),
('Piacenza', 'Emilia-Romagna', NOW(), NOW()),
('Pisa', 'Toscana', NOW(), NOW()),
('Pistoia', 'Toscana', NOW(), NOW()),
('Pordenone', 'Friuli-Venezia Giulia', NOW(), NOW()),
('Potenza', 'Basilicata', NOW(), NOW()),
('Prato', 'Toscana', NOW(), NOW()),
('Ragusa', 'Sicilia', NOW(), NOW()),
('Ravenna', 'Emilia-Romagna', NOW(), NOW()),
('Reggio Calabria', 'Calabria', NOW(), NOW()),
('Reggio Emilia', 'Emilia-Romagna', NOW(), NOW()),
('Rieti', 'Lazio', NOW(), NOW()),
('Rimini', 'Emilia-Romagna', NOW(), NOW()),
('Roma', 'Lazio', NOW(), NOW()),
('Rosario', 'Argentina', NOW(), NOW()),
('Rovigo', 'Veneto', NOW(), NOW()),
('Salerno', 'Campania', NOW(), NOW()),
('San Severo', 'Puglia', NOW(), NOW()),
('Sassari', 'Sardegna', NOW(), NOW()),
('Savona', 'Liguria', NOW(), NOW()),
('Siena', 'Toscana', NOW(), NOW()),
('Siracusa', 'Sicilia', NOW(), NOW()),
('Sondrio', 'Lombardia', NOW(), NOW()),
('Spoleto', 'Umbria', NOW(), NOW()),
('Taranto', 'Puglia', NOW(), NOW()),
('Tempio Ampurias', 'Sardegna', NOW(), NOW()),
('Teramo', 'Abruzzo', NOW(), NOW()),
('Terni', 'Umbria', NOW(), NOW()),
('Tirana', 'Albania', NOW(), NOW()),
('Torino', 'Piemonte', NOW(), NOW()),
('Tortona', 'Piemonte', NOW(), NOW()),
('Trani', 'Puglia', NOW(), NOW()),
('Trapani', 'Sicilia', NOW(), NOW()),
('Trento', 'Trentino-Alto Adige', NOW(), NOW()),
('Treviso', 'Veneto', NOW(), NOW()),
('Trieste', 'Friuli-Venezia Giulia', NOW(), NOW()),
('Udine', 'Friuli-Venezia Giulia', NOW(), NOW()),
('Urbino', 'Marche', NOW(), NOW()),
('Vaticano', 'Lazio', NOW(), NOW()),
('Vercelli', 'Piemonte', NOW(), NOW()),
('Verona', 'Veneto', NOW(), NOW()),
('Vibo Valentia', 'Calabria', NOW(), NOW()),
('Vicenza', 'Veneto', NOW(), NOW()),
('Viterbo', 'Lazio', NOW(), NOW()),
('Vittorio Veneto', 'Veneto', NOW(), NOW());

-- Comuni
INSERT INTO `comuni` (`nome`, `codice_istat`, `cap`, `sigla_provincia`, `regione`, `created_at`, `updated_at`) VALUES
('Roma', '058091', '00100', 'RM', 'Lazio', NOW(), NOW()),
('Milano', '015146', '20100', 'MI', 'Lombardia', NOW(), NOW()),
('Napoli', '063138', '80100', 'NA', 'Campania', NOW(), NOW()),
('Torino', '001272', '10100', 'TO', 'Piemonte', NOW(), NOW()),
('Palermo', 'G273', '90100', 'PA', 'Sicilia', NOW(), NOW()),
('Genova', '009969', '16100', 'GE', 'Liguria', NOW(), NOW()),
('Bologna', '037015', '40100', 'BO', 'Emilia-Romagna', NOW(), NOW()),
('Firenze', '048017', '50100', 'FI', 'Toscana', NOW(), NOW()),
('Bari', 'A662', '70100', 'BA', 'Puglia', NOW(), NOW()),
('Catania', 'C351', '95100', 'CT', 'Sicilia', NOW(), NOW()),
('Venezia', 'L736', '30100', 'VE', 'Veneto', NOW(), NOW()),
('Verona', 'L780', '37100', 'VR', 'Veneto', NOW(), NOW()),
('Messina', 'F158', '98100', 'ME', 'Sicilia', NOW(), NOW()),
('Padova', 'G224', '35100', 'PD', 'Veneto', NOW(), NOW()),
('Trieste', 'L424', '34100', 'TS', 'Friuli-Venezia Giulia', NOW(), NOW()),
('Brescia', 'B157', '25100', 'BS', 'Lombardia', NOW(), NOW()),
('Reggio Calabria', 'H224', '89100', 'RC', 'Calabria', NOW(), NOW()),
('Taranto', 'L049', '74100', 'TA', 'Puglia', NOW(), NOW()),
('Modena', 'F257', '41100', 'MO', 'Emilia-Romagna', NOW(), NOW()),
('Parma', 'G337', '43100', 'PR', 'Emilia-Romagna', NOW(), NOW()),
('Reggio Emilia', 'H223', '42100', 'RE', 'Emilia-Romagna', NOW(), NOW()),
('Perugia', 'G478', '06100', 'PG', 'Umbria', NOW(), NOW()),
('Ravenna', 'H199', '48100', 'RA', 'Emilia-Romagna', NOW(), NOW()),
('Livorno', 'E625', '57100', 'LI', 'Toscana', NOW(), NOW()),
('Cagliari', 'B354', '09100', 'CA', 'Sardegna', NOW(), NOW()),
('Foggia', 'D643', '71100', 'FG', 'Puglia', NOW(), NOW()),
('Rimini', 'H294', '47900', 'RN', 'Emilia-Romagna', NOW(), NOW()),
('Salerno', 'H703', '84100', 'SA', 'Campania', NOW(), NOW()),
('Ferrara', 'D548', '44100', 'FE', 'Emilia-Romagna', NOW(), NOW()),
('Sassari', 'I452', '07100', 'SS', 'Sardegna', NOW(), NOW()),
('Latina', 'E472', '04100', 'LT', 'Lazio', NOW(), NOW()),
('Giugliano in Campania', 'E045', '80014', 'NA', 'Campania', NOW(), NOW()),
('Monza', 'F704', '20900', 'MB', 'Lombardia', NOW(), NOW()),
('Siracusa', 'I754', '96100', 'SR', 'Sicilia', NOW(), NOW()),
('Bergamo', 'A794', '24100', 'BG', 'Lombardia', NOW(), NOW()),
('Pescara', 'G482', '65100', 'PE', 'Abruzzo', NOW(), NOW()),
('Forlì', 'D704', '47100', 'FC', 'Emilia-Romagna', NOW(), NOW()),
('Trento', 'L378', '38100', 'TN', 'Trentino-Alto Adige', NOW(), NOW()),
('Terni', 'L117', '05100', 'TR', 'Umbria', NOW(), NOW()),
('Novara', 'F952', '28100', 'NO', 'Piemonte', NOW(), NOW()),
('Ancona', 'A271', '60100', 'AN', 'Marche', NOW(), NOW()),
('Piacenza', 'G535', '29100', 'PC', 'Emilia-Romagna', NOW(), NOW()),
('Lecce', 'E506', '73100', 'LE', 'Puglia', NOW(), NOW()),
('Bolzano', 'A952', '39100', 'BZ', 'Trentino-Alto Adige', NOW(), NOW()),
('Catanzaro', 'C352', '88100', 'CZ', 'Calabria', NOW(), NOW()),
('Udine', 'L483', '33100', 'UD', 'Friuli-Venezia Giulia', NOW(), NOW()),
('Aversa', 'A512', '81031', 'CE', 'Campania', NOW(), NOW()),
('Potenza', 'G942', '85100', 'PZ', 'Basilicata', NOW(), NOW()),
('Castellammare di Stabia', 'C129', '80053', 'NA', 'Campania', NOW(), NOW()),
('Afragola', 'A064', '80021', 'NA', 'Campania', NOW(), NOW()),
('Crotone', 'D122', '88900', 'KR', 'Calabria', NOW(), NOW()),
('Pavia', 'G388', '27100', 'PV', 'Lombardia', NOW(), NOW()),
('Caserta', 'B963', '81100', 'CE', 'Campania', NOW(), NOW()),
('Salò', 'H727', '25087', 'BS', 'Lombardia', NOW(), NOW());

-- Tipologie documenti
INSERT INTO `tipologie_documenti` (`nome`, `descrizione`, `ordine`, `attiva`, `created_at`, `updated_at`) VALUES
('documento', 'Documento', 1, 1, NOW(), NOW()),
('avatar', 'Avatar', 2, 1, NOW(), NOW()),
('galleria', 'Galleria', 3, 1, NOW(), NOW()),
('statuto', 'Statuto', 4, 1, NOW(), NOW()),
('programma', 'Programma', 5, 1, NOW(), NOW()),
('locandina', 'Locandina', 6, 1, NOW(), NOW()),
('altro', 'Altro', 7, 1, NOW(), NOW()),
('email_attachment', 'Allegato email', 100, 1, NOW(), NOW());

-- Tipologie eventi
INSERT INTO `tipologie_eventi` (`nome`, `descrizione`, `ordine`, `attiva`, `created_at`, `updated_at`) VALUES
('catechesi', 'Catechesi', 1, 1, NOW(), NOW()),
('liturgia', 'Liturgia', 2, 1, NOW(), NOW()),
('animazione', 'Animazione', 3, 1, NOW(), NOW()),
('formazione', 'Formazione', 4, 1, NOW(), NOW()),
('incontro', 'Incontro', 5, 1, NOW(), NOW()),
('riunione', 'Riunione', 6, 1, NOW(), NOW()),
('festa', 'Festa', 7, 1, NOW(), NOW()),
('altro', 'Altro', 8, 1, NOW(), NOW());

-- Ruoli
INSERT INTO `ruoli` (`nome`, `descrizione`, `ordine`, `attiva`, `created_at`, `updated_at`) VALUES
('presidente', 'Presidente', 1, 1, NOW(), NOW()),
('vicepresidente', 'Vicepresidente', 2, 1, NOW(), NOW()),
('segretario', 'Segretario', 3, 1, NOW(), NOW()),
('tesoriere', 'Tesoriere', 4, 1, NOW(), NOW()),
('consigliere', 'Consigliere', 5, 1, NOW(), NOW()),
('volontario', 'Volontario', 6, 1, NOW(), NOW()),
('socio', 'Socio', 7, 1, NOW(), NOW()),
('altro', 'Altro', 8, 1, NOW(), NOW());

-- Spatie permissions
INSERT INTO `permissions` (`name`, `guard_name`, `created_at`, `updated_at`) VALUES
('view individuals', 'web', NOW(), NOW()),
('create individuals', 'web', NOW(), NOW()),
('edit individuals', 'web', NOW(), NOW()),
('delete individuals', 'web', NOW(), NOW()),
('view groups', 'web', NOW(), NOW()),
('create groups', 'web', NOW(), NOW()),
('edit groups', 'web', NOW(), NOW()),
('delete groups', 'web', NOW(), NOW()),
('view events', 'web', NOW(), NOW()),
('create events', 'web', NOW(), NOW()),
('edit events', 'web', NOW(), NOW()),
('delete events', 'web', NOW(), NOW()),
('view documents', 'web', NOW(), NOW()),
('create documents', 'web', NOW(), NOW()),
('edit documents', 'web', NOW(), NOW()),
('delete documents', 'web', NOW(), NOW()),
('view mailing lists', 'web', NOW(), NOW()),
('create mailing lists', 'web', NOW(), NOW()),
('edit mailing lists', 'web', NOW(), NOW()),
('delete mailing lists', 'web', NOW(), NOW()),
('send mailing', 'web', NOW(), NOW()),
('manage settings', 'web', NOW(), NOW()),
('manage tipologie', 'web', NOW(), NOW());

-- Spatie roles
INSERT INTO `roles` (`name`, `guard_name`, `created_at`, `updated_at`) VALUES
('admin', 'web', NOW(), NOW()),
('user', 'web', NOW(), NOW());

-- Spatie role_has_permissions: admin gets all 23 permissions
INSERT INTO `role_has_permissions` (`role_id`, `permission_id`)
SELECT 1, `id` FROM `permissions`;

-- Spatie role_has_permissions: user gets limited permissions
INSERT INTO `role_has_permissions` (`role_id`, `permission_id`)
SELECT 2, `id` FROM `permissions` WHERE `name` IN (
    'view individuals', 'create individuals', 'edit individuals',
    'view groups',
    'view events', 'create events', 'edit events',
    'view documents', 'create documents', 'edit documents',
    'view mailing lists', 'send mailing'
);

-- Role presets
INSERT INTO `role_presets` (`name`, `description`, `permissions`, `created_at`, `updated_at`) VALUES
('admin', 'Amministratore con accesso completo a tutti i moduli',
 '{\"individui\":2,\"gruppi\":2,\"eventi\":2,\"documenti\":2,\"mailing\":2,\"viste\":2,\"report\":2,\"settings\":2}',
 NOW(), NOW()),
('operatore', 'Operatore con accesso completo a individui/gruppi/eventi, sola lettura per documenti/mailing',
 '{\"individui\":2,\"gruppi\":2,\"eventi\":2,\"documenti\":1,\"mailing\":1,\"viste\":1,\"report\":1,\"settings\":1}',
 NOW(), NOW()),
('lettura', 'Sola lettura su tutti i moduli',
 '{\"individui\":1,\"gruppi\":1,\"eventi\":1,\"documenti\":1,\"mailing\":1,\"viste\":1,\"report\":1,\"settings\":1}',
 NOW(), NOW());

-- Email folders (system)
INSERT INTO `email_folders` (`name`, `imap_folder_name`, `type`, `icon`, `color`, `sort_order`, `is_system`, `created_at`, `updated_at`) VALUES
('Posta in arrivo', 'INBOX', 'inbox', 'fa-inbox', '#28a745', 1, 1, NOW(), NOW()),
('Inviata', 'SENT', 'sent', 'fa-paper-plane', '#007bff', 2, 1, NOW(), NOW()),
('Bozze', 'DRAFTS', 'draft', 'fa-edit', '#ffc107', 3, 1, NOW(), NOW()),
('Cestino', 'TRASH', 'trash', 'fa-trash', '#dc3545', 4, 1, NOW(), NOW()),
('Spam', 'SPAM', 'spam', 'fa-exclamation-triangle', '#fd7e14', 5, 1, NOW(), NOW());

-- App settings (default row)
INSERT INTO `app_settings` (`nome_applicazione`, `documenti_storage_disk`, `documenti_storage_path`, `backup_path`, `backup_retention_days`, `backup_include_files`, `backup_include_env`, `backup_auto_enabled`, `backup_auto_frequency`, `backup_auto_hour`, `show_version`, `created_at`, `updated_at`)
VALUES ('Glastree', 'local', 'documenti', 'backups', 30, 1, 1, 0, 'daily', 3, 0, NOW(), NOW());

SET FOREIGN_KEY_CHECKS = 1;
