-- ============================================================
-- DOWN Migration: Rollback 57_unified_brand_rates_migration.sql
-- WARNING: Any rates created after migration WILL BE LOST.
-- Run verification queries first to ensure no important new data exists.
-- ============================================================

-- Drop new objects first
DROP TRIGGER IF EXISTS `trg_bar_duplicate_guard`;
DROP VIEW IF EXISTS `v_brand_ampere_rate_history`;
DROP VIEW IF EXISTS `v_partner_ampere_summary`;

-- Drop new indexes
ALTER TABLE `brand_ampere_rates` DROP INDEX IF EXISTS `idx_bar_lookup`;
ALTER TABLE `brand_ampere_rates` DROP INDEX IF EXISTS `idx_bar_history`;
ALTER TABLE `brand_ampere_rates` DROP INDEX IF EXISTS `idx_bar_product`;

-- Drop new FK
ALTER TABLE `brand_ampere_rates` DROP FOREIGN KEY IF EXISTS `fk_bar_product`;

-- Drop new columns (reverse order of addition)
ALTER TABLE `brand_ampere_rates`
  DROP COLUMN IF EXISTS `version`,
  DROP COLUMN IF EXISTS `product_id`,
  DROP COLUMN IF EXISTS `rate_type`;

-- Restore amperage_id to NOT NULL
ALTER TABLE `brand_ampere_rates`
  MODIFY COLUMN `amperage_id` INT(10) UNSIGNED NOT NULL;

-- NOTE: Restore original generated column and unique index from your schema backup.
-- The original generated column _active_brand_uk and index uq_single_active_rate
-- must be restored manually from a pre-migration backup.
