-- ============================================================
-- Gmind Institute — Login Fix: Seed Users & Role Updates
-- ============================================================
-- Run this SQL in phpMyAdmin after uploading the PHP files.
--
-- This SQL does the following:
-- 1. Updates the users table role enum to support new roles
-- 2. Updates admin@gmind.in to role 'admin' (website frontage)
-- 3. Ensures superadmin@gmind.in exists as 'super_admin' (branches, wallet, ERP)
-- 4. Creates a branch_admin user linked to branch GMBR01
-- 5. Links the branch_admin to the branches table via admin_user_id
--
-- Login credentials after running this:
--   Admin:       admin@gmind.in / Admin@123
--   Super Admin: superadmin@gmind.in / Admin@123
--   Branch:      GMBR01 / 123456
-- ============================================================

-- Step 1: Update the role enum to include all new roles
ALTER TABLE `users` MODIFY COLUMN `role` ENUM('super_admin','admin','branch_admin','staff','student') NOT NULL DEFAULT 'student';

-- Step 2: Update existing admin user to 'admin' role (controls website frontage)
UPDATE `users` SET `role` = 'admin' WHERE `email` = 'admin@gmind.in' AND `role` = 'admin';

-- Step 3: Create super_admin user if not exists (controls branches, wallet, ERP)
-- Password: Admin@123 (same hash as existing admin)
INSERT IGNORE INTO `users` (`id`, `name`, `email`, `password`, `role`, `phone`, `photo`, `is_active`, `created_at`, `updated_at`)
VALUES (
    3,
    'Super Admin',
    'superadmin@gmind.in',
    '$2y$10$pDJ8I1yCCthvyA/GryQN4OcviNmrHaa1nwvUFaROrOkjEGFWTiFAe',
    'super_admin',
    '9305640290',
    NULL,
    1,
    NOW(),
    NOW()
);

-- Step 4: Create branch_admin user linked to GMBR01 branch
-- Password: 123456 (hashed)
INSERT IGNORE INTO `users` (`id`, `name`, `email`, `password`, `role`, `phone`, `photo`, `is_active`, `branch_id`, `branch_code`, `created_at`, `updated_at`)
VALUES (
    4,
    'Gorakhpur Branch Admin',
    'gorakhpur@gmind.in',
    '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi',
    'branch_admin',
    '9305640291',
    NULL,
    1,
    (SELECT `id` FROM (SELECT `id` FROM `branches` WHERE `branch_code` = 'GMBR01' LIMIT 1) AS tmp),
    'GMBR01',
    NOW(),
    NOW()
);

-- Step 5: Update branches table to link admin_user_id for GMBR01
UPDATE `branches` SET `admin_user_id` = (
    SELECT `id` FROM (SELECT `id` FROM `users` WHERE `role` = 'branch_admin' AND `branch_code` = 'GMBR01' LIMIT 1) AS tmp
) WHERE `branch_code` = 'GMBR01';

-- Step 6: Also update the branch_admin's branch_id directly (in case subquery didn't work in INSERT)
UPDATE `users` SET `branch_id` = (SELECT `id` FROM `branches` WHERE `branch_code` = 'GMBR01' LIMIT 1)
WHERE `role` = 'branch_admin' AND `branch_code` = 'GMBR01';

-- ============================================================
-- Verification queries (run these to check results):
-- ============================================================
-- SELECT id, name, email, role, branch_id, branch_code FROM users;
-- SELECT id, name, branch_code, admin_user_id FROM branches WHERE branch_code = 'GMBR01';
