import {
  REST_Customer as RESTCustomer,
} from '../../types/ShopifyAdminREST';

import { ShopifyObjectModel } from './ShopifyObject';

export const OmittedCustomerKeys = [
  'accepts_marketing',
  'accepts_marketing_updated_at',
  'default_address',
  'email_marketing_consent',
  'marketing_opt_in_level',
  'metafield',
  'sms_marketing_consent',
  'addresses',
  'id',
  'admin_graphql_api_id'
] as const;
export type OmittedCustomerKey = typeof OmittedCustomerKeys[number];

type RemainingCustomerKey = Exclude<keyof RESTCustomer, OmittedCustomerKey>;
const RemainingCustomerKeysRecord:Record<RemainingCustomerKey, undefined> = {
  created_at: undefined,
  currency: undefined,
  email: undefined,
  first_name: undefined,
  last_name: undefined,
  last_order_id: undefined,
  last_order_name: undefined,
  multipass_identifier: undefined,
  note: undefined,
  orders_count: undefined,
  phone: undefined,
  state: undefined,
  tags: undefined,
  tax_exempt: undefined,
  tax_exemptions: undefined,
  total_spent: undefined,
  updated_at: undefined,
  verified_email: undefined,
  name: undefined,
};
const RemainingCustomerKeys = Object.keys(RemainingCustomerKeysRecord) as readonly RemainingCustomerKey[];

const NewCustomerKeys = [
  'id',
  'addresses_json',
  'average_order_value',
  'default_address.address1',
  'default_address.address2',
  'default_address.city',
  'default_address.company',
  'default_address.country',
  'default_address.country_code',
  'default_address.first_name',
  'default_address.id',
  'default_address.last_name',
  'default_address.phone',
  'default_address.province',
  'default_address.province_code',
  'default_address.zip',
  'email_marketing_consent.opt_in_level',
  'email_marketing_consent.state',
  'email_marketing_consent.updated_at',
  'sms_marketing_consent.collected_from',
  'sms_marketing_consent.opt_in_level',
  'sms_marketing_consent.state',
  'sms_marketing_consent.updated_at',
] as const;

type NewCustomerKeys = typeof NewCustomerKeys[number];
type CustomerProps = Omit<RESTCustomer, OmittedCustomerKey> & {
  'id': RESTCustomer['admin_graphql_api_id'];
  'addresses_json': RESTCustomer['addresses'];
  'average_order_value': RESTCustomer['total_spent'];
  'default_address.address1': RESTCustomer['default_address']['address1'];
  'default_address.address2': RESTCustomer['default_address']['address2'];
  'default_address.city': RESTCustomer['default_address']['city'];
  'default_address.company': RESTCustomer['default_address']['company'];
  'default_address.country': RESTCustomer['default_address']['country'];
  'default_address.country_code': RESTCustomer['default_address']['country_code'];
  'default_address.first_name': RESTCustomer['default_address']['first_name'];
  'default_address.id': RESTCustomer['default_address']['id'];
  'default_address.last_name': RESTCustomer['default_address']['last_name'];
  'default_address.phone': RESTCustomer['default_address']['phone'];
  'default_address.province': RESTCustomer['default_address']['province'];
  'default_address.province_code': RESTCustomer['default_address']['province_code'];
  'default_address.zip': RESTCustomer['default_address']['zip'];
  'email_marketing_consent.opt_in_level': RESTCustomer['email_marketing_consent']['opt_in_level'] | null;
  'email_marketing_consent.state': RESTCustomer['email_marketing_consent']['state'] | null;
  'email_marketing_consent.updated_at': RESTCustomer['email_marketing_consent']['consent_updated_at'] | null;
  'sms_marketing_consent.collected_from': RESTCustomer['sms_marketing_consent']['consent_collected_from'] | null;
  'sms_marketing_consent.opt_in_level': RESTCustomer['sms_marketing_consent']['opt_in_level'] | null;
  'sms_marketing_consent.state': RESTCustomer['sms_marketing_consent']['state'] | null;
  'sms_marketing_consent.updated_at': RESTCustomer['sms_marketing_consent']['consent_updated_at'] | null;
};

export const CustomerKeys = [
  ...RemainingCustomerKeys,
  ...NewCustomerKeys
] as const;

export type CustomerKey = (typeof CustomerKeys)[number];

export const CustomerTypeName = 'customers';
export type CustomerTypeName = typeof CustomerTypeName;

export interface Customer extends ShopifyObjectModel {
  type: CustomerTypeName;
  props: CustomerProps;
}