import type { IValueObject } from '@core/shared/domain';
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';

/**
 * Admin Trust Reason Code Value Object
 * 
 * Stable machine codes for admin trust rating events to support:
 * - Filtering and analytics
 * - i18n translations
 * - Consistent UI explanations
 * 
 * Based on ratings-architecture-concept.md sections 5.2.1 and 5.2.2
 */

export type AdminTrustReasonCodeValue = 
  // Vote outcomes
  | 'ADMIN_VOTE_OUTCOME_POSITIVE'
  | 'ADMIN_VOTE_OUTCOME_NEGATIVE'
  // System signals
  | 'ADMIN_ACTION_SLA_BONUS'
  | 'ADMIN_ACTION_REVERSAL_PENALTY'
  | 'ADMIN_ACTION_RULE_CLARITY_BONUS'
  | 'ADMIN_ACTION_ABUSE_REPORT_PENALTY';

export interface AdminTrustReasonCodeProps {
  value: AdminTrustReasonCodeValue;
}

const VALID_REASON_CODES: AdminTrustReasonCodeValue[] = [
  // Vote outcomes
  'ADMIN_VOTE_OUTCOME_POSITIVE',
  'ADMIN_VOTE_OUTCOME_NEGATIVE',
  // System signals
  'ADMIN_ACTION_SLA_BONUS',
  'ADMIN_ACTION_REVERSAL_PENALTY',
  'ADMIN_ACTION_RULE_CLARITY_BONUS',
  'ADMIN_ACTION_ABUSE_REPORT_PENALTY',
];

export class AdminTrustReasonCode implements IValueObject<AdminTrustReasonCodeProps> {
  readonly value: AdminTrustReasonCodeValue;

  private constructor(value: AdminTrustReasonCodeValue) {
    this.value = value;
  }

  static create(value: string): AdminTrustReasonCode {
    if (!value || value.trim().length === 0) {
      throw new IdentityDomainValidationError('AdminTrustReasonCode cannot be empty');
    }

    const trimmed = value.trim() as AdminTrustReasonCodeValue;
    
    if (!VALID_REASON_CODES.includes(trimmed)) {
      throw new IdentityDomainValidationError(
        `Invalid admin trust reason code: ${value}. Valid options: ${VALID_REASON_CODES.join(', ')}`
      );
    }

    return new AdminTrustReasonCode(trimmed);
  }

  static fromValue(value: AdminTrustReasonCodeValue): AdminTrustReasonCode {
    return new AdminTrustReasonCode(value);
  }

  get props(): AdminTrustReasonCodeProps {
    return { value: this.value };
  }

  equals(other: IValueObject<AdminTrustReasonCodeProps>): boolean {
    return this.value === other.props.value;
  }

  toString(): string {
    return this.value;
  }

  /**
   * Check if this is a vote-related reason code
   */
  isVoteOutcome(): boolean {
    return this.value === 'ADMIN_VOTE_OUTCOME_POSITIVE' ||
           this.value === 'ADMIN_VOTE_OUTCOME_NEGATIVE';
  }

  /**
   * Check if this is a system signal reason code
   */
  isSystemSignal(): boolean {
    return this.value === 'ADMIN_ACTION_SLA_BONUS' ||
           this.value === 'ADMIN_ACTION_REVERSAL_PENALTY' ||
           this.value === 'ADMIN_ACTION_RULE_CLARITY_BONUS' ||
           this.value === 'ADMIN_ACTION_ABUSE_REPORT_PENALTY';
  }

  /**
   * Check if this is a positive impact (bonus)
   */
  isPositive(): boolean {
    return this.value.endsWith('_BONUS') || 
           this.value === 'ADMIN_VOTE_OUTCOME_POSITIVE';
  }

  /**
   * Check if this is a negative impact (penalty)
   */
  isNegative(): boolean {
    return this.value.endsWith('_PENALTY') ||
           this.value === 'ADMIN_VOTE_OUTCOME_NEGATIVE';
  }
}
