import type { AdminRole, Permission } from '../roles'
import { RolePermissions, RoleLabels } from '../roles'
import type { AuthAdapter, AuthUser, LoginResult } from '../types'

// 角色名称映射
const roleNameMap: Record<AdminRole, string> = {
  'super_admin': '演示超级管理员',
  'system_admin': '演示系统管理员',
  'operation_manager': '演示运营经理',
  'operation_member': '演示运营成员',
  'marketing_manager': '演示市场经理',
  'marketing_member': '演示市场成员',
  'finance_manager': '演示财务经理',
  'finance_member': '演示财务成员',
  'risk_manager': '演示风控经理',
  'risk_member': '演示风控成员',
  'customer_service': '演示客服',
  'auditor': '演示审计员',
  'viewer': '演示访客'
}

const demoUser = (role: AdminRole): AuthUser => ({
  id: `demo-${role}`,
  name: roleNameMap[role] || '演示用户',
  email: 'demo@mosquito.local',
  role
})

export class DemoAuthAdapter implements AuthAdapter {
  private currentUser: AuthUser = demoUser('super_admin')

  async loginWithPassword(_username: string, _password: string): Promise<LoginResult> {
    return { user: demoUser('super_admin') }
  }

  async loginDemo(role: AdminRole = 'super_admin'): Promise<LoginResult> {
    this.currentUser = demoUser(role)
    return { user: this.currentUser }
  }

  async logout(): Promise<void> {
    this.currentUser = null as any
  }

  async switchRole(role: AdminRole): Promise<AuthUser> {
    this.currentUser = demoUser(role)
    return this.currentUser
  }

  async getCurrentUser(): Promise<AuthUser | null> {
    return this.currentUser
  }

  hasPermission(role: AdminRole, permission: Permission): boolean {
    return RolePermissions[role]?.includes(permission) ?? false
  }
}
