-- ============================================================================
-- SET ADMIN PERMISSIONS
-- ============================================================================
-- This file contains SQL queries to manually set full permissions for admin users
-- 
-- IMPORTANT: The backend controller (PermissionController) handles both:
--   - staff_id: When passing a staff table ID
--   - user_id: When passing a users table ID
-- 
-- It automatically finds the correct user account via the staff.email = users.email relationship
--
-- ============================================================================
-- RECOMMENDED METHOD: Use the artisan command
-- ============================================================================
-- Run: php artisan permissions:set-admin
-- This command will automatically:
-- 1. Find all users with role = 'admin'
-- 2. Delete their existing permissions
-- 3. Create full permissions for all 9 modules
-- 4. Show a summary table
--
-- ============================================================================
-- MANUAL SQL METHOD (if needed)
-- ============================================================================

-- Step 1: Delete existing permissions for admin users
DELETE FROM staff_permissions 
WHERE user_id IN (SELECT id FROM users WHERE role = 'admin');

-- Step 2: Insert full permissions for all admin users for all modules
INSERT INTO staff_permissions (user_id, module_id, can_view, can_create, can_edit, can_delete, created_at, updated_at)
SELECT 
    u.id AS user_id,
    m.id AS module_id,
    1 AS can_view,
    1 AS can_create,
    1 AS can_edit,
    1 AS can_delete,
    NOW() AS created_at,
    NOW() AS updated_at
FROM users u
CROSS JOIN modules m
WHERE u.role = 'admin';

-- Step 3: Verify the results
SELECT 
    u.name AS admin_name,
    u.email,
    COUNT(sp.id) AS permission_count,
    SUM(sp.can_view) AS view_count,
    SUM(sp.can_create) AS create_count,
    SUM(sp.can_edit) AS edit_count,
    SUM(sp.can_delete) AS delete_count
FROM users u
LEFT JOIN staff_permissions sp ON u.id = sp.user_id
WHERE u.role = 'admin'
GROUP BY u.id, u.name, u.email;

-- ============================================================================
-- MODULES REFERENCE
-- ============================================================================
-- The following modules are created by default:
-- 1. dashboard          - Dashboard Stats
-- 2. orders             - Orders Management
-- 3. menu               - Menu Management
-- 4. staff              - Staff Management
-- 5. attendance         - Attendance
-- 6. analytics          - Analytics
-- 7. restaurant_settings - Restaurant Setting
-- 8. preferences        - Preferences
-- 9. my_detail          - My Detail
-- ============================================================================

