CREATE TABLE IF NOT EXISTS `used_battery_rates` (
  `id`         int(10) unsigned NOT NULL AUTO_INCREMENT,
  `rate_per_amp` int(10) unsigned NOT NULL COMMENT 'Price per ampere for used batteries in Rials',
  `is_current` tinyint(1) NOT NULL DEFAULT 0 COMMENT '1 = active rate',
  `created_by` int(10) unsigned DEFAULT NULL COMMENT 'Admin user id who set the rate',
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `idx_is_current` (`is_current`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Seed initial rate from the settings table value (6000 as default)
INSERT IGNORE INTO `used_battery_rates` (`rate_per_amp`, `is_current`, `created_at`)
SELECT CAST(`value` AS UNSIGNED), 1, NOW()
FROM `settings`
WHERE `setting_group` = 'used_battery' AND `setting_key` = 'rate_per_amp'
LIMIT 1;
