/**
 * Checkout UI types. Mirrors backend `convex/model/checkout.ts` types
 * with string IDs (Convex serializes Id<T> as strings over the wire).
 *
 * Convention: checkout content components are presentational (receive data via props).
 * Data fetching lives at the page/container level.
 */

// ─── Cart Item Issues (discriminated union) ─────────────────────────────────

export type CheckoutItemIssue =
  | { type: "out_of_stock"; variantId: string; requested: number; available: number }
  | { type: "insufficient_stock"; variantId: string; requested: number; available: number }
  | { type: "variant_inactive"; variantId: string }
  | { type: "variant_not_found"; variantId: string }
  | { type: "product_not_found"; productId: string }
  | { type: "price_changed"; variantId: string; oldPrice: number; newPrice: number };

// ─── Validated & Enriched Cart Item ─────────────────────────────────────────

export type CheckoutValidatedItem = {
  variantId: string;
  productId: string;
  quantity: number;
  unitPrice: number;
  originalPrice: number;
  productName: string;
  variantName: string;
  sku: string;
  imageUrl: string | undefined;
  stockQuantity: number;

  weight: number;
  weightUnit: "g" | "kg" | "lb" | "oz";
  length: number | undefined;
  width: number | undefined;
  height: number | undefined;
  dimensionUnit: "cm" | "in" | undefined;

  productSlug: string;
  parentCategorySlug: string;
  childCategorySlug: string;
};

// ─── Validation Result ──────────────────────────────────────────────────────

export type CheckoutValidationResult = {
  valid: boolean;
  items: CheckoutValidatedItem[];
  issues: CheckoutItemIssue[];
  subtotal: number;
};

// ─── Address Validation Types ───────────────────────────────────────────────

export type CheckoutRecommendedAddress = {
  addressLine1: string;
  additionalInformation?: string;
  city: string;
  postalCode: string;
  country: string;
  completeAddress?: string;
  confidenceScore: "high" | "medium" | "low";
  confidenceCode: string;
  confidenceDescription: string;
};

export type CheckoutAddressValidationResult = {
  isValid: boolean;
  validationValue: "valid" | "partially_valid" | "invalid";
  reasons: Array<{ code: string; description: string }>;
  addressType: "residential" | "commercial" | "unknown" | "po_box" | "military";
  changedAttributes: string[];
  recommendedAddress?: CheckoutRecommendedAddress;
  originalAddress: {
    addressLine1: string;
    additionalInformation?: string;
    city: string;
    postalCode: string;
    country: string;
  };
};

export type CheckoutAddress = {
  id: string;
  type: "shipping" | "billing";
  fullName: string;
  firstName: string;
  lastName: string;
  phone: string;
  addressLine1: string;
  additionalInformation?: string;
  city: string;
  postalCode: string;
  country: string;
  isDefault: boolean;
  isValidated: boolean;
};

// ─── Shipping Rate Types ─────────────────────────────────────────────────────

export type CheckoutSelectedShippingRate = {
  provider: string;
  serviceName: string;
  serviceToken: string;
  amount: number;
  currency: string;
  estimatedDays: number | null;
  durationTerms: string;
  carrierAccount: string;
};

export type CheckoutShippingRateResult = {
  shipmentObjectId: string;
  selectedRate: CheckoutSelectedShippingRate;
  alternativeRates: CheckoutSelectedShippingRate[];
  cartSubtotal: number;
  shippingTotal: number;
  orderTotal: number;
};

// ─── Address Form Data ───────────────────────────────────────────────────────

export type AddressFormData = {
  firstName: string;
  lastName: string;
  phone: string;
  addressLine1: string;
  additionalInformation?: string;
  city: string;
  postalCode: string;
  country: string;
};

// ─── Payment Step Props ──────────────────────────────────────────────────────

export type PaymentStepProps = {
  shipmentObjectId: string;
  selectedShippingRate: CheckoutSelectedShippingRate;
  addressId: string;
  sessionId: string | undefined;
  onBack: () => void;
};
