import { type WebAcceptanceClientContext } from 'src/utils/webClientContext';

export type CollaboratorStatusResponse = {
  offer: boolean;
  plan: boolean;
  payments: readonly unknown[] | null;
  hasFinishedPlans: boolean;
  phase: string;
};

export type FinishedLoanStatus = 'PAID' | 'CANCELLED';

export type Periodicity = 'weekly' | 'biweekly';

export type FinishedLoan = {
  id: string;
  amount: number;
  weeks: number;
  closeDate: string;
  status: FinishedLoanStatus;
  periodicity: Periodicity;
};

export type LoanOfferPlanOption = {
  weeks: number;
  totalMultiplier: number;
};
export type AcceptanceField = 'bankAccountNumber';

export function isAcceptanceField(value: string): value is AcceptanceField {
  return value === 'bankAccountNumber';
}

export type LoanOffer = {
  id: string;
  preApprovedMaxAmountMxn: number;
  minAmountMxn: number;
  currency: 'MXN';

  availability: 'available' | 'paused' | 'no_offer';
  planOptions: readonly LoanOfferPlanOption[];

  maxInstallmentAmountMxn?: number;

  tnaAnnualPercent?: number;

  tnaAnnualDecimal?: number;

  validUntil?: string;

  requiredAcceptanceFields: AcceptanceField[];

  phoneNumber?: string | null;

  periodicity: Periodicity;
};

export type LoanPaymentPlanOption = {
  weeks: number;
  weeklyPaymentMxn: number;
  totalPaymentMxn: number;
};

export type LoanPaymentPlan = {
  amountMxn: number;
  options: readonly LoanPaymentPlanOption[];
};

export type CurrencyCode = 'USD' | 'MXN' | 'ARS';

export type CollaboratorOfferResponse = {
  id: string;
  status: 'PENDING' | 'OFFERED' | 'ACCEPTED' | 'EXPIRED' | 'CANCELLED';
  currency: CurrencyCode;
  tna: number;
  cat?: number;
  maxTerm: number;
  maxAmount: number;
  minAmount: number;
  maxInstallmentAmount: number;
  offeredAt?: string;
  expiresAt?: string;
  requiredAcceptanceFields: string[];
  phoneNumber?: string | null;
  periodicity?: Periodicity;
};

export type SimulateRequest = {
  totalAmount: number;
  installmentsChosen: number;
};

export type SimulateResponse = {
  totalAmount: number;
  installmentsChosen: number;
  amountPerCycle: number;
  totalToRepay: number;
  cost: number;
  weeklyRate?: number;
  cat?: number;
  cea?: number;
  tna?: number;
  installments?: CollaboratorLoanPlanInstallment[];
};

export type AcceptOfferRequest = {
  totalAmount: number;
  installmentsChosen: number;
  phoneNumber?: string;
  loanReason?: string;
  bankAccountNumber?: string;
  bankName?: string;
  loanReasonComment?: string;
  deviceMetadata?: WebAcceptanceClientContext;
};

export type CollaboratorLoanPlanInstallment = {
  installmentNumber?: number;
  sequence?: number;
  dueDate?: string;

  amount?: number;

  dueAmount?: number;
  status?: string;
};

export type CollaboratorPaymentItem = CollaboratorLoanPlanInstallment;

export type CollaboratorLoanPlanNested = {
  startDate?: string;
  endDate?: string;
};

export type CollaboratorLoanPlanResponse = {
  id: string;

  loanOfferId?: string;
  status: 'PENDING' | 'ACTIVE' | 'PAUSED' | 'COMPLETED' | 'CANCELLED';
  installmentsChosen?: number;
  totalAmount?: number;
  amountPerCycle?: number;
  totalToRepay?: number;
  cost?: number;
  acceptedAt?: string;

  startDate?: string;
  endDate?: string;
  loanPlan?: CollaboratorLoanPlanNested;
  contractLink?: string;
  installments?: CollaboratorLoanPlanInstallment[];

  creditedAt?: string;
  disbursedAt?: string;

  paidInstallments?: number;
  paidInstallmentsCount?: number;
  overdueInstallmentsCount?: number;
  overdueDays?: number;
  nextPaymentDueDate?: string;

  outstandingBalance?: number;

  cat?: number;
  tna?: number;

  periodicity?: Periodicity;
};

export type AcceptOfferResponse = {
  id: string;
  loanOfferId: string;
  status: 'PENDING' | 'ACTIVE' | 'PAUSED' | 'COMPLETED' | 'CANCELLED';
  installmentsChosen: number;
  totalAmount: number;
  amountPerCycle: number;
  totalToRepay: number;
  cost: number;
  acceptedAt: string;
  startDate: string;
  endDate: string;
  contractLink?: string;
};
