import { UserProfileDto } from '../types';

/**
 * Entidade de Domínio: Colaborador
 */
export class Colaborador {
  private constructor(
    public readonly matricula: number,
    public readonly userName: string,
    public readonly nome: string,
    public readonly codigoFilial: string,
    public readonly nomeFilial: string,
    public readonly codigoRCA: number,
    private readonly _percentualDesconto: number,
    public readonly codigoSetor: number,
    public readonly codigoSupervisor: number
  ) {}

  // ========== FACTORY METHODS ==========

  static criarAPartirDoDto(dto: UserProfileDto): Colaborador {
    return new Colaborador(
      dto.matricula,
      dto.userName,
      dto.nome,
      dto.codigoFilial,
      dto.nomeFilial,
      dto.rca,
      dto.discountPercent,
      dto.sectorId,
      dto.supervisorId
    );
  }

  // ========== COMPUTED PROPERTIES ==========

  get iniciais(): string {
    if (!this.nome) return 'U';
    const partes = this.nome.trim().split(' ').filter(Boolean);
    if (partes.length === 1) {
      return partes[0][0].toUpperCase();
    }
    return (partes[0][0] + partes[partes.length - 1][0]).toUpperCase();
  }

  get ehRepresentanteComercial(): boolean {
    return this.codigoRCA > 0;
  }

  get podeAplicarDesconto(): boolean {
    return this._percentualDesconto > 0 && this.ehRepresentanteComercial;
  }

  get percentualDesconto(): number {
    return this._percentualDesconto;
  }

  get nomeComFilial(): string {
    return `${this.nome} - ${this.nomeFilial}`;
  }

  // ========== BUSINESS METHODS ==========

  aplicarDescontoAoValor(valorOriginal: number): number {
    if (!this.podeAplicarDesconto) {
      return valorOriginal;
    }
    return valorOriginal * (1 - this._percentualDesconto / 100);
  }
}

