-- Phase 3: GameForge Integration Migration
-- Auto-provision communication channels for GameForge projects
-- Migration: 20260110130000

-- GameForge project integrations
CREATE TABLE IF NOT EXISTS gameforge_integrations (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  project_id VARCHAR(255) UNIQUE NOT NULL,  -- GameForge project ID
  domain VARCHAR(255) UNIQUE NOT NULL,       -- e.g., hideandseek@forge.aethex.dev
  auto_provision_channels BOOLEAN DEFAULT true,
  channel_config JSONB DEFAULT '{}',         -- Default channel configuration
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

-- Add GameForge project reference to conversations
ALTER TABLE conversations
ADD COLUMN IF NOT EXISTS gameforge_project_id VARCHAR(255) REFERENCES gameforge_integrations(project_id) ON DELETE CASCADE;

-- Add metadata column to conversations if not exists (for channel permissions)
ALTER TABLE conversations
ADD COLUMN IF NOT EXISTS metadata JSONB DEFAULT '{}';

-- Add is_archived column to conversations if not exists
ALTER TABLE conversations
ADD COLUMN IF NOT EXISTS is_archived BOOLEAN DEFAULT false;

-- Audit log for GameForge operations
CREATE TABLE IF NOT EXISTS audit_logs (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID,
  action VARCHAR(100) NOT NULL,
  resource_type VARCHAR(100) NOT NULL,
  resource_id VARCHAR(255),
  metadata JSONB DEFAULT '{}',
  created_at TIMESTAMP DEFAULT NOW()
);

-- Ensure resource_type column exists for index creation
ALTER TABLE audit_logs ADD COLUMN IF NOT EXISTS resource_type VARCHAR;
ALTER TABLE audit_logs ADD COLUMN IF NOT EXISTS resource_id VARCHAR;

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_gameforge_integrations_project_id ON gameforge_integrations(project_id);
CREATE INDEX IF NOT EXISTS idx_conversations_gameforge_project ON conversations(gameforge_project_id) WHERE gameforge_project_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_conversations_archived ON conversations(is_archived);
CREATE INDEX IF NOT EXISTS idx_audit_logs_user_id ON audit_logs(user_id);
CREATE INDEX IF NOT EXISTS idx_audit_logs_created_at ON audit_logs(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_audit_logs_resource ON audit_logs(resource_type, resource_id);

-- Trigger to update updated_at timestamp on gameforge_integrations
CREATE OR REPLACE FUNCTION update_gameforge_integration_timestamp()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER gameforge_integration_updated
BEFORE UPDATE ON gameforge_integrations
FOR EACH ROW
EXECUTE FUNCTION update_gameforge_integration_timestamp();

-- Comments
COMMENT ON TABLE gameforge_integrations IS 'Links GameForge projects to AeThex Connect communication infrastructure';
COMMENT ON TABLE audit_logs IS 'Audit trail for GameForge integration operations';
COMMENT ON COLUMN conversations.gameforge_project_id IS 'Links conversation to GameForge project for auto-provisioned channels';
COMMENT ON COLUMN conversations.metadata IS 'Stores channel-specific data like permissions, channel type, etc.';
COMMENT ON COLUMN conversations.is_archived IS 'Soft delete for archived projects';
