CREATE TABLE IF NOT EXISTS `wa_atendentes` (
  `id` int NOT NULL AUTO_INCREMENT,
  `nome` varchar(100) NOT NULL,
  `telefone` varchar(40) NOT NULL,
  `ativo` tinyint(1) NOT NULL DEFAULT 1,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `telefone` (`telefone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE IF NOT EXISTS `wa_atendimentos` (
  `id` int NOT NULL AUTO_INCREMENT,
  `conversa_id` int DEFAULT NULL,
  `numero` varchar(40) NOT NULL,
  `nome` varchar(150) DEFAULT NULL,
  `status` varchar(30) NOT NULL DEFAULT 'aberto',
  `atendente` varchar(100) DEFAULT NULL,
  `observacoes` text DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `finalized_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `conversa_id` (`conversa_id`),
  KEY `numero` (`numero`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

ALTER TABLE `wa_atendimentos`
  ADD CONSTRAINT `wa_atendimentos_ibfk_1`
  FOREIGN KEY (`conversa_id`) REFERENCES `wa_conversas` (`id`) ON DELETE SET NULL;
