-- Co-Lite change add-whatsapp-inbox: the WhatsApp inbox schema. -- Target: new schema `service-whatsapp`. Additive only. Run once, after the release package. -- DDL: run with a user that may create databases and tables, not the services' user. -- company_id, owner_user_id, sent_by_user_id and user_id point at `service-system`; lead_uuid at `service-counter`.`lead`.`uuid`; no foreign keys across schemas. -- direction (IN | OUT), type and status are varchar, not ENUM, like the rest of the repo. CREATE DATABASE IF NOT EXISTS `service-whatsapp` CHARACTER SET utf8mb4; USE `service-whatsapp`; CREATE TABLE `wa_conversation` ( `id` bigint NOT NULL AUTO_INCREMENT, `uuid` varchar(36) NOT NULL, `company_id` bigint NOT NULL, `phone_number_id` varchar(32) NOT NULL, `contact_wa_id` varchar(20) NOT NULL COMMENT 'E.164 digits, no leading +', `contact_name` varchar(255) NULL, `lead_uuid` bigint NULL, `owner_user_id` bigint NULL, `owner_manual` boolean NOT NULL DEFAULT false, `last_inbound_at` datetime NULL, `last_message_at` datetime NULL, `last_message_preview` varchar(80) NULL, `created_at` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_wa_conversation_uuid` (`uuid`), UNIQUE KEY `uk_wa_conversation_contact` (`company_id`, `contact_wa_id`), KEY `idx_wa_conversation_owner` (`company_id`, `owner_user_id`, `last_message_at`), KEY `idx_wa_conversation_last_message` (`company_id`, `last_message_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `wa_message` ( `id` bigint NOT NULL AUTO_INCREMENT, `uuid` varchar(36) NOT NULL, `conversation_id` bigint NOT NULL, `direction` varchar(3) NOT NULL COMMENT 'IN | OUT', `wamid` varchar(128) NULL COMMENT 'NULL while an OUT message is pending, before Meta answers', `type` varchar(16) NOT NULL COMMENT 'text | template | image | document | audio | video | sticker | location | unsupported', `body` text NULL, `template_name` varchar(255) NULL, `media_id` varchar(128) NULL, `media_mime` varchar(128) NULL, `media_filename` varchar(255) NULL, `location_lat` decimal(10,7) NULL, `location_lng` decimal(10,7) NULL, `location_name` varchar(255) NULL, `status` varchar(16) NULL COMMENT 'OUT only: pending | sent | delivered | read | failed', `error_code` varchar(32) NULL, `error_title` varchar(255) NULL, `sent_by_user_id` bigint NULL, `created_at` datetime NOT NULL, `status_at` datetime NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_wa_message_uuid` (`uuid`), UNIQUE KEY `uk_wa_message_wamid` (`wamid`), KEY `idx_wa_message_conversation` (`conversation_id`, `id`), CONSTRAINT `fk_wa_message_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wa_conversation` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `wa_read` ( `conversation_id` bigint NOT NULL, `user_id` bigint NOT NULL, `last_read_message_id` bigint NOT NULL, PRIMARY KEY (`conversation_id`, `user_id`), CONSTRAINT `fk_wa_read_conversation` FOREIGN KEY (`conversation_id`) REFERENCES `wa_conversation` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `wa_template` ( `id` bigint NOT NULL AUTO_INCREMENT, `company_id` bigint NOT NULL, `name` varchar(255) NOT NULL, `language` varchar(16) NOT NULL, `category` varchar(32) NULL, `status` varchar(32) NOT NULL, `body_text` text NULL, `param_count` int NOT NULL DEFAULT 0, `synced_at` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk_wa_template` (`company_id`, `name`, `language`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;