import { v4 as uuidv4 } from 'uuid';
import { AdminRole, ROLE_PERMISSIONS } from '../value-objects/admin-role.enum';

export interface AdminProps {
  id: string;
  username: string;
  passwordHash: string;
  name: string;
  email?: string;
  phone?: string;
  role: AdminRole;
  permissions: string[];
  avatar?: string;
  lastLoginAt?: Date;
  lastLoginIp?: string;
  isActive: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export class AdminEntity {
  private constructor(private props: AdminProps) {}

  // Getters
  get id(): string { return this.props.id; }
  get username(): string { return this.props.username; }
  get passwordHash(): string { return this.props.passwordHash; }
  get name(): string { return this.props.name; }
  get email(): string | undefined { return this.props.email; }
  get phone(): string | undefined { return this.props.phone; }
  get role(): AdminRole { return this.props.role; }
  get permissions(): string[] { return this.props.permissions; }
  get avatar(): string | undefined { return this.props.avatar; }
  get lastLoginAt(): Date | undefined { return this.props.lastLoginAt; }
  get lastLoginIp(): string | undefined { return this.props.lastLoginIp; }
  get isActive(): boolean { return this.props.isActive; }
  get createdAt(): Date { return this.props.createdAt; }
  get updatedAt(): Date { return this.props.updatedAt; }

  // Setters
  set name(value: string) { this.props.name = value; this.props.updatedAt = new Date(); }
  set email(value: string | undefined) { this.props.email = value; this.props.updatedAt = new Date(); }
  set phone(value: string | undefined) { this.props.phone = value; this.props.updatedAt = new Date(); }
  set isActive(value: boolean) { this.props.isActive = value; this.props.updatedAt = new Date(); }
  set passwordHash(value: string) { this.props.passwordHash = value; this.props.updatedAt = new Date(); }

  /**
   * 创建新管理员
   */
  static create(params: {
    username: string;
    passwordHash: string;
    name: string;
    email?: string;
    phone?: string;
    role: AdminRole;
  }): AdminEntity {
    const now = new Date();
    return new AdminEntity({
      id: uuidv4(),
      username: params.username,
      passwordHash: params.passwordHash,
      name: params.name,
      email: params.email,
      phone: params.phone,
      role: params.role,
      permissions: ROLE_PERMISSIONS[params.role],
      isActive: true,
      createdAt: now,
      updatedAt: now,
    });
  }

  /**
   * 从持久化数据恢复实体
   */
  static fromPersistence(props: AdminProps): AdminEntity {
    return new AdminEntity(props);
  }

  /**
   * 更新角色
   */
  updateRole(role: AdminRole): void {
    this.props.role = role;
    this.props.permissions = ROLE_PERMISSIONS[role];
    this.props.updatedAt = new Date();
  }

  /**
   * 记录登录
   */
  recordLogin(ip?: string): void {
    this.props.lastLoginAt = new Date();
    this.props.lastLoginIp = ip;
    this.props.updatedAt = new Date();
  }

  /**
   * 获取完整权限列表
   */
  getPermissions(customPermissions?: string[]): string[] {
    const rolePermissions = ROLE_PERMISSIONS[this.props.role] || [];
    if (customPermissions && customPermissions.length > 0) {
      return [...new Set([...rolePermissions, ...customPermissions])];
    }
    return rolePermissions;
  }

  /**
   * 检查是否有指定权限
   */
  hasPermission(requiredPermission: string): boolean {
    // 超管拥有所有权限
    if (this.props.permissions.includes('*')) {
      return true;
    }

    // 完全匹配
    if (this.props.permissions.includes(requiredPermission)) {
      return true;
    }

    // 通配符匹配 (如 knowledge:* 匹配 knowledge:read)
    const [resource] = requiredPermission.split(':');
    if (this.props.permissions.includes(`${resource}:*`)) {
      return true;
    }

    return false;
  }
}
