import { logEvent } from 'src/config/logging';
import { EventName } from 'src/types/amplitude';

import { type LoanPaymentPlanOption } from './types';
import { resolvePlanPaymentTotals } from './utils/formatters';

export type LoanPlanAnalyticsPayload = {
  amount: number;
  plan_weeks: number;
  plan_weekly_payment: number;
  plan_total: number;
};

export function buildLoanPlanAnalyticsPayload(
  amount: number,
  planWeeks: number,
  simulatedOption?: Pick<
    LoanPaymentPlanOption,
    'weeks' | 'weeklyPaymentMxn' | 'totalPaymentMxn'
  > | null,
): LoanPlanAnalyticsPayload {
  const { weekly, total } = resolvePlanPaymentTotals(
    amount,
    planWeeks,
    simulatedOption,
  );
  return {
    amount,
    plan_weeks: planWeeks,
    plan_weekly_payment: weekly,
    plan_total: total,
  };
}

export function trackLoanStep1Viewed(): void {
  logEvent(EventName.LOAN_STEP1_VIEWED);
}

export function trackLoanAmountSelected(amount: number): void {
  logEvent(EventName.LOAN_AMOUNT_SELECTED, { amount });
}

export function trackLoanPlanSelected(payload: LoanPlanAnalyticsPayload): void {
  logEvent(EventName.LOAN_PLAN_SELECTED, payload);
}

export function trackLoanStep2Viewed(payload: LoanPlanAnalyticsPayload): void {
  logEvent(EventName.LOAN_STEP2_VIEWED, payload);
}

export function trackLoanContactPhoneChanged(): void {
  logEvent(EventName.LOAN_CONTACT_PHONE_CHANGED);
}

export function trackLoanAdvanceReasonChanged(): void {
  logEvent(EventName.LOAN_ADVANCE_REASON_CHANGED);
}

export function trackLoanApplicationSubmitted(
  payload: LoanPlanAnalyticsPayload,
): void {
  logEvent(EventName.LOAN_APPLICATION_SUBMITTED, payload);
}

export function trackLoanTermsOpened(): void {
  logEvent(EventName.LOAN_TERMS_OPENED);
}

export function trackLoanAcceptTyc(): void {
  logEvent(EventName.LOAN_ACCEPT_TYC);
}

export function trackLoanStatusViewed(): void {
  logEvent(EventName.LOAN_STATUS_VIEWED);
}

export function trackLoanSidebarItemViewed(): void {
  logEvent(EventName.LOAN_SIDEBAR_ITEM_VIEWED);
}

export function trackLoanOpenInfoDetail(): void {
  logEvent(EventName.LOAN_OPEN_INFO_DETAIL);
}

export function trackLoanBankAccountNumberShown(): void {
  logEvent(EventName.LOAN_BANK_ACCOUNT_NUMBER_SHOWN);
}

export function trackLoanBankAccountNumberFilled(): void {
  logEvent(EventName.LOAN_BANK_ACCOUNT_NUMBER_FILLED);
}

export function trackLoanBankNameResolved(source: 'catalog' | 'manual'): void {
  logEvent(EventName.LOAN_BANK_NAME_RESOLVED, { source });
}

export function trackLoanBankAccountNumberModalOpened(): void {
  logEvent(EventName.LOAN_BANK_ACCOUNT_NUMBER_MODAL_OPENED);
}

export function trackLoanBankAccountNumberModalConfirmed(): void {
  logEvent(EventName.LOAN_BANK_ACCOUNT_NUMBER_MODAL_CONFIRMED);
}

export function trackLoanBankAccountNumberModalBack(): void {
  logEvent(EventName.LOAN_BANK_ACCOUNT_NUMBER_MODAL_BACK);
}

export function trackLoanHistoryViewed(): void {
  logEvent(EventName.LOAN_HISTORY_VIEWED);
}

export function trackLoanHistoryLinkClicked(
  source: 'request-offer' | 'status',
): void {
  logEvent(EventName.LOAN_HISTORY_LINK_CLICKED, { source });
}
