USE whp;

-- Defensive: migrate any 'local' type rows before removing 'local' from ENUM
-- Expected to affect zero rows based on system design, but guards against
-- partial installations or manual data entry.
UPDATE backup_targets SET type = 's3' WHERE type = 'local';

-- Expand type enum to include ftp, sftp (replace unused 'local')
ALTER TABLE backup_targets MODIFY COLUMN type ENUM('s3', 'ftp', 'sftp') NOT NULL DEFAULT 's3';

-- Add columns for FTP/SFTP credentials
ALTER TABLE backup_targets ADD COLUMN IF NOT EXISTS host VARCHAR(255) DEFAULT NULL AFTER type;
ALTER TABLE backup_targets ADD COLUMN IF NOT EXISTS port INT DEFAULT NULL AFTER host;
ALTER TABLE backup_targets ADD COLUMN IF NOT EXISTS username VARCHAR(100) DEFAULT NULL AFTER port;
ALTER TABLE backup_targets ADD COLUMN IF NOT EXISTS password VARCHAR(255) DEFAULT NULL AFTER username;
ALTER TABLE backup_targets ADD COLUMN IF NOT EXISTS remote_path VARCHAR(255) DEFAULT '/' AFTER password;

-- Make S3-specific fields nullable (not needed for FTP/SFTP)
ALTER TABLE backup_targets MODIFY COLUMN endpoint VARCHAR(255) DEFAULT NULL;
ALTER TABLE backup_targets MODIFY COLUMN bucket VARCHAR(100) DEFAULT NULL;
ALTER TABLE backup_targets MODIFY COLUMN access_key VARCHAR(100) DEFAULT NULL;
ALTER TABLE backup_targets MODIFY COLUMN secret_key VARCHAR(255) DEFAULT NULL;
