-- Add operator authentication fields to madrasah table
ALTER TABLE madrasah ADD COLUMN IF NOT EXISTS operator_token VARCHAR(100) UNIQUE;
ALTER TABLE madrasah ADD COLUMN IF NOT EXISTS operator_password TEXT;
ALTER TABLE madrasah ADD COLUMN IF NOT EXISTS operator_registered BOOLEAN DEFAULT FALSE;

-- Generate random tokens for existing madrasah
UPDATE madrasah 
SET operator_token = UPPER(SUBSTRING(MD5(RANDOM()::TEXT || nsm) FROM 1 FOR 8))
WHERE operator_token IS NULL;
