import { createHash } from 'crypto';

/**
 * Utilitário para calcular hashes de provider JWT
 * baseado nos modelos padrão do Laravel
 */
export class ProviderHashUtil {
  private static readonly USER_MODEL = 'App\\Models\\User';
  private static readonly CLIENT_MODEL = 'App\\Models\\User\\Client';

  private static userModelHash: string | null = null;
  private static clientModelHash: string | null = null;

  /**
   * Calcula o hash SHA1 de um modelo Laravel
   */
  private static calculateModelHash(modelName: string): string {
    return createHash('sha1').update(modelName).digest('hex');
  }

  /**
   * Inicializa os hashes baseado nos modelos padrão do Laravel
   */
  static initialize(): void {
    this.userModelHash = this.calculateModelHash(this.USER_MODEL);
    this.clientModelHash = this.calculateModelHash(this.CLIENT_MODEL);

    console.log('🔐 Provider hashes inicializados:');
    console.log(`  User Model: ${this.USER_MODEL} -> ${this.userModelHash}`);
    console.log(
      `  Client Model: ${this.CLIENT_MODEL} -> ${this.clientModelHash}`,
    );
  }

  /**
   * Determina o tipo de usuário baseado no hash do provider
   */
  static determineUserType(providerHash: string): 'user' | 'client' | null {
    if (this.userModelHash && providerHash === this.userModelHash) {
      return 'user';
    }

    if (this.clientModelHash && providerHash === this.clientModelHash) {
      return 'client';
    }

    return null;
  }

  /**
   * Retorna os hashes configurados para debug
   */
  static getConfiguredHashes(): { user: string | null; client: string | null } {
    return {
      user: this.userModelHash,
      client: this.clientModelHash,
    };
  }
}
