// Branded ID types for type safety
export type ProductId = string & { __brand: 'ProductId' };
export type VariantId = string & { __brand: 'VariantId' };
export type CollectionId = string & { __brand: 'CollectionId' };
export type OrderId = string & { __brand: 'OrderId' };
export type CustomerId = string & { __brand: 'CustomerId' };
export type FulfillmentId = string & { __brand: 'FulfillmentId' };
export type FulfillmentOrderId = string & { __brand: 'FulfillmentOrderId' };
export type LocationId = string & { __brand: 'LocationId' };
export type InventoryItemId = string & { __brand: 'InventoryItemId' };
export type PriceRuleId = string & { __brand: 'PriceRuleId' };
export type DiscountCodeId = string & { __brand: 'DiscountCodeId' };
export type ThemeId = string & { __brand: 'ThemeId' };
export type PageId = string & { __brand: 'PageId' };
export type BlogId = string & { __brand: 'BlogId' };
export type ArticleId = string & { __brand: 'ArticleId' };
export type WebhookId = string & { __brand: 'WebhookId' };
export type MetafieldId = string & { __brand: 'MetafieldId' };
export type TransactionId = string & { __brand: 'TransactionId' };
export type RefundId = string & { __brand: 'RefundId' };

// Order status discriminated unions
export type OrderFinancialStatus = 
  | 'pending'
  | 'authorized'
  | 'partially_paid'
  | 'paid'
  | 'partially_refunded'
  | 'refunded'
  | 'voided';

export type OrderFulfillmentStatus = 
  | 'fulfilled'
  | 'null'
  | 'partial'
  | 'restocked';

export type OrderStatus = 'open' | 'archived' | 'cancelled';

// Product types
export interface ProductImage {
  id?: number;
  product_id?: ProductId;
  position?: number;
  created_at?: string;
  updated_at?: string;
  alt?: string | null;
  width?: number;
  height?: number;
  src: string;
  variant_ids?: VariantId[];
}

export interface ProductVariant {
  id: VariantId;
  product_id: ProductId;
  title: string;
  price: string;
  sku?: string | null;
  position?: number;
  inventory_policy?: 'deny' | 'continue';
  compare_at_price?: string | null;
  fulfillment_service?: string;
  inventory_management?: string | null;
  option1?: string | null;
  option2?: string | null;
  option3?: string | null;
  created_at?: string;
  updated_at?: string;
  taxable?: boolean;
  barcode?: string | null;
  grams?: number;
  image_id?: number | null;
  weight?: number;
  weight_unit?: string;
  inventory_item_id?: InventoryItemId;
  inventory_quantity?: number;
  old_inventory_quantity?: number;
  requires_shipping?: boolean;
}

export interface ProductOption {
  id?: number;
  product_id?: ProductId;
  name: string;
  position?: number;
  values: string[];
}

export interface Product {
  id: ProductId;
  title: string;
  body_html?: string | null;
  vendor?: string;
  product_type?: string;
  created_at?: string;
  handle?: string;
  updated_at?: string;
  published_at?: string | null;
  template_suffix?: string | null;
  status?: 'active' | 'archived' | 'draft';
  published_scope?: string;
  tags?: string;
  admin_graphql_api_id?: string;
  variants?: ProductVariant[];
  options?: ProductOption[];
  images?: ProductImage[];
  image?: ProductImage | null;
}

// Collection types
export interface CollectionRule {
  column: string;
  relation: string;
  condition: string;
}

export interface SmartCollection {
  id: CollectionId;
  handle?: string;
  title: string;
  updated_at?: string;
  body_html?: string | null;
  published_at?: string | null;
  sort_order?: string;
  template_suffix?: string | null;
  published_scope?: string;
  admin_graphql_api_id?: string;
  rules?: CollectionRule[];
  disjunctive?: boolean;
}

export interface CustomCollection {
  id: CollectionId;
  handle?: string;
  title: string;
  updated_at?: string;
  body_html?: string | null;
  published_at?: string | null;
  sort_order?: string;
  template_suffix?: string | null;
  published_scope?: string;
  admin_graphql_api_id?: string;
}

export type Collection = SmartCollection | CustomCollection;

// Order types
export interface OrderLineItem {
  id: number;
  variant_id?: VariantId | null;
  title: string;
  quantity: number;
  price: string;
  grams?: number;
  sku?: string | null;
  variant_title?: string | null;
  vendor?: string | null;
  fulfillment_service?: string;
  product_id?: ProductId | null;
  requires_shipping?: boolean;
  taxable?: boolean;
  gift_card?: boolean;
  name?: string;
  properties?: Array<{ name: string; value: string }>;
  product_exists?: boolean;
  fulfillable_quantity?: number;
  total_discount?: string;
  fulfillment_status?: 'fulfilled' | 'partial' | 'null';
}

export interface ShippingAddress {
  first_name?: string | null;
  address1?: string | null;
  phone?: string | null;
  city?: string | null;
  zip?: string | null;
  province?: string | null;
  country?: string | null;
  last_name?: string | null;
  address2?: string | null;
  company?: string | null;
  latitude?: number | null;
  longitude?: number | null;
  name?: string;
  country_code?: string | null;
  province_code?: string | null;
}

export interface Transaction {
  id: TransactionId;
  order_id: OrderId;
  kind: 'authorization' | 'capture' | 'sale' | 'void' | 'refund';
  gateway: string;
  status: 'pending' | 'failure' | 'success' | 'error';
  message?: string | null;
  created_at?: string;
  test?: boolean;
  authorization?: string | null;
  currency?: string;
  amount?: string;
  parent_id?: TransactionId | null;
}

export interface Refund {
  id: RefundId;
  order_id: OrderId;
  created_at?: string;
  note?: string | null;
  user_id?: number | null;
  processed_at?: string | null;
  restock?: boolean;
  transactions?: Transaction[];
  refund_line_items?: Array<{
    id: number;
    quantity: number;
    line_item_id: number;
    location_id?: LocationId | null;
    restock_type?: string;
    subtotal?: string;
    total_tax?: string;
  }>;
}

export interface Fulfillment {
  id: FulfillmentId;
  order_id: OrderId;
  status: 'pending' | 'open' | 'success' | 'cancelled' | 'error' | 'failure';
  created_at?: string;
  service?: string | null;
  updated_at?: string;
  tracking_company?: string | null;
  shipment_status?: string | null;
  location_id?: LocationId | null;
  tracking_number?: string | null;
  tracking_numbers?: string[];
  tracking_url?: string | null;
  tracking_urls?: string[];
  receipt?: Record<string, unknown>;
  line_items?: OrderLineItem[];
}

export interface FulfillmentOrder {
  id: FulfillmentOrderId;
  shop_id: number;
  order_id: OrderId;
  assigned_location_id?: LocationId | null;
  request_status: string;
  status: 'open' | 'in_progress' | 'cancelled' | 'incomplete' | 'closed';
  supported_actions?: string[];
  destination?: ShippingAddress;
  line_items?: Array<{
    id: number;
    shop_id: number;
    fulfillment_order_id: FulfillmentOrderId;
    quantity: number;
    line_item_id: number;
    inventory_item_id: InventoryItemId;
    fulfillable_quantity: number;
    variant_id: VariantId;
  }>;
  fulfillment_service_handle?: string;
  created_at?: string;
  updated_at?: string;
}

export interface Order {
  id: OrderId;
  email?: string | null;
  closed_at?: string | null;
  created_at?: string;
  updated_at?: string;
  number?: number;
  note?: string | null;
  token?: string;
  gateway?: string | null;
  test?: boolean;
  total_price?: string;
  subtotal_price?: string;
  total_weight?: number;
  total_tax?: string;
  taxes_included?: boolean;
  currency?: string;
  financial_status?: OrderFinancialStatus;
  confirmed?: boolean;
  total_discounts?: string;
  total_line_items_price?: string;
  cart_token?: string | null;
  buyer_accepts_marketing?: boolean;
  name?: string;
  referring_site?: string | null;
  landing_site?: string | null;
  cancelled_at?: string | null;
  cancel_reason?: string | null;
  user_id?: number | null;
  location_id?: LocationId | null;
  processed_at?: string | null;
  device_id?: number | null;
  phone?: string | null;
  customer_locale?: string | null;
  app_id?: number | null;
  browser_ip?: string | null;
  landing_site_ref?: string | null;
  order_number?: number;
  discount_codes?: Array<{ code: string; amount: string; type: string }>;
  note_attributes?: Array<{ name: string; value: string }>;
  payment_gateway_names?: string[];
  processing_method?: string;
  source_name?: string;
  fulfillment_status?: OrderFulfillmentStatus;
  tax_lines?: Array<{ price: string; rate: number; title: string }>;
  tags?: string;
  contact_email?: string | null;
  order_status_url?: string;
  presentment_currency?: string;
  total_line_items_price_set?: Record<string, unknown>;
  total_discounts_set?: Record<string, unknown>;
  total_shipping_price_set?: Record<string, unknown>;
  subtotal_price_set?: Record<string, unknown>;
  total_price_set?: Record<string, unknown>;
  total_tax_set?: Record<string, unknown>;
  line_items?: OrderLineItem[];
  shipping_lines?: Array<{
    id: number;
    title: string;
    price: string;
    code: string;
    source: string;
  }>;
  shipping_address?: ShippingAddress | null;
  billing_address?: ShippingAddress | null;
  fulfillments?: Fulfillment[];
  refunds?: Refund[];
  customer?: Customer | null;
}

// Customer types
export interface CustomerAddress {
  id: number;
  customer_id: CustomerId;
  first_name?: string | null;
  last_name?: string | null;
  company?: string | null;
  address1?: string | null;
  address2?: string | null;
  city?: string | null;
  province?: string | null;
  country?: string | null;
  zip?: string | null;
  phone?: string | null;
  name?: string;
  province_code?: string | null;
  country_code?: string | null;
  country_name?: string;
  default?: boolean;
}

export interface Customer {
  id: CustomerId;
  email?: string | null;
  accepts_marketing?: boolean;
  created_at?: string;
  updated_at?: string;
  first_name?: string | null;
  last_name?: string | null;
  orders_count?: number;
  state?: 'disabled' | 'invited' | 'enabled' | 'declined';
  total_spent?: string;
  last_order_id?: OrderId | null;
  note?: string | null;
  verified_email?: boolean;
  multipass_identifier?: string | null;
  tax_exempt?: boolean;
  phone?: string | null;
  tags?: string;
  last_order_name?: string | null;
  currency?: string;
  addresses?: CustomerAddress[];
  accepts_marketing_updated_at?: string;
  marketing_opt_in_level?: string | null;
  admin_graphql_api_id?: string;
  default_address?: CustomerAddress;
}

export interface CustomerSavedSearch {
  id: number;
  name: string;
  created_at?: string;
  updated_at?: string;
  query: string;
}

// Inventory types
export interface InventoryItem {
  id: InventoryItemId;
  sku?: string | null;
  created_at?: string;
  updated_at?: string;
  requires_shipping?: boolean;
  cost?: string | null;
  country_code_of_origin?: string | null;
  province_code_of_origin?: string | null;
  harmonized_system_code?: string | null;
  tracked?: boolean;
  country_harmonized_system_codes?: Array<{
    country_code: string;
    harmonized_system_code: string;
  }>;
}

export interface InventoryLevel {
  inventory_item_id: InventoryItemId;
  location_id: LocationId;
  available?: number | null;
  updated_at?: string;
  admin_graphql_api_id?: string;
}

export interface Location {
  id: LocationId;
  name: string;
  address1?: string | null;
  address2?: string | null;
  city?: string | null;
  zip?: string | null;
  province?: string | null;
  country?: string | null;
  phone?: string | null;
  created_at?: string;
  updated_at?: string;
  country_code?: string;
  country_name?: string;
  province_code?: string | null;
  legacy?: boolean;
  active?: boolean;
  admin_graphql_api_id?: string;
}

// Discount types
export interface PriceRule {
  id: PriceRuleId;
  title: string;
  target_type: 'line_item' | 'shipping_line';
  target_selection: 'all' | 'entitled';
  allocation_method: 'each' | 'across';
  value_type: 'fixed_amount' | 'percentage';
  value: string;
  customer_selection: 'all' | 'prerequisite';
  prerequisite_customer_ids?: CustomerId[];
  prerequisite_saved_search_ids?: number[];
  prerequisite_subtotal_range?: { greater_than_or_equal_to?: string };
  prerequisite_quantity_range?: { greater_than_or_equal_to?: number };
  prerequisite_shipping_price_range?: { less_than_or_equal_to?: string };
  entitled_product_ids?: ProductId[];
  entitled_variant_ids?: VariantId[];
  entitled_collection_ids?: CollectionId[];
  entitled_country_ids?: number[];
  starts_at: string;
  ends_at?: string | null;
  created_at?: string;
  updated_at?: string;
  once_per_customer?: boolean;
  usage_limit?: number | null;
  admin_graphql_api_id?: string;
}

export interface DiscountCode {
  id: DiscountCodeId;
  price_rule_id: PriceRuleId;
  code: string;
  usage_count?: number;
  created_at?: string;
  updated_at?: string;
}

// Shipping types
export interface ShippingZone {
  id: number;
  name: string;
  countries?: Array<{
    id: number;
    name: string;
    tax: number;
    code: string;
    tax_name: string;
    provinces: Array<{
      id: number;
      country_id: number;
      name: string;
      code: string;
      tax: number;
      tax_name: string;
      tax_type: string | null;
      shipping_zone_id: number;
      tax_percentage: number;
    }>;
  }>;
  weight_based_shipping_rates?: Array<{
    id: number;
    weight_low: number;
    weight_high: number;
    price: string;
    name: string;
  }>;
  price_based_shipping_rates?: Array<{
    id: number;
    min_order_subtotal: string;
    max_order_subtotal: string | null;
    price: string;
    name: string;
  }>;
  carrier_shipping_rate_providers?: Array<{
    id: number;
    carrier_service_id: number;
    flat_modifier: string;
    percent_modifier: number;
  }>;
}

export interface CarrierService {
  id: number;
  name: string;
  active: boolean;
  service_discovery: boolean;
  carrier_service_type: 'api' | 'legacy';
  admin_graphql_api_id?: string;
  format: 'json' | 'xml';
  callback_url?: string;
}

export interface DeliveryProfile {
  id: number;
  name: string;
  default: boolean;
  created_at?: string;
  updated_at?: string;
}

// Theme types
export interface Theme {
  id: ThemeId;
  name: string;
  created_at?: string;
  updated_at?: string;
  role: 'main' | 'unpublished' | 'demo' | 'development';
  theme_store_id?: number | null;
  previewable?: boolean;
  processing?: boolean;
  admin_graphql_api_id?: string;
}

export interface Asset {
  key: string;
  public_url?: string | null;
  value?: string;
  attachment?: string;
  content_type?: string;
  size?: number;
  created_at?: string;
  updated_at?: string;
  checksum?: string | null;
  theme_id?: ThemeId;
}

// Content types
export interface Page {
  id: PageId;
  title: string;
  shop_id?: number;
  handle?: string;
  body_html?: string | null;
  author?: string;
  created_at?: string;
  updated_at?: string;
  published_at?: string | null;
  template_suffix?: string | null;
  admin_graphql_api_id?: string;
}

export interface Blog {
  id: BlogId;
  handle?: string;
  title: string;
  updated_at?: string;
  commentable?: string;
  feedburner?: string | null;
  feedburner_location?: string | null;
  created_at?: string;
  template_suffix?: string | null;
  tags?: string;
  admin_graphql_api_id?: string;
}

export interface Article {
  id: ArticleId;
  title: string;
  created_at?: string;
  body_html?: string | null;
  blog_id: BlogId;
  author?: string;
  user_id?: number;
  published_at?: string | null;
  updated_at?: string;
  summary_html?: string | null;
  template_suffix?: string | null;
  handle?: string;
  tags?: string;
  admin_graphql_api_id?: string;
  image?: {
    created_at?: string;
    alt?: string | null;
    width?: number;
    height?: number;
    src: string;
  };
}

export interface Redirect {
  id: number;
  path: string;
  target: string;
}

export interface ScriptTag {
  id: number;
  src: string;
  event: 'onload';
  created_at?: string;
  updated_at?: string;
  display_scope: 'online_store' | 'order_status' | 'all';
}

// Webhook types
export interface Webhook {
  id: WebhookId;
  address: string;
  topic: string;
  created_at?: string;
  updated_at?: string;
  format: 'json' | 'xml';
  fields?: string[];
  metafield_namespaces?: string[];
  api_version?: string;
  private_metafield_namespaces?: string[];
}

// Event types
export interface Event {
  id: number;
  subject_id: number;
  created_at?: string;
  subject_type: string;
  verb: string;
  arguments?: unknown[];
  body?: string | null;
  message?: string;
  author?: string;
  description?: string;
  path?: string;
}

// Metafield types
export interface Metafield {
  id: MetafieldId;
  namespace: string;
  key: string;
  value: string;
  type: string;
  description?: string | null;
  owner_id?: number;
  created_at?: string;
  updated_at?: string;
  owner_resource?: string;
  admin_graphql_api_id?: string;
}

// Shop types
export interface Shop {
  id: number;
  name: string;
  email: string;
  domain: string;
  province?: string;
  country?: string;
  address1?: string;
  zip?: string;
  city?: string;
  source?: string | null;
  phone?: string;
  latitude?: number | null;
  longitude?: number | null;
  primary_locale?: string;
  address2?: string | null;
  created_at?: string;
  updated_at?: string;
  country_code?: string;
  country_name?: string;
  currency?: string;
  customer_email?: string;
  timezone?: string;
  iana_timezone?: string;
  shop_owner?: string;
  money_format?: string;
  money_with_currency_format?: string;
  weight_unit?: string;
  province_code?: string | null;
  taxes_included?: boolean | null;
  auto_configure_tax_inclusivity?: boolean | null;
  tax_shipping?: boolean | null;
  county_taxes?: boolean;
  plan_display_name?: string;
  plan_name?: string;
  has_discounts?: boolean;
  has_gift_cards?: boolean;
  myshopify_domain?: string;
  google_apps_domain?: string | null;
  google_apps_login_enabled?: boolean | null;
  money_in_emails_format?: string;
  money_with_currency_in_emails_format?: string;
  eligible_for_payments?: boolean;
  requires_extra_payments_agreement?: boolean;
  password_enabled?: boolean;
  has_storefront?: boolean;
  finances?: boolean;
  primary_location_id?: LocationId;
  checkout_api_supported?: boolean;
  multi_location_enabled?: boolean;
  setup_required?: boolean;
  pre_launch_enabled?: boolean;
  enabled_presentment_currencies?: string[];
}

// API Response wrappers
export interface ShopifyListResponse<T> {
  data: T[];
  pageInfo?: {
    hasNextPage: boolean;
    hasPreviousPage: boolean;
    nextCursor?: string;
    previousCursor?: string;
  };
}

export interface ShopifyError {
  errors: string | Record<string, string[]>;
}

// GraphQL types
export interface GraphQLRequest {
  query: string;
  variables?: Record<string, unknown>;
}

export interface GraphQLResponse<T = unknown> {
  data?: T;
  errors?: Array<{
    message: string;
    locations?: Array<{ line: number; column: number }>;
    path?: string[];
    extensions?: Record<string, unknown>;
  }>;
}
