-- =====================================================
-- MIGRATION SCRIPT v2: Compatible Version
-- For MySQL 5.7+ / MariaDB 10.2+
-- =====================================================

-- =====================================================
-- STEP 1: Add phone column to staff_users
-- =====================================================

-- Check and add phone column (run this separately if error)
SET @dbname = DATABASE();
SET @tablename = 'staff_users';
SET @columnname = 'phone';
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 phone already exists in staff_users"',
  'ALTER TABLE staff_users ADD COLUMN phone VARCHAR(20) AFTER role'
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- =====================================================
-- STEP 2: Update attendance status ENUM
-- =====================================================

ALTER TABLE attendance 
MODIFY COLUMN status ENUM('PRESENT', 'LATE', 'REGISTRATION', 'ALFA', 'SAKIT', 'IZIN') DEFAULT 'PRESENT';

-- =====================================================
-- STEP 3: Create leave_requests table
-- =====================================================

CREATE TABLE IF NOT EXISTS leave_requests (
    id VARCHAR(36) NOT NULL,
    student_id VARCHAR(36) NOT NULL,
    student_name VARCHAR(255) NOT NULL,
    student_nis VARCHAR(50) DEFAULT NULL,
    student_class VARCHAR(100) DEFAULT NULL,
    request_type ENUM('SAKIT', 'IZIN') NOT NULL,
    request_date VARCHAR(20) NOT NULL,
    reason TEXT NOT NULL,
    photo_evidence LONGTEXT,
    status ENUM('PENDING', 'APPROVED', 'REJECTED') DEFAULT 'PENDING',
    reviewed_by VARCHAR(36) DEFAULT NULL,
    reviewed_by_name VARCHAR(255) DEFAULT NULL,
    reviewed_at TIMESTAMP NULL DEFAULT NULL,
    rejection_reason TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    KEY idx_student (student_id),
    KEY idx_status (status),
    KEY idx_date (request_date),
    KEY idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================
-- STEP 4: Ensure Fonnte Token setting exists
-- =====================================================

INSERT INTO settings (setting_key, setting_value) 
SELECT 'FONNTE_TOKEN', '' 
WHERE NOT EXISTS (
    SELECT 1 FROM settings WHERE setting_key = 'FONNTE_TOKEN'
);

-- =====================================================
-- VERIFICATION - Run these to confirm success
-- =====================================================

SELECT 'Checking staff_users phone column...' AS step;
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
  AND TABLE_NAME = 'staff_users' 
  AND COLUMN_NAME = 'phone';

SELECT 'Checking leave_requests table...' AS step;
SELECT COUNT(*) as table_exists FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'leave_requests';

SELECT 'Checking attendance status enum...' AS step;
SHOW COLUMNS FROM attendance LIKE 'status';

SELECT 'Migration completed successfully!' AS result;
