import {CartDetail,CartAddress} from "@/types/cart/type";

export type AddressType = "cart_billing" | "cart_shipping";





export interface CheckoutAddressEdge {
  node: CheckoutAddressNode;
}

export interface CheckoutAddressNode {
  addressType: AddressType;
  defaultAddress: boolean;
  useForShipping?: boolean;
  id: string;
  _id: number;
  email: string;
  firstName: string;
  lastName: string;
  companyName?: string | null;
  address: string;
  city: string;
  state: string;
  country: string;
  postcode: string;
  phone: string;
}

export interface ShipAddressFormData { 
    shippingAddressId: string | number;
    shippingEmail: string;
    shippingFirstName: string;
    shippingLastName: string;
    shippingCompanyName: string;
    shippingAddress: string;
    shippingCountry: string;
    shippingState: string;
    shippingCity: string;
    shippingPostcode: string;
    shippingPhoneNumber: string;
}
export interface BillAddressFormData { 
    billingAddressId: string | number;
    billingEmail: string;
    billingFirstName: string;
    billingLastName: string;
    billingCompanyName: string;
    billingAddress: string;
    billingCountry: string;
    billingState: string;
    billingCity: string;
    billingPostcode: string;
    billingPhoneNumber: string;
    billingSameAsShipping: boolean;
}

export type FullAddressFormData = ShipAddressFormData & BillAddressFormData;



export interface MappedCheckoutAddress {
  firstName: string;
  lastName: string;
  companyName?: string | null;
  address: string;
  city: string;
  state: string;
  country: string;
  postcode: string;
  email: string;
  phone: string;
}

export interface CheckoutShippingMethodResult {
  success: boolean;
  id: string | null;
  message: string | null;
}

export interface CreateCheckoutShippingMethodPayload {
  checkoutShippingMethod: CheckoutShippingMethodResult;
}

export interface CreateCheckoutShippingMethodData {
  createCheckoutShippingMethod: CreateCheckoutShippingMethodPayload;
}

export interface CreateCheckoutShippingMethodOperation {
  data: CreateCheckoutShippingMethodData;
  variables: CreateCheckoutShippingMethodVariables;
}

export interface CreateCheckoutShippingMethodVariables {
  shippingMethod: string;
}

export interface SelectedPaymentType {
  method: string;
  methodTitle?: string;
}

export interface SelectedShippingRateType {
  method: string;
  methodDescription?: string;
}



// Checkout Payment Methods

export interface CheckoutPaymentMethod {
  id: string;
  _id: string;
  method: string;
  title: string;
  description: string | null;
  icon: string | null;
  isAllowed: boolean;
}

export interface CheckoutPaymentMethodsData {
  collectionPaymentMethods: CheckoutPaymentMethod[];
}
export interface CheckoutPaymentMethodsOperation {
  variables: Record<string, unknown>;
  data: CheckoutPaymentMethodsData;
}
export interface RepayOrderPaymentMethodsData {
  collectionRepayOrderPaymentMethods: CheckoutPaymentMethod[];
}

// checkout save payment

export interface CreateCheckoutPaymentMethodVariables {
  paymentMethod: string;
  successUrl?: string | null;
  failureUrl?: string | null;
  cancelUrl?: string | null;
}

export interface CheckoutPaymentMethod {
  success: boolean;
  message: string;
  paymentGatewayUrl: string | null;
  paymentData: unknown;
}

export interface CreateCheckoutPaymentMethodResponse {
  createCheckoutPaymentMethod: {
    checkoutPaymentMethod: CheckoutPaymentMethod;
  };
}

export interface CreateCheckoutPaymentMethodOperation {
  data: CreateCheckoutPaymentMethodResponse;
  variables: CreateCheckoutPaymentMethodVariables;
}

// Checkout Shipping Rates

export interface CheckoutShippingRate {
  id: string;
  code: string;
  description: string | null;
  method: string;
  price: number;
  label: string;
  _id: string;
  formattedPrice: string;
}

export interface GetCheckoutShippingRatesData {
  collectionShippingRates: CheckoutShippingRate[];
}

export interface GetCheckoutShippingRatesVariables {
  token: string;
}

export interface GetCheckoutShippingRatesOperation {
  data: GetCheckoutShippingRatesData;
  variables: GetCheckoutShippingRatesVariables;
}

// save checkout cart
export type SaveCheckoutCart = CartDetail;

export interface SaveCheckoutCartPayload {
  saveCheckoutCart: SaveCheckoutCart;
}
export interface SaveCheckoutCartData {
  createSaveCheckoutCart: SaveCheckoutCartPayload;
}
export interface SaveCheckoutCartVariables {
  shippingMethod: string;
  paymentMethod: string;
  couponCode: string;
}
// Checkout Place Order

export interface CheckoutOrder {
  id: string;
  orderId: string;
  message: string;
  success: boolean;
  paymentTransactionId: string;
}
export interface CreateCheckoutOrderPayload {
  checkoutOrder: CheckoutOrder;
}
export interface CreateCheckoutOrderData {
  createCheckoutOrder: CreateCheckoutOrderPayload;
}
export interface CreateCheckoutOrderOperation {
  data: CreateCheckoutOrderData;
}


export interface CreatePaymentInitiate {
  id: string;
  success: boolean;
  message: string;
  orderId: string;
  status: string;
  gatewayOrderId: string;
  newCartToken: string;
  express: boolean;
}

export interface CreatePaymentInitiatePayload {
  paymentInitiate: CreatePaymentInitiate;
}
export interface CreatePaymentInitiateData {
  createPaymentInitiate: CreatePaymentInitiatePayload;
}
export interface CreatePaymentInitiateVariables {
    expressCheckout?: boolean;
    paymentMethod?: string;
    paymentSuccessUrl?: string;
    paymentFailureUrl?: string;
    paymentCancelUrl?: string;
}

// create payment callback
export interface CreatePaymentCallback {
  id: string;
  success: boolean;
  message: string;
  orderId: string;
  status: string;
  orderStatus: string;
  gatewayStatus: string;
}

export interface CreatePaymentCallbackPayload {
  paymentCallback: CreatePaymentCallback;
}
export interface CreatePaymentCallbackData {
  createPaymentCallback: CreatePaymentCallbackPayload;
}
export interface CreatePaymentCallbackVariables {
  orderId: number;
  gatewayOrderId: string;
  status: "success" | "cancel" | "failure";
  paymentSignature?: string;
  verifyToken?: string;
  
  // 快捷支付需要
  shippingFirstName?: string;
  shippingLastName?: string;
  shippingEmail?: string;
  shippingAddress?: string;
  shippingCountry?: string;
  shippingState?: string;
  shippingCity?: string;
  shippingPostcode?: string;
  shippingPhoneNumber?: string;

  billingFirstName?: string;
  billingLastName?: string;
  billingEmail?: string;
  billingAddress?: string;
  billingCountry?: string;
  billingState?: string;
  billingCity?: string;
  billingPostcode?: string;
  billingPhoneNumber?: string;
}

// calcel order
export interface CreateCancelOrder {
  id: number;
  success: boolean;
  message: string;
  orderId: number;
  status: string;
}

export interface CreateCancelOrderPayload {
  cancelOrder: CreateCancelOrder;
}
export interface CreateCancelOrderData {
  createCancelOrder: CreateCancelOrderPayload;
}
export interface CreateCancelOrderVariables {
  orderId: number;
}


// Checkout Get Address
export interface CheckoutAddress {
  id: string;
  _id: number;
  addressType: string;
  firstName: string;
  lastName: string;
  companyName: string | null;
  address: string;
  city: string;
  state: string;
  country: string;
  postcode: string;
  email: string;
  phone: string;
  defaultAddress: boolean;
  useForShipping: boolean;
}


export interface CheckoutAddressConnection {
  edges: CheckoutAddressEdge[];
}
export interface GetCheckoutAddressesData {
  collectionGetCheckoutAddresses: CheckoutAddressConnection;
}
export interface GetCheckoutAddressesOperation {
  data: GetCheckoutAddressesData;
}

// checkout save address
export interface CheckoutAddressResult {
  id: string;
  _id: string;
  cartToken: string;

  success: boolean;
  message: string;

  billingFirstName: string;
  billingLastName: string;
  billingAddress: string;
  billingCity: string;
  billingState: string;
  billingPostcode: string;
  billingPhoneNumber: string;

  shippingFirstName: string | null;
  shippingLastName: string | null;
  shippingCity: string | null;
  shippingCompanyName: string | null;
}
export interface CreateCheckoutAddressPayload {
  checkoutAddress: CheckoutAddressResult;
}
export interface CreateCheckoutAddressData {
  createCheckoutAddress: CreateCheckoutAddressPayload;
}
export interface CreateCheckoutAddressVariables {
  // Billing
  billingAddressId?: string | number;
  billingFirstName: string;
  billingLastName: string;
  billingEmail: string;
  billingAddress: string;
  billingCity: string;
  billingCountry: string;
  billingState: string;
  billingPostcode: string;
  billingPhoneNumber: string;
  billingCompanyName: string;

  // Shipping control
  useForShipping?: boolean;

  // Shipping (optional when useForShipping === true)
  shippingAddressId?: string | number;
  shippingFirstName?: string;
  shippingLastName?: string;
  shippingEmail?: string;
  shippingAddress?: string;
  shippingCity?: string;
  shippingCountry?: string;
  shippingState?: string;
  shippingPostcode?: string;
  shippingPhoneNumber?: string;
  shippingCompanyName?: string;
}
export interface CreateCheckoutAddressOperation {
  data: CreateCheckoutAddressData;
  variables: CreateCheckoutAddressVariables;
}

// checkout save payment

export interface Country {
  code: string;
  name: string;
  _id: number;
  id: string;
}

export interface State {
    id: string;
    _id: number;
    code: string;
    defaultName: string;
    countryId: string;
    countryCode: string;
}


export interface ShipAddressFormData { 
    shippingAddressId: string | number,
    shippingEmail: string,
    shippingFirstName: string,
    shippingLastName: string,
    shippingCompanyName: string,
    shippingAddress: string,
    shippingCountry: string,
    shippingState: string,
    shippingCity: string,
    shippingPostcode: string,
    shippingPhoneNumber: string,
}
export interface BillAddressFormData { 
    billingAddressId: string | number,
    billingEmail: string,
    billingFirstName: string,
    billingLastName: string,
    billingCompanyName: string,
    billingAddress: string,
    billingCountry: string,
    billingState: string,
    billingCity: string,
    billingPostcode: string,
    billingPhoneNumber: string,
}

export interface CreatePaymentReplay{
    id: number;
    success: boolean;
    message: string;
    orderId: string;
    status: string;
    gatewayOrderId: string;
}
export interface CreatePaymentReplayPayload {
  paymentReplay: CreatePaymentReplay;
}
export interface CreatePaymentReplayData {
  createPaymentReplay: CreatePaymentReplayPayload;
}
export interface CreatePaymentReplayVariables {
  orderId: number;
  paymentMethod: string;
  paymentSuccessUrl?: string;
  paymentFailureUrl?: string;
  paymentCancelUrl?: string;
}


// 结账页额外用到的类型

export type KlarnaOtherData = {
  billingAddress: CartAddress;
  shippingAddress: CartAddress;
}
export interface PlaceOrderResult<T> {
    // valid: boolean; 
    data: CreatePaymentInitiate | null;
    otherData: T | null;
}

export type PlaceOrderFunction = <T = null>(params?: CreatePaymentInitiateVariables) => Promise<PlaceOrderResult<T>>



export interface AirwallexCardStateType {
    cardNumberReady: boolean;
    cvcReady: boolean;
    expiryReady: boolean;

    cardNumberValid: boolean;
    cvcValid: boolean;
    expiryValid: boolean;

    cardNumberErrorMsg: string;
    cvcErrorMsg: string;
    expiryErrorMsg: string;
}