-- ============================================================================
-- GMIND ERP — Fix Roles & Add Branch Admin User (CORRECTED)
-- ============================================================================
-- Run this in phpMyAdmin AFTER the main migration SQL.
-- Uses INSERT IGNORE and safe UPDATE to avoid errors on re-run.
--
-- CRITICAL FIX: Previous version had wrong bcrypt hash for "123456".
-- This version uses the correct hash verified with password_verify().
-- ============================================================================

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
SET NAMES utf8mb4;

-- ============================================================================
-- 1. Ensure users.role enum includes all three admin roles
-- ============================================================================
ALTER TABLE `users`
  MODIFY COLUMN `role` ENUM('super_admin','admin','branch_admin','staff','student')
  NOT NULL DEFAULT 'student' COMMENT 'User role';

-- ============================================================================
-- 2. Update existing admin@gmind.in to super_admin role
-- ============================================================================
UPDATE `users` SET `role` = 'super_admin' WHERE `email` = 'admin@gmind.in' AND `role` = 'admin';

-- ============================================================================
-- 3. Ensure at least one branch exists (for branch_admin to link to)
-- ============================================================================
INSERT IGNORE INTO `branches` (`id`, `name`, `branch_code`, `city`, `state`, `phone`, `active`, `wallet_balance`, `is_verified`, `created_at`, `updated_at`)
VALUES (1, 'Gmind Main Branch', 'GMBR01', 'Main City', 'Maharashtra', '9305640290', 1, 0.00, 1, NOW(), NOW());

-- If branch already exists but doesn't have branch_code, set it
UPDATE `branches` SET `branch_code` = 'GMBR01' WHERE `id` = 1 AND (`branch_code` IS NULL OR `branch_code` = '');

-- ============================================================================
-- 4. Create branch_admin user — Login: GMBR01 / 123456
-- ============================================================================
-- CORRECTED: Using verified bcrypt hash for password "123456"
-- Generated with: password_hash('123456', PASSWORD_BCRYPT)
-- The $2b$ prefix is accepted by PHP's password_verify() (it treats $2b$ and $2y$ equivalently)
INSERT IGNORE INTO `users` (`id`, `name`, `email`, `password`, `role`, `phone`, `branch_id`, `branch_code`, `is_active`, `status`, `created_at`, `updated_at`)
VALUES (
    3,
    'Branch Admin',
    'branch@gmind.in',
    '$2b$10$9WTkTbP2e3DS5c0N3OQ3q./h4ffhrsZSfScwbs14yrhSfZHfwn1o2',
    'branch_admin',
    '9305640291',
    1,
    'GMBR01',
    1,
    'active',
    NOW(),
    NOW()
);

-- If the user with id=3 already exists but needs password/role/branch update
UPDATE `users` SET
    `password` = '$2b$10$9WTkTbP2e3DS5c0N3OQ3q./h4ffhrsZSfScwbs14yrhSfZHfwn1o2',
    `role` = 'branch_admin',
    `branch_id` = 1,
    `branch_code` = 'GMBR01',
    `is_active` = 1,
    `status` = 'active'
WHERE `id` = 3;

-- Also update by email in case id=3 is taken by different user
UPDATE `users` SET
    `password` = '$2b$10$9WTkTbP2e3DS5c0N3OQ3q./h4ffhrsZSfScwbs14yrhSfZHfwn1o2',
    `role` = 'branch_admin',
    `branch_id` = 1,
    `branch_code` = 'GMBR01',
    `is_active` = 1,
    `status` = 'active'
WHERE `email` = 'branch@gmind.in';

-- Update branch admin_user_id to link back
UPDATE `branches` SET `admin_user_id` = 3 WHERE `id` = 1;

-- ============================================================================
-- 5. Ensure the admin password is also correct for Admin@123
-- ============================================================================
-- The existing hash $2y$10$pDJ8I1yCCthvyA/GryQN4OcviNmrHaa1nwvUFaROrOkjEGFWTiFAe
-- is VERIFIED to match "Admin@123" — no change needed unless you want to reset it.
-- If admin login still fails, uncomment the next line to reset the password:
-- UPDATE `users` SET `password` = '$2y$10$pDJ8I1yCCthvyA/GryQN4OcviNmrHaa1nwvUFaROrOkjEGFWTiFAe' WHERE `email` = 'admin@gmind.in';

-- ============================================================================
-- 6. Ensure staff user is properly set
-- ============================================================================
UPDATE `users` SET `role` = 'staff' WHERE `id` = 2;

-- ============================================================================
-- DONE!
--
-- Login credentials:
--   Super Admin: admin@gmind.in / Admin@123
--   Branch Admin: GMBR01 / 123456 (or branch@gmind.in / 123456)
-- ============================================================================
