"use client";

import { useSearchParams } from "next/navigation";
import type { SimCardType, ActivationType, MnpData } from "@customer-portal/domain/sim";
import type { AccessModeValue } from "@customer-portal/domain/orders";

/**
 * Parse URL parameters for configuration deep linking
 *
 * Note: These params are only used for initial page load/deep linking.
 * State management is handled by Zustand store (catalog.store.ts).
 * The store's restore functions handle parsing these params into state.
 */

const parseSimCardType = (value: string | null): SimCardType | null => {
  if (value === "eSIM" || value === "Physical SIM") {
    return value;
  }
  return null;
};

const parseActivationType = (value: string | null): ActivationType | null => {
  if (value === "Immediate" || value === "Scheduled") {
    return value;
  }
  return null;
};

const parsePortingGender = (value: string | null): MnpData["portingGender"] | undefined => {
  if (value === "Male" || value === "Female" || value === "Corporate/Other") {
    return value;
  }
  return undefined;
};

const coalesce = <T>(...values: Array<T | null | undefined>): T | undefined => {
  for (const candidate of values) {
    if (candidate !== null && candidate !== undefined) {
      return candidate;
    }
  }
  return undefined;
};

/**
 * Parse Internet configuration params (for deep linking only)
 * Actual state is managed by Zustand store
 */
export function useInternetConfigureParams() {
  const params = useSearchParams();
  const accessModeParam = params.get("accessMode");
  const accessMode: AccessModeValue | null =
    accessModeParam === "IPoE-BYOR" || accessModeParam === "IPoE-HGW" || accessModeParam === "PPPoE"
      ? accessModeParam
      : null;
  const installationSku = params.get("installationSku");

  // Support both formats: comma-separated 'addons' or multiple 'addonSku' params
  const addonsParam = params.get("addons");
  const addonSkuParams = params.getAll("addonSku");

  const addonSkus = addonsParam
    ? addonsParam
        .split(",")
        .map(s => s.trim())
        .filter(Boolean)
    : addonSkuParams.length > 0
      ? addonSkuParams
      : [];

  return {
    accessMode,
    installationSku: installationSku ?? null,
    addonSkus,
  } as const;
}

/**
 * Parse SIM configuration params (for deep linking only)
 * Actual state is managed by Zustand store
 */
export function useSimConfigureParams() {
  const params = useSearchParams();

  const simType = parseSimCardType(params.get("simType"));
  const activationType = parseActivationType(params.get("activationType"));
  const scheduledAt = coalesce(params.get("scheduledAt"), params.get("scheduledDate")) ?? null;
  const addonSkus = params.getAll("addonSku");
  const isMnp = coalesce(params.get("isMnp"), params.get("wantsMnp"))?.toLowerCase() === "true";
  const eid = params.get("eid") ?? null;

  const mnp: Partial<MnpData> = {
    reservationNumber: coalesce(
      params.get("mnpNumber"),
      params.get("reservationNumber"),
      params.get("mnp_reservationNumber")
    ),
    expiryDate: coalesce(
      params.get("mnpExpiry"),
      params.get("expiryDate"),
      params.get("mnp_expiryDate")
    ),
    phoneNumber: coalesce(
      params.get("mnpPhone"),
      params.get("phoneNumber"),
      params.get("mnp_phoneNumber")
    ),
    mvnoAccountNumber: coalesce(
      params.get("mvnoAccountNumber"),
      params.get("mnp_mvnoAccountNumber")
    ),
    portingLastName: coalesce(params.get("portingLastName"), params.get("mnp_portingLastName")),
    portingFirstName: coalesce(params.get("portingFirstName"), params.get("mnp_portingFirstName")),
    portingLastNameKatakana: coalesce(
      params.get("portingLastNameKatakana"),
      params.get("mnp_portingLastNameKatakana")
    ),
    portingFirstNameKatakana: coalesce(
      params.get("portingFirstNameKatakana"),
      params.get("mnp_portingFirstNameKatakana")
    ),
    portingGender: parsePortingGender(
      coalesce(params.get("portingGender"), params.get("mnp_portingGender")) ?? null
    ),
    portingDateOfBirth: coalesce(
      params.get("portingDateOfBirth"),
      params.get("mnp_portingDateOfBirth")
    ),
  };

  return {
    simType,
    eid,
    activationType,
    scheduledAt,
    addonSkus,
    isMnp,
    mnp,
  } as const;
}
