-- =====================================================================
-- Tabela: tb_app (catálogo de "apps" — famílias de integração)
-- Banco:  w19_clikil41_eleicao
-- Projeto: f19
--
-- Um App é uma família de integração (zapi, claude, chatgpt, meta, rapid,
-- sendflow, stripe). Cada App tem N instâncias em tb_api (instâncias = chaves/
-- tokens diferentes daquele app — ex: 60 instâncias Z-API distintas, cada
-- uma de um apoiador).
--
-- app_source = nome da pasta em api.w19.com.br/<source>/ que contém os
-- endpoints específicos daquele app (ex: api.w19.com.br/zapi/api.php).
-- =====================================================================

CREATE TABLE IF NOT EXISTS `tb_app` (
  `id_app`           SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `app_name`         VARCHAR(60)  NOT NULL,           -- chave técnica (en, snake_case): 'zapi','claude','chatgpt',...
  `app_source`       VARCHAR(60)  NOT NULL,           -- pasta dentro de api.w19.com.br/ (geralmente == app_name)
  `app_label`        VARCHAR(100) DEFAULT NULL,       -- label exibido em UI (pt): 'Z-API', 'Claude', 'ChatGPT'
  `app_description`  VARCHAR(255) DEFAULT NULL,
  `app_status`       TINYINT(1)   NOT NULL DEFAULT 1, -- 1 ativo, 0 inativo, -1 deprecated
  `app_created_at`   DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `app_updated_at`   DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_app`),
  UNIQUE KEY `uk_app_name`  (`app_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Seed inicial dos apps usados na Etapa 1
INSERT INTO `tb_app` (`id_app`, `app_name`, `app_source`, `app_label`, `app_description`) VALUES
  (1, 'zapi',     'zapi',     'Z-API',           'WhatsApp via instâncias Z-API (1 instância = 1 apoiador)'),
  (2, 'claude',   'claude',   'Anthropic Claude','Sentiment, classificação, geração de texto (Anthropic API)'),
  (3, 'chatgpt',  'chatgpt',  'ChatGPT',         'OpenAI GPT (chat/completion)'),
  (4, 'rapid',    'rapid',    'RapidAPI',        'Hub de APIs (Instagram, etc.)'),
  (5, 'meta',     'meta',     'Meta Business',   'Facebook/Instagram Ads/Insights'),
  (6, 'sendflow', 'sendflow', 'Sendflow',        'Sendflow (SMS/transactional)'),
  (7, 'stripe',   'stripe',   'Stripe',          'Gateway de pagamento')
ON DUPLICATE KEY UPDATE
  `app_source`      = VALUES(`app_source`),
  `app_label`       = VALUES(`app_label`),
  `app_description` = VALUES(`app_description`);
