-- Add user_id column to ticket_comments table
ALTER TABLE ticket_comments
  ADD COLUMN IF NOT EXISTS user_id INT;

-- Add foreign key constraint if it doesn't exist
SET @fk_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                  WHERE CONSTRAINT_NAME = 'fk_comments_user_id'
                  AND TABLE_NAME = 'ticket_comments'
                  AND TABLE_SCHEMA = DATABASE());

SET @sql = IF(@fk_exists = 0,
  'ALTER TABLE ticket_comments ADD CONSTRAINT fk_comments_user_id FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE SET NULL',
  'SELECT "Foreign key fk_comments_user_id already exists"');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Update existing comments to reference jared user (first admin)
-- This will be done after jared user is created via web login
