import {
  OrderTypeName
} from '../../models/shopify';
import {
  CurrencyCode,
  OrderCancelReason,
  OrderDisplayFinancialStatus,
  OrderDisplayFulfillmentStatus,
  OrderRiskLevel
} from '../../types/ShopifyGraphQLAdminAPI';
import {
  OrderAirtableFields as oaf
} from '../airtable';
import * as AirtableFields from '../airtable/AirtableFieldDefaultConfigs';

import {
  getShopifyObjectAirtableFields
} from './getCreateTableProps';

const {
  id,
  barcode,
  url,
  singleLineText,
  datetime,
  checkbox,
  singleSelect,
  multipleRecordLinks,
  float,
  currency,
  multiLineText,
  json,
  integer,
  multipleSelects,
  latLong
} = AirtableFields;

function getSingleSelectConfig(values:string[]) {
  return {
    ...singleSelect,
    options: {
      choices: values.map(wu => ({ name: wu }))
    }
  };
}

export const getOrderAirtableFields:getShopifyObjectAirtableFields<OrderTypeName> = ({
  airtableTableLinks,
  baseSyncConfig,
}) => {
  const OrderAirtableFields:oaf = {
    type: OrderTypeName,
    fields: {
      // Core
      name: singleLineText,
      id: singleLineText,
      createdAt: datetime,
      updatedAt: datetime,
      lineItems_json: multiLineText,
      displayFinancialStatus: getSingleSelectConfig(Object.values(OrderDisplayFinancialStatus)),
      displayFulfillmentStatus: getSingleSelectConfig(Object.values(OrderDisplayFulfillmentStatus)),
      tags: multipleSelects,
      phone: singleLineText,
      email: singleLineText,
      note: singleLineText,
      customAttributes_json: json,
      discountCode: singleLineText,

      confirmed: checkbox,
      closed: checkbox,
      closedAt: datetime,
      cancelledAt: datetime,
      cancelReason: getSingleSelectConfig(Object.values(OrderCancelReason)),
      edited: checkbox,

      // Shipping Address
      shippingAddressFirstName: singleLineText,
      shippingAddressLastName: singleLineText,
      shippingAddressAddress1: singleLineText,
      shippingAddressAddress2: singleLineText,
      shippingAddressCity: singleLineText,
      shippingAddressZip: singleLineText,
      shippingAddressProvinceCode: singleLineText,
      shippingAddressCountryCodeV2: singleLineText,
      shippingAddressPhone: singleLineText,
      shippingAddressCompany: singleLineText,

      // Money Fields
      capturable: checkbox,
      unpaid: checkbox,
      fullyPaid: checkbox,
      processedAt: datetime,

      // Total Payment
      refundDiscrepancy: float,
      netPayment: float,
      totalPrice: float,
      totalTax: float,
      totalShippingPrice: float,
      subtotalPrice: float,
      totalDiscounts: float,
      cartDiscountAmount: float,
      totalCapturable: float,
      totalReceived: float,
      totalOutstanding: float,
      totalTipReceived: float,
      totalRefunded: float,
      totalRefundedShipping: float,

      originalTotalDuties: float,
      originalTotalPrice: float,

      // Taxes
      taxLines_json: json,
      currentTaxLines_json: json,

      // Payment Meta
      disputes_json: json,
      transactions_json: json,
      paymentGatewayNames: multipleSelects,
      // Risk
      riskLevel: getSingleSelectConfig(Object.values(OrderRiskLevel)),
      risks_json: json,

      // Current Payment
      currentTotalPrice: float,
      currentTotalTax: float,
      taxesIncluded: checkbox,
      currentSubtotalPrice: float,
      currentCartDiscountAmount: float,
      currentTotalDiscounts: float,
      currentTotalDuties: float,

      fulfillable: checkbox,
      fulfillments_json: json,
      shippingLine_json: json,
      currentTotalWeight: float,
      totalWeight: float,

      // Customer
      customer_json: json,
      customerAcceptsMarketing: checkbox,
      canNotifyCustomer: checkbox,

      // Order Meta
      hasTimelineComment: checkbox,
      merchantEditable: checkbox,
      test: checkbox,
      canMarkAsPaid: checkbox,
      legacyResourceId: singleLineText,
      physicalLocationId: singleLineText,
      publicationId: singleLineText,
      refundable: checkbox,
      requiresShipping: checkbox,
      restockable: checkbox,
      currencyCode: getSingleSelectConfig(Object.values(CurrencyCode)),
      presentmentCurrencyCode: getSingleSelectConfig(Object.values(CurrencyCode)),
      paymentCollectionDetails_json: json,

      // Customer Meta
      customerLocale: singleLineText,
      clientIp: singleLineText,
      customerJourneySummary_json: json,

      billingAddressFirstName: singleLineText,
      billingAddressLastName: singleLineText,
      billingAddressAddress1: singleLineText,
      billingAddressAddress2: singleLineText,
      billingAddressCity: singleLineText,
      billingAddressZip: singleLineText,
      billingAddressProvinceCode: singleLineText,
      billingAddressCountryCodeV2: singleLineText,
      billingAddressPhone: singleLineText,
      billingAddressCompany: singleLineText,

      displayAddressFirstName: singleLineText,
      displayAddressLastName: singleLineText,
      displayAddressAddress1: singleLineText,
      displayAddressAddress2: singleLineText,
      displayAddressCity: singleLineText,
      displayAddressZip: singleLineText,
      displayAddressProvinceCode: singleLineText,
      displayAddressCountryCodeV2: singleLineText,
      displayAddressPhone: singleLineText,
      displayAddressCompany: singleLineText,

      // Less important
      billingAddressCountry: singleLineText,
      billingAddressId: singleLineText,

      billingAddressLatitude: latLong,
      billingAddressLongitude: latLong,
      shippingAddressLatitude: latLong,
      shippingAddressLongitude: latLong,
      displayAddressLatitude: latLong,
      displayAddressLongitude: latLong,

      billingAddressProvince: singleLineText,
      currentSubtotalLineItemsQuantity: integer,
      displayAddressCountry: singleLineText,
      displayAddressId: singleLineText,
      displayAddressProvince: singleLineText,
      merchantEditableErrors: multipleSelects,
      shippingAddressCountry: singleLineText,
      shippingAddressId: singleLineText,
      shippingAddressProvince: singleLineText,
      subtotalLineItemsQuantity: integer,
    }
  } as const;

  return OrderAirtableFields;
};
