-- ============================================================================
-- Gmind Institute - Fix Admin CRUD Issues
-- ============================================================================
-- Run this SQL on your database (advaysin_gmind) via phpMyAdmin or MySQL CLI
-- This fixes:
-- 1. Gallery items with wrong active status
-- 2. Image paths that might have BASE_PATH prefix in database
-- 3. Branches table address column allowing empty string
-- 4. Missing image path corrections
-- ============================================================================

-- Fix gallery items that might have been deactivated by the old toggle bug
-- The old code toggled active status on every update, so some items may be inactive
UPDATE `gallery` SET `active` = 1 WHERE `active` = 0;

-- Fix image paths that may have BASE_PATH prefix stored in database
-- (e.g. '/gmind/uploads/sliders/img_xxx.jpg' → 'uploads/sliders/img_xxx.jpg')
UPDATE `sliders` SET `image` = REPLACE(`image`, '/gmind/', '') WHERE `image` LIKE '/gmind/%';
UPDATE `courses` SET `image` = REPLACE(`image`, '/gmind/', '') WHERE `image` LIKE '/gmind/%';
UPDATE `branches` SET `image` = REPLACE(`image`, '/gmind/', '') WHERE `image` LIKE '/gmind/%';
UPDATE `gallery` SET `image` = REPLACE(`image`, '/gmind/', '') WHERE `image` LIKE '/gmind/%';
UPDATE `affiliations` SET `logo` = REPLACE(`logo`, '/gmind/', '') WHERE `logo` LIKE '/gmind/%';

-- Fix image paths that start with just '/' (absolute web path without BASE_PATH)
UPDATE `sliders` SET `image` = SUBSTRING(`image`, 2) WHERE `image` LIKE '/uploads/%';
UPDATE `courses` SET `image` = SUBSTRING(`image`, 2) WHERE `image` LIKE '/uploads/%';
UPDATE `branches` SET `image` = SUBSTRING(`image`, 2) WHERE `image` LIKE '/uploads/%';
UPDATE `gallery` SET `image` = SUBSTRING(`image`, 2) WHERE `image` LIKE '/uploads/%';
UPDATE `affiliations` SET `logo` = SUBSTRING(`logo`, 2) WHERE `logo` LIKE '/uploads/%';

-- Fix branches with empty address (NOT NULL constraint)
UPDATE `branches` SET `address` = CONCAT(`city`, ', ', `state`) WHERE `address` = '' OR `address` IS NULL;

-- Fix courses with empty duration (NOT NULL constraint)
UPDATE `courses` SET `duration` = 'N/A' WHERE `duration` = '' OR `duration` IS NULL;

-- ============================================================================
-- End of Fix
-- ============================================================================
