-- ============================================================================
-- PHASE 4 PATCH — Admit Card Login Credentials
-- For: Gmind Institute — Branch Admin panel
-- ============================================================================
-- Adds login_username + login_password columns to admit_cards table so the
-- admit card can display the student's portal login credentials.
--
-- The password stored in admit_cards.login_password is plain text (for
-- printing on the admit card). When the branch admin generates the admit
-- card, they specify a username + password — the password is hashed and
-- saved to users.password (so login works), AND saved as plain text to
-- admit_cards.login_password (so it can be printed).
--
-- COMPATIBILITY: MySQL 5.7+ / MySQL 8.x / MariaDB 10.x (all versions)
-- SAFE TO RE-RUN: Uses INFORMATION_SCHEMA checks (not "IF NOT EXISTS").
-- ============================================================================

-- 1. admit_cards.login_username — username shown on the admit card
--    (usually the student's enrollment_no, but branch admin can override)
SET @c := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='admit_cards' AND COLUMN_NAME='login_username');
SET @s := IF(@c=0, 'ALTER TABLE `admit_cards` ADD COLUMN `login_username` VARCHAR(100) NULL DEFAULT NULL AFTER `card_number`', 'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- 2. admit_cards.login_password — plain-text password for printing on the card
--    (also hashed into users.password for actual authentication)
SET @c := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='admit_cards' AND COLUMN_NAME='login_password');
SET @s := IF(@c=0, 'ALTER TABLE `admit_cards` ADD COLUMN `login_password` VARCHAR(100) NULL DEFAULT NULL AFTER `login_username`', 'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ============================================================================
-- END OF PATCH — verify with:
--   SHOW COLUMNS FROM admit_cards LIKE 'login_%';
-- ============================================================================
