SET @has_is_system_col := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'loan_proposal_status_catalog'
      AND column_name = 'is_system'
);

SET @add_is_system_sql := IF(
    @has_is_system_col > 0,
    'DO 0',
    'ALTER TABLE loan_proposal_status_catalog ADD COLUMN is_system TINYINT(1) NOT NULL DEFAULT 0 AFTER is_positive'
);

PREPARE alter_status_catalog_stmt FROM @add_is_system_sql;
EXECUTE alter_status_catalog_stmt;
DEALLOCATE PREPARE alter_status_catalog_stmt;

SET @old_slug := 'draft';
SET @new_slug := 'digitado';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'internal_review';
SET @new_slug := 'ag_assinatura';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'pending_docs';
SET @new_slug := 'pendente_documento';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'submitted';
SET @new_slug := 'enviada';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'bank_review';
SET @new_slug := 'analise_banco';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'approved';
SET @new_slug := 'ag_saldo_devedor';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'funded';
SET @new_slug := 'enviado_quitacao';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'paid';
SET @new_slug := 'saldo_quitado';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'rejected';
SET @new_slug := 'reprovada';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

SET @old_slug := 'cancelled';
SET @new_slug := 'cancelada';
SET @old_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @old_slug);
SET @new_exists := (SELECT COUNT(*) FROM loan_proposal_status_catalog WHERE slug = @new_slug);

SET @rename_sql := IF(
    @old_exists > 0 AND @new_exists = 0,
    CONCAT('UPDATE loan_proposal_status_catalog SET slug = ''', @new_slug, ''' WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE rename_stmt FROM @rename_sql;
EXECUTE rename_stmt;
DEALLOCATE PREPARE rename_stmt;

SET @merge_proposals_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposals SET status = ''', @new_slug, ''' WHERE status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_proposals_stmt FROM @merge_proposals_sql;
EXECUTE merge_proposals_stmt;
DEALLOCATE PREPARE merge_proposals_stmt;

SET @merge_history_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET old_status = ''', @new_slug, ''' WHERE old_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_old_stmt FROM @merge_history_old_sql;
EXECUTE merge_history_old_stmt;
DEALLOCATE PREPARE merge_history_old_stmt;

SET @merge_history_new_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('UPDATE loan_proposal_status_history SET new_status = ''', @new_slug, ''' WHERE new_status = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE merge_history_new_stmt FROM @merge_history_new_sql;
EXECUTE merge_history_new_stmt;
DEALLOCATE PREPARE merge_history_new_stmt;

SET @delete_old_sql := IF(
    @old_exists > 0 AND @new_exists > 0,
    CONCAT('DELETE FROM loan_proposal_status_catalog WHERE slug = ''', @old_slug, ''''),
    'DO 0'
);
PREPARE delete_old_stmt FROM @delete_old_sql;
EXECUTE delete_old_stmt;
DEALLOCATE PREPARE delete_old_stmt;

INSERT INTO loan_proposal_status_catalog (slug, label, pipeline_group, sort_order, is_final, is_positive, is_system)
VALUES
    ('digitado', 'Digitado', 'preparation', 10, 0, 0, 1),
    ('ag_assinatura', 'Ag. assinatura', 'preparation', 20, 0, 0, 1),
    ('pendente_documento', 'Pend. documento', 'preparation', 30, 0, 0, 1),
    ('enviada', 'Enviada', 'bank_submission', 40, 0, 0, 1),
    ('analise_banco', 'Analise banco', 'bank_process', 50, 0, 0, 1),
    ('ag_saldo_devedor', 'Ag. saldo devedor', 'results', 60, 1, 1, 1),
    ('enviado_quitacao', 'Enviado quitacao', 'results', 70, 1, 1, 1),
    ('saldo_quitado', 'Saldo quitado', 'after_sales', 80, 1, 1, 1),
    ('reprovada', 'Reprovada', 'results', 90, 1, 0, 1),
    ('cancelada', 'Cancelada', 'results', 100, 1, 0, 1)
ON DUPLICATE KEY UPDATE
    label = VALUES(label),
    pipeline_group = VALUES(pipeline_group),
    sort_order = VALUES(sort_order),
    is_final = VALUES(is_final),
    is_positive = VALUES(is_positive),
    is_system = VALUES(is_system);

ALTER TABLE loan_proposals
    MODIFY COLUMN status VARCHAR(50) NOT NULL DEFAULT 'digitado';

