-- =====================================================
-- Add Pay Mode and Bank Mode to Employees Table
-- =====================================================
-- This migration adds pay_mode and bank_mode columns to employees table
-- Run this file in phpMyAdmin or MySQL client
-- =====================================================

-- Step 1: Add pay_mode column if it doesn't exist
-- Pay Mode options: Cash, Bank Transfer, Cheque
SET @dbname = DATABASE();
SET @tablename = 'employees';
SET @columnname = 'pay_mode';

SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @columnname)
  ) > 0,
  'SELECT "Column pay_mode already exists" AS message;',
  CONCAT('ALTER TABLE `', @tablename, '` ADD COLUMN `', @columnname, '` varchar(50) DEFAULT NULL AFTER `bank_ifsc`;')
));

PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Step 2: Add bank_mode column if it doesn't exist
-- Bank Mode options: NEFT, RTGS, IMPS, UPI
SET @columnname = 'bank_mode';

SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @columnname)
  ) > 0,
  'SELECT "Column bank_mode already exists" AS message;',
  CONCAT('ALTER TABLE `', @tablename, '` ADD COLUMN `', @columnname, '` varchar(50) DEFAULT "NEFT" AFTER `pay_mode`;')
));

PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Step 3: Verify columns were added
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    COLUMN_DEFAULT,
    IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE 
    TABLE_SCHEMA = @dbname
    AND TABLE_NAME = @tablename
    AND COLUMN_NAME IN ('pay_mode', 'bank_mode')
ORDER BY ORDINAL_POSITION;

-- Migration Complete
SELECT 'Pay Mode and Bank Mode migration completed successfully!' AS status;

