-- =============================================
-- MIGRATION: Admin Panel Tables
-- Data: 2026-02-17
-- Descrição: Cria tabelas para gestão admin
-- =============================================

-- TABELA: planos
CREATE TABLE IF NOT EXISTS `planos` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `nome` VARCHAR(50) NOT NULL,
    `slug` VARCHAR(50) NOT NULL,
    `valor` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    `descricao` TEXT NULL,
    `max_colaboradores` INT(11) NOT NULL DEFAULT 0,
    `max_clientes` INT(11) NOT NULL DEFAULT 0,
    `ativo` TINYINT(1) NOT NULL DEFAULT 1,
    `created_at` DATETIME NULL,
    `updated_at` DATETIME NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed planos
INSERT INTO `planos` (`nome`, `slug`, `valor`, `descricao`, `max_colaboradores`, `max_clientes`, `ativo`, `created_at`) VALUES
('Bronze', 'bronze', 49.90, 'Plano básico', 2, 100, 1, NOW()),
('Prata', 'prata', 99.90, 'Plano intermediário', 5, 500, 1, NOW()),
('Ouro', 'ouro', 149.90, 'Plano avançado', 15, 2000, 1, NOW()),
('Diamante', 'diamante', 199.90, 'Plano premium ilimitado', 0, 0, 1, NOW());

-- TABELA: faturas
CREATE TABLE IF NOT EXISTS `faturas` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) UNSIGNED NOT NULL,
    `plano_id` INT(11) UNSIGNED NULL,
    `referencia` VARCHAR(7) NOT NULL,
    `valor` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    `status` ENUM('pendente','pago','atrasado','cancelado') NOT NULL DEFAULT 'pendente',
    `data_vencimento` DATE NOT NULL,
    `data_pagamento` DATETIME NULL,
    `mp_payment_id` VARCHAR(100) NULL,
    `mp_preference_id` VARCHAR(100) NULL,
    `pix_copia_cola` TEXT NULL,
    `pix_qr_code_base64` LONGTEXT NULL,
    `observacao` TEXT NULL,
    `created_at` DATETIME NULL,
    `updated_at` DATETIME NULL,
    PRIMARY KEY (`id`),
    INDEX `idx_faturas_user_id` (`user_id`),
    INDEX `idx_faturas_status` (`status`),
    INDEX `idx_faturas_referencia` (`referencia`),
    INDEX `idx_faturas_vencimento` (`data_vencimento`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- TABELA: tickets_suporte
CREATE TABLE IF NOT EXISTS `tickets_suporte` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) UNSIGNED NOT NULL,
    `assunto` VARCHAR(255) NOT NULL,
    `mensagem` TEXT NOT NULL,
    `status` ENUM('aberto','em_andamento','resolvido','fechado') NOT NULL DEFAULT 'aberto',
    `prioridade` ENUM('baixa','media','alta','urgente') NOT NULL DEFAULT 'media',
    `categoria` VARCHAR(100) NULL,
    `created_at` DATETIME NULL,
    `updated_at` DATETIME NULL,
    `closed_at` DATETIME NULL,
    PRIMARY KEY (`id`),
    INDEX `idx_tickets_user_id` (`user_id`),
    INDEX `idx_tickets_status` (`status`),
    INDEX `idx_tickets_prioridade` (`prioridade`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- TABELA: ticket_respostas
CREATE TABLE IF NOT EXISTS `ticket_respostas` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `ticket_id` INT(11) UNSIGNED NOT NULL,
    `user_id` INT(11) UNSIGNED NOT NULL,
    `is_admin` TINYINT(1) NOT NULL DEFAULT 0,
    `mensagem` TEXT NOT NULL,
    `created_at` DATETIME NULL,
    PRIMARY KEY (`id`),
    INDEX `idx_respostas_ticket_id` (`ticket_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- TABELA: admin_config
CREATE TABLE IF NOT EXISTS `admin_config` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `chave` VARCHAR(100) NOT NULL,
    `valor` TEXT NULL,
    `updated_at` DATETIME NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `chave` (`chave`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed admin_config
INSERT INTO `admin_config` (`chave`, `valor`, `updated_at`) VALUES
('mp_access_token', '', NOW()),
('mp_public_key', '', NOW()),
('whatsapp_instancia', '', NOW()),
('dia_vencimento', '10', NOW()),
('email_admin', '', NOW()),
('nome_sistema', 'Sou Mulher Agenda', NOW());
