export type DeliveryType = 'store' | 'pickup' | 'courier';

export interface ShippingMethod {
  id: string;
  name: string;
  description?: string;
  type: DeliveryType;
  price: number;
  logoUrl?: string;
  payments: string[];
  carrierCode?: string;
}

export interface PaymentMethod {
  id: string;
  name: string;
  description?: string;
  logoUrl?: string;
  group?: 'primary' | 'secondary';
  disabled?: boolean;
  disabledReason?: string;
  availableWithGiftCards?: boolean;
}

const CDN = 'https://static.sinsay.com';

export const SHIPPING_METHODS: ShippingMethod[] = [
  {
    id: 'storemethod',
    name: 'Odbiór w sklepie',
    description: 'Bezpłatnie',
    type: 'store',
    price: 0,
    logoUrl: `${CDN}/media/checkout/store.svg`,
    payments: [
      'lpp_papay_payu_blik',
      'lpp_papay_paypo',
      'lpp_papay_payu_card',
      'lpp_papay_payu_quickcheckout',
      'lpp_papay_klarna',
      'giftcard',
      'lpp_papay_paypal',
    ],
    carrierCode: 'store',
  },
  {
    id: 'orlenpp',
    name: 'Orlen Paczka',
    type: 'pickup',
    price: 9.99,
    logoUrl: `${CDN}/media/checkout/orlen.svg`,
    payments: [
      'lpp_papay_payu_blik',
      'lpp_papay_paypo',
      'lpp_papay_payu_card',
      'lpp_papay_payu_quickcheckout',
      'lpp_papay_klarna',
      'giftcard',
      'lpp_papay_paypal',
    ],
    carrierCode: 'orlen',
  },
  {
    id: 'dpdmetapp',
    name: 'DPD Pickup',
    type: 'pickup',
    price: 9.99,
    logoUrl: `${CDN}/media/checkout/dpd.svg`,
    payments: [
      'lpp_papay_payu_blik',
      'lpp_papay_paypo',
      'lpp_papay_payu_card',
      'lpp_papay_payu_quickcheckout',
      'lpp_papay_klarna',
      'giftcard',
      'lpp_papay_paypal',
    ],
    carrierCode: 'dpd',
  },
  {
    id: 'inpostpp',
    name: 'InPost Paczkomat',
    type: 'pickup',
    price: 9.99,
    logoUrl: `${CDN}/media/checkout/inpost.svg`,
    payments: [
      'lpp_papay_payu_blik',
      'lpp_papay_paypo',
      'lpp_papay_payu_card',
      'lpp_papay_payu_quickcheckout',
      'lpp_papay_klarna',
      'giftcard',
      'lpp_papay_paypal',
    ],
    carrierCode: 'inpost',
  },
  {
    id: 'pocztapolskacouriermethod',
    name: 'Poczta Polska – kurier',
    type: 'courier',
    price: 14.99,
    payments: [
      'lpp_papay_payu_blik',
      'lpp_papay_paypo',
      'lpp_papay_payu_card',
      'lpp_papay_payu_quickcheckout',
      'lpp_papay_klarna',
      'giftcard',
      'lpp_papay_paypal',
    ],
    carrierCode: 'pocztapolska',
  },
  {
    id: 'dpdmetacouriermethod',
    name: 'DPD Kurier',
    type: 'courier',
    price: 14.99,
    logoUrl: `${CDN}/media/checkout/dpd.svg`,
    payments: [
      'lpp_papay_payu_blik',
      'lpp_papay_paypo',
      'lpp_papay_payu_card',
      'lpp_papay_payu_quickcheckout',
      'lpp_papay_klarna',
      'giftcard',
      'lpp_papay_paypal',
    ],
    carrierCode: 'dpd',
  },
  {
    id: 'dpdmetacouriermethodcod',
    name: 'DPD Kurier – za pobraniem',
    type: 'courier',
    price: 19.99,
    logoUrl: `${CDN}/media/checkout/dpd.svg`,
    payments: ['cashondelivery'],
    carrierCode: 'dpd',
  },
];

export const PAYMENT_METHODS: PaymentMethod[] = [
  {
    id: 'lpp_papay_payu_blik',
    name: 'BLIK',
    logoUrl: `${CDN}/media/checkout/blik.svg`,
    group: 'primary',
  },
  {
    id: 'lpp_papay_paypo',
    name: 'PayPo',
    logoUrl: `${CDN}/media/checkout/paypo.svg`,
    group: 'primary',
  },
  {
    id: 'lpp_papay_payu_card',
    name: 'Karta płatnicza',
    description: 'Visa, Mastercard',
    logoUrl: `${CDN}/media/checkout/card.svg`,
    group: 'primary',
  },
  {
    id: 'lpp_papay_google_pay',
    name: 'Google Pay',
    group: 'primary',
    disabled: true,
    disabledReason: 'Dostępne tylko w aplikacji Sinsay',
  },
  {
    id: 'lpp_papay_apple_pay',
    name: 'Apple Pay',
    group: 'primary',
    disabled: true,
    disabledReason: 'Dostępne tylko w aplikacji Sinsay',
  },
  {
    id: 'lpp_papay_payu_quickcheckout',
    name: 'Szybki przelew',
    description: 'mBank, PKO BP, Santander i inne',
    group: 'secondary',
  },
  {
    id: 'lpp_papay_klarna',
    name: 'Klarna',
    logoUrl: `${CDN}/media/checkout/klarna.svg`,
    group: 'secondary',
  },
  {
    id: 'giftcard',
    name: 'Karta podarunkowa',
    group: 'secondary',
    availableWithGiftCards: true,
  },
  {
    id: 'lpp_papay_paypal',
    name: 'PayPal',
    logoUrl: `${CDN}/media/checkout/paypal.svg`,
    group: 'secondary',
  },
  {
    id: 'cashondelivery',
    name: 'Za pobraniem',
    description: 'Płatność przy odbiorze',
    group: 'secondary',
  },
];

const shippingById = new Map(SHIPPING_METHODS.map((m) => [m.id, m]));
const paymentById = new Map(PAYMENT_METHODS.map((m) => [m.id, m]));

export function getShippingMethod(id: string): ShippingMethod | undefined {
  return shippingById.get(id);
}

export function getPaymentMethod(id: string): PaymentMethod | undefined {
  return paymentById.get(id);
}

export function getShippingMethods(_cartTotal?: number): ShippingMethod[] {
  return SHIPPING_METHODS.map((m) => ({ ...m }));
}

export function getPaymentMethodsForShipping(shippingId: string): PaymentMethod[] {
  const shipping = shippingById.get(shippingId);
  if (!shipping) return [];
  const allowed = new Set(shipping.payments);
  return PAYMENT_METHODS.filter((p) => allowed.has(p.id) && !p.disabled).map((p) => ({
    ...p,
  }));
}

export function isPaymentAllowedForShipping(
  shippingId: string,
  paymentId: string,
): boolean {
  const shipping = shippingById.get(shippingId);
  if (!shipping) return false;
  return shipping.payments.includes(paymentId);
}

export function getCatalogResponse(cartTotal = 0) {
  return {
    shipping: getShippingMethods(cartTotal),
    payments: PAYMENT_METHODS,
    cartTotal,
    freeShippingThreshold: null as number | null,
  };
}

export function formatPaymentLabel(id: string): string {
  return paymentById.get(id)?.name ?? id;
}

export function formatShippingLabel(id: string): string {
  return shippingById.get(id)?.name ?? id;
}
