// Main Response Structure
export interface CreditReportResponse {
  data: {
    report_status_information: ReportStatusInformation;
    credit_score_history: CreditScoreHistory[];
    credit_score_factors: CreditScoreFactors;
  };
}

// Report Status Information
export interface ReportStatusInformation {
  bureau_statuses: {
    Removed: number;
    Deleted: number;
    New: number;
    Reinsertion: number;
    Existing: number;
  };
  tcp_statuses: {
    waiting_for_attestation: number;
    not_processed: number;
    to_proceed: number;
  };
  latest_report_by_bureau: {
    Equifax: null | string;
    Transunion: null | string;
    Experian: null | string;
  };
}

// Credit Score History
export interface CreditScoreHistory {
  bureau: 'Equifax' | 'TransUnion' | 'Experian';
  history: CreditScoreEntry[];
}

export interface CreditScoreEntry {
  id: number;
  date: string;
  value: number;
  change: number | null;
  change_direction: 'no_change' | 'up' | 'down' | 'stable';
  model_name: string | null;
  model_desc: string;
  report_id: number;
}

// Credit Score Factors
export interface CreditScoreFactors {
  id: number;
  internal_client_key: number;
  client_key: string;
  report_id: number;
  vendor: string;
  bureau: string;
  report_date: string;
  modified_date: string;
  content: CreditResponseContent;
}

// Credit Response Content
export interface CreditResponseContent {
  CREDIT_RESPONSE: CreditResponse;
}

export interface CreditResponse {
  BORROWER: Borrower;
  CREDIT_BUREAU: string;
  CREDIT_FILE: CreditFile;
  CREDIT_FROZEN_STATUS: CreditFrozenStatus;
  CREDIT_INQUIRY: CreditInquiry[];
  CREDIT_LIABILITY: CreditLiability[];
  CREDIT_PUBLIC_RECORD: any[];
  CREDIT_REPOSITORY_INCLUDED: CreditRepositoryIncluded;
  CREDIT_REQUEST_DATA: CreditRequestData;
  CREDIT_SCORE: CreditScore[];
  CREDIT_SUMMARY: CreditSummary;
  CREDIT_SUMMARY_TUI: any;
  CreditRatingCodeType: string;
  CreditReportFirstIssuedDate: string;
  CreditReportIdentifier: string;
  CreditReportMergeTypeIndicator: null;
  CreditResponseID: string;
  MISMOVersionID: null;
  Internal_Client_Key: number;
  Client_key: string;
  Report_ID: number;
  Report_Type: null;
  Vendor: string;
}

// Borrower
export interface Borrower {
  BirthDate: string;
  BorrowerID: null | string;
  FirstName: string;
  LastName: string;
  PrintPositionType: null;
  RESIDENCE: Residence[];
  SSN: string;
  UnparsedName: null;
}

export interface Residence {
  BorrowerResidencyType: 'CURRENT' | 'PREVIOUS';
  City: string;
  PostalCode: string;
  State: string;
  StreetAddress: string;
  BorrowerID?: null | string;
  BorrowerResidencyDurationMonths?: number;
  BorrowerResidencyDurationYears?: string;
  DateReported?: string;
}

// Credit File
export interface CreditFile {
  ALERT_MESSAGE: any[];
  BORROWER: CreditFileBorrower;
  BorrowerID: null | string;
  CreditFileID: string;
  CreditRepositorySourceType: string;
  CreditScoreID: null;
  InfileDate: string;
  ResultStatusType: null;
  VARIATION: {
    Type: null;
  };
}

export interface CreditFileBorrower {
  ALIAS: any[];
  BirthDate: string;
  EMPLOYER: any[];
  FirstName: string;
  LastName: string;
  RESIDENCE: Residence[];
  SSN: string;
  UnparsedName: null;
}

// Credit Frozen Status
export interface CreditFrozenStatus {
  EquifaxIndicator: null;
  ExperianIndicator: null;
  TransUnionIndicator: null;
}

// Credit Inquiry
export interface CreditInquiry {
  BorrowerID: null;
  City: null;
  CreditBusinessType: null;
  CreditFileID: null;
  CreditInquiryID: null;
  CreditRepository: {
    SourceType: string;
    SubscriberCode: null;
  };
  Date: string;
  InquiryHashComplex: null;
  InquiryHashSimple: null;
  Name: string;
  Phone: null;
  PostalCode: null;
  PurposeType: 'SOFT' | 'HARD' | 'OTHER';
  RawIndustryCode: string;
  RawIndustryText: null;
  State: null;
  StreetAddress: null;
}

// Credit Liability
export interface CreditLiability {
  AccountClosedDate: string | null;
  AccountIdentifier: string;
  AccountOpenedDate: string;
  AccountOwnershipType: 'INDIVIDUAL' | 'JOINT_CONTRACTUAL_LIABILITY' | 'TERMINATED';
  AccountPaidDate: null;
  AccountReportedDate: string;
  AccountStatusType: 'Open' | 'Closed';
  AccountType: 'Revolving' | 'Installment' | 'Open';
  ArrayAccountIdentifier: string;
  BorrowerID: string;
  ConsumerDisputeIndicator: boolean;
  CreditBusinessType: string;
  CreditComment: CreditComment[];
  CreditFileID: null;
  CreditLiabilityID: null;
  CreditLimitAmount: number | null;
  CreditLoanType: string;
  CreditLoanTypeCode: null;
  CreditTradeReferenceID: null;
  Creditor: Creditor;
  CurrentRating: {
    Code: null;
    Type: null;
  };
  DerogatoryDataIndicator: boolean;
  HighBalanceAmount: null;
  HighCreditAmount: number | null;
  HighestAdverseRating: {
    Code: string | null;
    Type: string | null;
  };
  IsChargeoffIndicator: boolean;
  IsClosedIndicator: null;
  IsCollectionIndicator: boolean;
  IsMortgageIndicator: null;
  LastActivityDate: string | null;
  LastPaymentDate: null;
  LateCount: {
    Days30: number;
    Days60: number;
    Days90: number;
  };
  MonthlyPaymentAmount: number;
  MonthsReviewedCount: number;
  PastDueAmount: number | null;
  PaymentPattern: PaymentPattern[];
  RawAccountStatus: null;
  RawAccountType: null;
  RawIndustryCode: string;
  RawIndustryText: string;
  SecuredLoanIndicator: null;
  TUI_Handle: null;
  TermsDescription: string | null;
  TermsSourceType: null;
  TradelineHashComplex: null;
  TradelineHashSimple: null;
  UnpaidBalanceAmount: number;
  TermsMonthsCount: number | null;
  OriginalBalanceAmount: number | null;
  CreditBalance: number;
  ChargeOffDate: null;
  CollectionDate: null;
  MostrecentAdverserating_Code: string | null;
  MostrecentAdverserating_Date: string | null;
}

export interface CreditComment {
  Code: string;
  SourceType: null;
  Type: null;
  Text: string;
  TypeOtherDescription: null;
}

export interface Creditor {
  City: string | null;
  ContactDetail: {
    Type: null;
    Value: null;
  };
  Name: string;
  Phone: string | null;
  PostalCode: string | null;
  State: string | null;
  StreetAddress: string | null;
}

export interface PaymentPattern {
  Data: string;
  StartDate: string;
}

// Credit Repository Included
export interface CreditRepositoryIncluded {
  EquifaxIndicator: null;
  ExperianIndicator: null;
  TransUnionIndicator: null;
}

// Credit Request Data
export interface CreditRequestData {
  BorrowerID: string;
  CREDIT_REPOSITORY_INCLUDED: CreditRepositoryIncluded;
}

// Credit Score
export interface CreditScore {
  BorrowerID: null;
  CreditFileID: null;
  CreditReportIdentifier: null;
  CreditRepositorySourceType: string;
  CreditScoreID: null;
  RiskBasedPricingMax: null;
  RiskBasedPricingMin: null;
  RiskBasedPricingPercent: null;
  Date: string;
  ModelNameType: null;
  ModelNameTypeOtherDescription: string;
  Value: number;
  InquiriesAffectedScore: null;
  FACTOR: CreditScoreFactor[];
}

export interface CreditScoreFactor {
  Code: string;
  Text: string;
  Factor_Type: 'Negative' | 'Positive' | 'Unknown';
}

// Credit Summary
export interface CreditSummary {
  BorrowerID: null;
  DATA_SET: DataSetItem[];
  Name: null;
}

export interface DataSetItem {
  ID: string | null;
  Name: string;
  Value: string;
  Type: 'creditSummary' | 'creditCardSummary' | 'autoLoanSummary' | 'miscellaneousLiabilitySummary' | 'personalLoanSummary';
}

// Helper Types for Parsed Data
export interface ParsedCreditScores {
  transunion: number | null;
  equifax: number | null;
  experian: number | null;
}

export interface ParsedCreditFactors {
  paymentHistory: {
    percentage: number;
    rating: 'Excellent' | 'Good' | 'Fair' | 'Poor';
    impact: 'High' | 'Medium' | 'Low';
    description: string;
  };
  creditCardUse: {
    percentage: number;
    rating: 'Excellent' | 'Good' | 'Fair' | 'Poor';
    impact: 'High' | 'Medium' | 'Low';
    description: string;
  };
  derogatoryMarks: {
    count: number;
    rating: 'Excellent' | 'Good' | 'Fair' | 'Poor';
    impact: 'High' | 'Medium' | 'Low';
    description: string;
  };
  creditAge: {
    years: number;
    rating: 'Excellent' | 'Good' | 'Fair' | 'Poor';
    impact: 'High' | 'Medium' | 'Low';
    description: string;
  };
  totalAccounts: {
    count: number;
    rating: 'Excellent' | 'Good' | 'Fair' | 'Poor';
    impact: 'High' | 'Medium' | 'Low';
    description: string;
  };
  hardInquiries: {
    count: number;
    rating: 'Excellent' | 'Good' | 'Fair' | 'Poor';
    impact: 'High' | 'Medium' | 'Low';
    description: string;
  };
}