export interface SingleProductResponse {
  product: ProductNode;
}
export interface ProductOptionValue {
  code: string;
  id: number;
  label: string;
  meta: [] | Record<string, unknown>;  // Record<string, unknown>也可以写成 { [key: string]: unknown }，表示任意对象，unknow比any更安全
  position: number;
}

export interface ProductOption {
  code: string;
  id: number;
  is_required: boolean;
  label: string;
  meta: [] | Record<string, unknown>;
  position: number;
  type: string;
  values: Array<ProductOptionValue>;
}

export interface ProductFlexibleVariantValue {
    code: string;
    id: number;
    label: string;
    option_code: string;
    option_id: number;
}

export interface ProductFlexibleVariantPriceIndices{
  id: number;
  min_price: string;
  regular_min_price:string;
}

export interface VatiantImages {
  id: number;
  position: number;
  url: string;
}

export interface ProductFlexibleVariant {
  id: string;
  _id: number;
  sku: string;
  variantImages: string | null | VatiantImages[]; 
  optionValues: string | Array<ProductFlexibleVariantValue>
  quantity:number;
  price: string;
  priceIndices: string | null | Array<ProductFlexibleVariantPriceIndices>;
}

export interface ProductMediaType{
  id: string;
  path: string;
  publicPath: string;
  position: string;
}

// 定义解析后的类型(根据最初类型裁剪)
export type ResolvedVariant = Omit<ProductFlexibleVariant, 'optionValues' | 'variantImages | priceIndices'> & {
  optionValues: ProductFlexibleVariantValue[];
  variantImages: VatiantImages[]; // 去掉 string 可能性
  priceIndices: ProductFlexibleVariantPriceIndices[];
};

export interface ProductSectionNode {
  isSaleable: string | undefined;
  id: string;
  sku: string;
  type: string;
  urlKey?: string;
  name?: string;
  baseImageUrl?: string;
  price?: string | number;
  specialPrice?: string;
  images?: {
    edges: Array<{
      node: {
        publicPath: string;
      };
    }>;
  };
}

export interface ProductVariantNode {
  id: string;
  sku: string;
  name?: string;
  price?: number;
  attributeValues?: {
    edges: Array<{
      node: {
        attribute?: {
          code?: string;
        };
        value?: string;
      };
    }>;
  };
}

export interface ProductNode {
  variants: {
    edges: Array<{ node: ProductVariantNode }>;
  };
  id: string;
  _id: number;
  sku: string;
  type: string;
  name?: string;
  urlKey?: string;
  isSaleable?: string | undefined;
  description?: string;
  shortDescription?: string;
  specialPrice?: string;
  metaTitle?: string;
  baseImageUrl?: string;
  images?: {
    edges: Array<{ node: ProductMediaType }>
  };
  price?: string | number | { value?: number; currencyCode?: string } | null;
  minimumPrice?: string | number;
  formattedPrice?: string;
  formattedSpecialPrice?: string | null;
  formattedMinimumPrice?: string;
  productOptions?: string | null;
  flexibleVariants?: Array<ProductFlexibleVariant>;
  priceHtml?: {
    currencyCode?: string;
  };
  superAttributes?: {
    edges: Array<{ node: ProductReviewNode }>;
  };
  reviews?: {
    edges: Array<{ node: ProductReviewNode }>;
  };
  relatedProducts?: {
    edges: Array<{ node: ProductSectionNode }>;
  };
  crossSells?: {
    edges: Array<{ node: ProductSectionNode }>;
  };
  upSells?: {
    edges: Array<{ node: ProductSectionNode }>;
  };
}

export interface ProductsResponse {
  products: {
    edges: Array<{ node: ProductNode }>;
    pageInfo: {
      endCursor: string;
      startCursor: string;
      hasNextPage: boolean;
      hasPreviousPage: boolean;
    };
    totalCount: number;
  };
}

export interface SearchProductsVariables {
    query: string;
    suggest?: boolean;
    filter?: string;
    sortKey?: string;
    reverse?: boolean;
    first?: number;
    last?: number;
    after?: string;
    before?: string;
    channel?: string;
    locale?: string;
}
export interface SearchProductsResponse {
  search: {
    edges: Array<{ node: ProductNode }>;
    pageInfo: {
      endCursor: string;
      startCursor: string;
      hasNextPage: boolean;
      hasPreviousPage: boolean;
    };
    totalCount: number;

    searchMetadata: {
      originalQuery: string;
      effectiveQuery: string;
      resultMessage: string;
      searchInsteadMessage: string;
    }
  };
}

export interface ProductSectionNode {
  id: string;
  sku: string;
  type: string;
  urlKey?: string;
  name?: string;
  baseImageUrl?: string;
  minimumPrice?: string | number;
  price?: string | number;
  specialPrice?: string;
  images?: {
    edges: Array<{
      node: {
        publicPath: string;
      };
    }>;
  };
}

export type ProductsSectionProps = {
  title: string;
  description: string;
  products: ProductSectionNode[];
};

export interface ProductFilterAttributeResponse {
  attribute: {
    id: string;
    code: string;
    options: {
      edges: Array<{
        node: {
          id: string;
          adminName: string;
        };
      }>;
    };
  };
}

export type ProductReview = {
  rating: number;
};

export interface ProductReviewNode {
  __typename: "ProductReview";
  name: string;
  title: string;
  rating: number;
  comment: string;
  createdAt: string;
  images?: {
    url: string;
    reviewId: string;
  }[];
  customer?: {
    name: string;
    imageUrl: string;
  };
}

export interface ProductData {
  urlKey: string;
  variants?: {
    edges?: {
      node?: {
        attributeValues?: {
          edges?: {
            node?: {
              attribute?: {
                code?: string;
              };
              value?: string;
            };
          }[];
        };
        id?: string;
        priceBaseImageUrl?: string;
        name?: string;
        name_id?: string;
        sku?: string;
        type?: string;
        color?: string;
        size?: string;
      };
    }[];
  } | null;
  name?: string;
  price?: { value?: number; currencyCode?: string } | number | null;
  priceHtml?: { currencyCode?: string } | null;
  averageRating?: number;
  type?: string;
  reviewCount?: number;
  minimumPrice?: string;
  specialPrice?: string;
  shortDescription?: string;
  status?: boolean;
  id?: string;
  description?: string;
  configutableData?: {
    attributes?: unknown[];
    index?: unknown[];
  } | null;
}

export interface AttributeType {
  isVisibleOnFront: string;
  id: string;
  code: string;
  adminName: string;
  type: string;
  label?: string;
}

export type additionalDataTypes = {
  attribute: AttributeType;
  id: string;
  code: string;
  label: string;
  value: string | null;
  admin_name: string;
  type: string;
};

// Product review

export interface RatingTypes {
  length?: number;
  value?: number;
  size?: string;
  className?: string;
  onChange?: (value: number) => void;
}

export interface ReviewDatatypes {
  id: string;
  name: string;
  title: string;
  rating: 5;
  status: string;
  comment: string;
  productId: string;
  customerId: string;
  createdAt: string;
  images: {
    url: string;
    reviewId: string;
  }[];
  customer: {
    name: string;
    imageUrl: string;
  };
}

