-- 2026-04-16
-- Generalizacao da estrutura de tipos do modulo Partners
-- Objetivos:
-- 1) renomear a tabela wpjy_parceiros_estabelecimentos_tipos para wpjy_parceiros_tipos
-- 2) renomear o vinculo da empresa de estabelecimento_tipo_id para tipo_id
-- 3) preparar a base para tipos aplicaveis a empresa, profissional ou ambos

SET @schema_name = DATABASE();

-- 1) renomeia a tabela principal de tipos, se ainda estiver no nome antigo
SET @has_old_table = (
    SELECT COUNT(*)
      FROM information_schema.tables
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_estabelecimentos_tipos'
);
SET @has_new_table = (
    SELECT COUNT(*)
      FROM information_schema.tables
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
);

SET @sql = IF(
    @has_old_table = 1 AND @has_new_table = 0,
    'RENAME TABLE `wpjy_parceiros_estabelecimentos_tipos` TO `wpjy_parceiros_tipos`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @has_new_table = (
    SELECT COUNT(*)
      FROM information_schema.tables
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
);

-- 2) inclui o escopo da entidade na tabela generica de tipos
SET @has_entidade = (
    SELECT COUNT(*)
      FROM information_schema.columns
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
       AND column_name = 'entidade'
);

SET @sql = IF(
    @has_new_table = 1 AND @has_entidade = 0,
    'ALTER TABLE `wpjy_parceiros_tipos`
        ADD COLUMN `entidade` ENUM(''empresa'',''profissional'',''ambos'') NOT NULL DEFAULT ''empresa'' AFTER `id_contrato`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
    @has_new_table = 1,
    'UPDATE `wpjy_parceiros_tipos` SET `entidade` = ''empresa'' WHERE COALESCE(`entidade`, '''') = ''''',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 3) reorganiza os indices da tabela de tipos para o escopo novo
SET @has_idx_old_unique = (
    SELECT COUNT(*)
      FROM information_schema.statistics
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
       AND index_name = 'uq_tipo_contrato_slug'
);

SET @sql = IF(
    @has_new_table = 1 AND @has_idx_old_unique > 0,
    'ALTER TABLE `wpjy_parceiros_tipos` DROP INDEX `uq_tipo_contrato_slug`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @has_idx_old_lookup = (
    SELECT COUNT(*)
      FROM information_schema.statistics
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
       AND index_name = 'idx_tipo_contrato_ativo'
);

SET @sql = IF(
    @has_new_table = 1 AND @has_idx_old_lookup > 0,
    'ALTER TABLE `wpjy_parceiros_tipos` DROP INDEX `idx_tipo_contrato_ativo`',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @has_idx_new_unique = (
    SELECT COUNT(*)
      FROM information_schema.statistics
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
       AND index_name = 'uq_parceiros_tipos_contrato_entidade_slug'
);

SET @sql = IF(
    @has_new_table = 1 AND @has_idx_new_unique = 0,
    'ALTER TABLE `wpjy_parceiros_tipos`
        ADD UNIQUE KEY `uq_parceiros_tipos_contrato_entidade_slug` (`id_contrato`,`entidade`,`slug`)',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @has_idx_new_lookup = (
    SELECT COUNT(*)
      FROM information_schema.statistics
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_tipos'
       AND index_name = 'idx_parceiros_tipos_lookup'
);

SET @sql = IF(
    @has_new_table = 1 AND @has_idx_new_lookup = 0,
    'ALTER TABLE `wpjy_parceiros_tipos`
        ADD KEY `idx_parceiros_tipos_lookup` (`id_contrato`,`entidade`,`ativo`,`ordem`,`nome`)',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 4) renomeia a coluna de vinculo da empresa
SET @has_old_tipo_col = (
    SELECT COUNT(*)
      FROM information_schema.columns
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_empresas'
       AND column_name = 'estabelecimento_tipo_id'
);
SET @has_new_tipo_col = (
    SELECT COUNT(*)
      FROM information_schema.columns
     WHERE table_schema = @schema_name
       AND table_name = 'wpjy_parceiros_empresas'
       AND column_name = 'tipo_id'
);

SET @sql = IF(
    @has_old_tipo_col = 1 AND @has_new_tipo_col = 0,
    'ALTER TABLE `wpjy_parceiros_empresas`
        CHANGE COLUMN `estabelecimento_tipo_id` `tipo_id` INT DEFAULT NULL',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SHOW COLUMNS FROM `wpjy_parceiros_empresas` LIKE 'tipo_id';
SHOW TABLES LIKE 'wpjy_parceiros_tipos';
SHOW INDEX FROM `wpjy_parceiros_tipos`;
