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

const demoUser = (role: AdminRole): AuthUser => ({
  id: `demo-${role}`,
  name: role === 'admin' ? '演示管理员' : role === 'operator' ? '演示运营' : '演示访客',
  email: 'demo@mosquito.local',
  role
})

export class DemoAuthAdapter implements AuthAdapter {
  private currentUser: AuthUser | null = demoUser('admin')

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

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

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

  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)
  }
}
