/**
 * Admin Onboarding System
 * Defines types, constants, and utilities for the admin onboarding workflow
 */

export type AdminRole = 'super_admin' | 'admin' | 'moderator' | 'finance_team' | 'developer' | 'support_lead';

export type OnboardingStep = 
  | 'welcome'
  | 'role_assignment'
  | 'permission_training'
  | 'security_training'
  | 'device_registration'
  | 'completion';

export interface OnboardingProgress {
  adminId: number;
  currentStep: OnboardingStep;
  completedSteps: OnboardingStep[];
  roleAssigned: AdminRole | null;
  permissionsAcknowledged: boolean;
  securityTrainingCompleted: boolean;
  deviceRegistered: boolean;
  startedAt: Date;
  completedAt: Date | null;
  ipAddress: string;
  deviceInfo: {
    userAgent: string;
    browser: string;
    os: string;
  };
}

export interface RoleTrainingModule {
  role: AdminRole;
  title: string;
  description: string;
  permissions: PermissionGroup[];
  responsibilities: string[];
  restrictions: string[];
  estimatedTime: number; // in minutes
}

export interface PermissionGroup {
  category: string;
  permissions: Permission[];
}

export interface Permission {
  id: string;
  name: string;
  description: string;
  riskLevel: 'low' | 'medium' | 'high';
  examples: string[];
}

export interface SecurityTrainingModule {
  id: string;
  title: string;
  description: string;
  topics: SecurityTopic[];
  estimatedTime: number; // in minutes
  quizzes: SecurityQuiz[];
}

export interface SecurityTopic {
  id: string;
  title: string;
  content: string;
  keyPoints: string[];
  examples: string[];
}

export interface SecurityQuiz {
  id: string;
  question: string;
  options: string[];
  correctAnswer: number;
  explanation: string;
}

export interface DeviceRegistration {
  deviceId: string;
  adminId: number;
  deviceName: string;
  browser: string;
  os: string;
  ipAddress: string;
  registeredAt: Date;
  lastUsedAt: Date;
  isActive: boolean;
}

// Role Training Modules
export const ROLE_TRAINING_MODULES: Record<AdminRole, RoleTrainingModule> = {
  super_admin: {
    role: 'super_admin',
    title: 'Super Administrator',
    description: 'Full system access with all permissions and responsibilities',
    permissions: [
      {
        category: 'System Management',
        permissions: [
          {
            id: 'system_config',
            name: 'System Configuration',
            description: 'Modify system settings, deploy updates, manage infrastructure',
            riskLevel: 'high',
            examples: ['Deploy new features', 'Configure system parameters', 'Manage database']
          },
          {
            id: 'user_management',
            name: 'User Management',
            description: 'Create, delete, and modify admin accounts and permissions',
            riskLevel: 'high',
            examples: ['Add new admins', 'Assign roles', 'Revoke access']
          },
          {
            id: 'audit_logs',
            name: 'Audit Logs',
            description: 'View and export all system audit logs',
            riskLevel: 'medium',
            examples: ['View activity logs', 'Export compliance reports', 'Investigate incidents']
          }
        ]
      },
      {
        category: 'Game Management',
        permissions: [
          {
            id: 'game_approval',
            name: 'Game Approval',
            description: 'Approve or reject new games and updates',
            riskLevel: 'high',
            examples: ['Approve new slot games', 'Reject non-compliant games']
          }
        ]
      }
    ],
    responsibilities: [
      'Oversee all admin activities and ensure compliance',
      'Manage system infrastructure and deployments',
      'Make critical business decisions',
      'Maintain security and audit trails'
    ],
    restrictions: ['None - full system access'],
    estimatedTime: 45
  },
  admin: {
    role: 'admin',
    title: 'Administrator',
    description: 'Full access to most admin sections except system settings',
    permissions: [
      {
        category: 'Game Management',
        permissions: [
          {
            id: 'game_approval',
            name: 'Game Approval',
            description: 'Approve or reject games',
            riskLevel: 'high',
            examples: ['Review game submissions', 'Approve compliant games']
          },
          {
            id: 'game_management',
            name: 'Game Management',
            description: 'Create and edit game configurations',
            riskLevel: 'medium',
            examples: ['Configure game parameters', 'Set payout rates']
          }
        ]
      },
      {
        category: 'User Management',
        permissions: [
          {
            id: 'user_review',
            name: 'User Review',
            description: 'View and manage user accounts',
            riskLevel: 'medium',
            examples: ['Ban users', 'Review KYC submissions']
          }
        ]
      }
    ],
    responsibilities: [
      'Review and approve game submissions',
      'Manage user accounts and compliance',
      'Process payments and refunds',
      'Monitor system health'
    ],
    restrictions: ['Cannot modify system settings', 'Cannot manage other admins'],
    estimatedTime: 30
  },
  moderator: {
    role: 'moderator',
    title: 'Moderator',
    description: 'User and content moderation access',
    permissions: [
      {
        category: 'User Moderation',
        permissions: [
          {
            id: 'user_ban',
            name: 'User Ban',
            description: 'Ban or suspend users for violations',
            riskLevel: 'medium',
            examples: ['Ban fraudulent users', 'Suspend rule violators']
          },
          {
            id: 'fraud_review',
            name: 'Fraud Review',
            description: 'Investigate and report fraud cases',
            riskLevel: 'medium',
            examples: ['Review fraud alerts', 'Investigate suspicious activity']
          }
        ]
      }
    ],
    responsibilities: [
      'Monitor user activity for violations',
      'Investigate fraud and suspicious behavior',
      'Enforce community guidelines',
      'Document incidents'
    ],
    restrictions: [
      'Cannot approve games',
      'Cannot process payments',
      'Cannot modify system settings'
    ],
    estimatedTime: 20
  },
  finance_team: {
    role: 'finance_team',
    title: 'Finance Team',
    description: 'Payment and financial management access',
    permissions: [
      {
        category: 'Payment Management',
        permissions: [
          {
            id: 'payment_process',
            name: 'Payment Processing',
            description: 'Process user payments and withdrawals',
            riskLevel: 'high',
            examples: ['Process cashout requests', 'Issue refunds']
          },
          {
            id: 'kyc_review',
            name: 'KYC Review',
            description: 'Review and approve KYC submissions',
            riskLevel: 'medium',
            examples: ['Verify identity documents', 'Approve KYC']
          }
        ]
      }
    ],
    responsibilities: [
      'Process financial transactions',
      'Verify user identities (KYC)',
      'Monitor financial compliance',
      'Generate financial reports'
    ],
    restrictions: [
      'Cannot approve games',
      'Cannot ban users',
      'Cannot access system settings'
    ],
    estimatedTime: 25
  },
  developer: {
    role: 'developer',
    title: 'Developer',
    description: 'Game development and deployment access',
    permissions: [
      {
        category: 'Game Development',
        permissions: [
          {
            id: 'game_create',
            name: 'Game Creation',
            description: 'Create and configure new games',
            riskLevel: 'medium',
            examples: ['Create slot games', 'Configure game mechanics']
          },
          {
            id: 'game_deploy',
            name: 'Game Deployment',
            description: 'Deploy games to production',
            riskLevel: 'high',
            examples: ['Deploy game updates', 'Roll out new features']
          }
        ]
      }
    ],
    responsibilities: [
      'Develop and test new games',
      'Deploy game updates',
      'Monitor game performance',
      'Fix game-related issues'
    ],
    restrictions: [
      'Cannot approve games',
      'Cannot manage users',
      'Cannot process payments'
    ],
    estimatedTime: 25
  },
  support_lead: {
    role: 'support_lead',
    title: 'Support Lead',
    description: 'Customer support and communication management',
    permissions: [
      {
        category: 'Customer Support',
        permissions: [
          {
            id: 'campaign_create',
            name: 'Campaign Creation',
            description: 'Create and launch email campaigns',
            riskLevel: 'low',
            examples: ['Send promotional emails', 'Notify users of updates']
          },
          {
            id: 'user_support',
            name: 'User Support',
            description: 'Assist users with account issues',
            riskLevel: 'low',
            examples: ['Reset passwords', 'Resolve account issues']
          }
        ]
      }
    ],
    responsibilities: [
      'Manage customer communications',
      'Launch campaigns and promotions',
      'Assist users with issues',
      'Gather user feedback'
    ],
    restrictions: [
      'Cannot approve games',
      'Cannot process payments',
      'Cannot ban users'
    ],
    estimatedTime: 15
  }
};

// Security Training Content
export const SECURITY_TRAINING: SecurityTrainingModule = {
  id: 'admin_security_101',
  title: 'Admin Security Training',
  description: 'Essential security practices for admin accounts',
  topics: [
    {
      id: 'password_security',
      title: 'Password Security',
      content: 'Learn how to create and maintain strong passwords, use password managers, and recognize phishing attempts.',
      keyPoints: [
        'Use unique, complex passwords (16+ characters)',
        'Enable 2FA on all accounts',
        'Never share credentials',
        'Use password managers'
      ],
      examples: [
        'Good: Tr0p!cal$unset#2024@Admin',
        'Bad: admin123 or password'
      ]
    },
    {
      id: 'phishing_awareness',
      title: 'Phishing & Social Engineering',
      content: 'Recognize and avoid phishing attacks and social engineering tactics.',
      keyPoints: [
        'Verify sender email addresses',
        'Check for suspicious links',
        'Never click attachments from unknown senders',
        'Report suspicious emails'
      ],
      examples: [
        'Phishing: "Urgent: Verify your admin account at admin-playcoinkrazy.com"',
        'Legitimate: Official emails from @playcoinkrazy.com domain'
      ]
    },
    {
      id: 'data_protection',
      title: 'Data Protection & Privacy',
      content: 'Understand how to protect sensitive data and maintain user privacy.',
      keyPoints: [
        'Never share user data externally',
        'Use VPN on public networks',
        'Lock your device when away',
        'Clear browser history regularly'
      ],
      examples: [
        'Protect: User KYC documents, payment information',
        'Avoid: Sharing data via email or messaging apps'
      ]
    },
    {
      id: 'incident_response',
      title: 'Incident Response',
      content: 'Know how to respond to security incidents and report them immediately.',
      keyPoints: [
        'Report suspicious activity immediately',
        'Do not investigate on your own',
        'Preserve evidence',
        'Follow incident response procedures'
      ],
      examples: [
        'Report: Unauthorized access attempts, data breaches',
        'Contact: Security team immediately'
      ]
    }
  ],
  estimatedTime: 30,
  quizzes: [
    {
      id: 'quiz_1',
      question: 'What should you do if you receive a suspicious email asking to verify your admin credentials?',
      options: [
        'Click the link and verify immediately',
        'Reply to the email with your credentials',
        'Report it to the security team and do not click any links',
        'Forward it to other admins'
      ],
      correctAnswer: 2,
      explanation: 'Never click links or provide credentials in response to suspicious emails. Always report to the security team.'
    },
    {
      id: 'quiz_2',
      question: 'What is the minimum recommended password length for admin accounts?',
      options: [
        '8 characters',
        '12 characters',
        '16 characters',
        '20 characters'
      ],
      correctAnswer: 2,
      explanation: 'Admin accounts should use passwords of at least 16 characters with mixed case, numbers, and symbols.'
    },
    {
      id: 'quiz_3',
      question: 'What should you do if you accidentally expose sensitive user data?',
      options: [
        'Delete it and say nothing',
        'Report it to the security team immediately',
        'Ask a colleague what to do',
        'Wait to see if anyone notices'
      ],
      correctAnswer: 1,
      explanation: 'Immediately report any data exposure to the security team so they can take corrective action.'
    }
  ]
};

// Onboarding Steps Configuration
export const ONBOARDING_STEPS: Record<OnboardingStep, {
  title: string;
  description: string;
  estimatedTime: number;
}> = {
  welcome: {
    title: 'Welcome to CoinKrazy Admin',
    description: 'Get started with your admin account setup',
    estimatedTime: 5
  },
  role_assignment: {
    title: 'Role Assignment',
    description: 'Understand your role and permissions',
    estimatedTime: 30
  },
  permission_training: {
    title: 'Permission Training',
    description: 'Learn about your specific permissions',
    estimatedTime: 20
  },
  security_training: {
    title: 'Security Training',
    description: 'Complete essential security training',
    estimatedTime: 30
  },
  device_registration: {
    title: 'Device Registration',
    description: 'Register your device and IP address',
    estimatedTime: 10
  },
  completion: {
    title: 'Onboarding Complete',
    description: 'You are ready to start managing',
    estimatedTime: 5
  }
};
