-- Drop the existing token-related indexes if they exist
DROP INDEX IF EXISTS idx_users_token;

-- Drop the existing token column if it exists
ALTER TABLE users DROP COLUMN IF EXISTS token;

-- Add the token column with proper constraints
ALTER TABLE users ADD COLUMN token VARCHAR(255);

-- Add index for faster token lookups
CREATE INDEX idx_users_token ON users(token);

-- Update the user_details view to include the token
DROP VIEW IF EXISTS user_details;
CREATE VIEW user_details AS
SELECT 
    u.*,
    r.role_id,
    r.role_name,
    r.role_slug
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id AND ur.is_primary = true
LEFT JOIN roles r ON ur.role_id = r.role_id;
