-- Production fix for "Field 'permission_id' doesn't have a default value" error
-- Run this in cPanel's phpMyAdmin or MySQL command line

-- First, check if the tables exist and their structure
-- You can run these queries to see the current state:

-- Check if users_user_permissions exists
-- SHOW TABLES LIKE 'users_user_permissions';

-- Check current structure
-- DESCRIBE users_user_permissions;

-- Check if users_groups exists  
-- SHOW TABLES LIKE 'users_groups';

-- Check current structure
-- DESCRIBE users_groups;

-- ===========================================
-- FIX USERS_USER_PERMISSIONS TABLE
-- ===========================================

-- Drop the table if it exists (this will remove any existing data)
-- WARNING: This will delete any existing user permission data!
DROP TABLE IF EXISTS users_user_permissions;

-- Create the table with correct structure
CREATE TABLE users_user_permissions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customuser_id CHAR(36) NOT NULL,
    permission_id INT NOT NULL,
    UNIQUE KEY users_user_permissions_customuser_id_permission_id_uniq (customuser_id, permission_id),
    KEY users_user_permissions_customuser_id (customuser_id),
    KEY users_user_permissions_permission_id (permission_id),
    CONSTRAINT users_user_permissions_customuser_id_fk 
        FOREIGN KEY (customuser_id) REFERENCES users (id) ON DELETE CASCADE,
    CONSTRAINT users_user_permissions_permission_id_fk 
        FOREIGN KEY (permission_id) REFERENCES auth_permission (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ===========================================
-- FIX USERS_GROUPS TABLE
-- ===========================================

-- Drop the table if it exists (this will remove any existing data)
-- WARNING: This will delete any existing user group data!
DROP TABLE IF EXISTS users_groups;

-- Create the table with correct structure
CREATE TABLE users_groups (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customuser_id CHAR(36) NOT NULL,
    group_id INT NOT NULL,
    UNIQUE KEY users_groups_customuser_id_group_id_uniq (customuser_id, group_id),
    KEY users_groups_customuser_id (customuser_id),
    KEY users_groups_group_id (group_id),
    CONSTRAINT users_groups_customuser_id_fk 
        FOREIGN KEY (customuser_id) REFERENCES users (id) ON DELETE CASCADE,
    CONSTRAINT users_groups_group_id_fk 
        FOREIGN KEY (group_id) REFERENCES auth_group (id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ===========================================
-- VERIFY THE FIX
-- ===========================================

-- Check the structure of the created tables
DESCRIBE users_user_permissions;
DESCRIBE users_groups;

-- Check that the tables exist
SHOW TABLES LIKE 'users%';

-- Test insert (replace with actual user ID and permission ID from your database)
-- INSERT INTO users_user_permissions (customuser_id, permission_id) 
-- VALUES ('your-user-id-here', 1);

-- Clean up test record
-- DELETE FROM users_user_permissions WHERE customuser_id = 'your-user-id-here' AND permission_id = 1;
