import type { IResponse } from "@/types/response.types";

export type ProductVariant = {
  id?: string;
  value: string;
  price: number;
  stock?: number;
};

export type CreateProductType = {
  categoryId: string;
  title: string;
  attribute: string;
  variants: ProductVariant[];
  desc: string;
  price: number;
  isActive: boolean;
  images: string[];
  discount: number;
  isSpecialOffer: boolean;
  order: number;
  /** فقط در حالت بدون تنوع استفاده می‌شود */
  stock?: number;
  maxPurchaseQuantity?: number;
};

export type Category = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  parent: string | null;
  title: string;
  isActive: boolean;
  shop: string;
  avatarUrl: string;
  order: number | null;
  children: Category[];
};

export type ProductDetailsCategory = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  parent: string;
  title: string;
  isActive: boolean;
  shop: string;
  avatarUrl: string;
  order: number;
};

export type ShopScore = {
  scoreAmount: string;
  scoreCredit: string;
  birthdayScore: string;
  purchaseScore: string;
  referrerScore: string;
  registerScore: string;
  purchaseAmount: string;
  marriageDateScore: string;
};

export type ProductDetailsShop = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  name: string;
  slug: string;
  logo: string | null;
  address: string | null;
  menuColor: string | null;
  latitude: number | null;
  longitude: number | null;
  serviceArea: unknown;
  isActive: boolean;
  establishedYear: number | null;
  phone: string;
  instagram: string | null;
  telegram: string | null;
  whatsapp: string | null;
  description: string | null;
  seoTitle: string | null;
  seoDescription: string | null;
  tagNames: unknown;
  images: unknown;
  vat: number;
  domain: string;
  score: ShopScore;
  subscriptionId: string;
  subscriptionEndDate: string;
  subscriptionStartDate: string;
};

export type Inventory = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  product?: string;
  food?: string; // API compatibility
  totalStock: number;
  availableStock: number;
};

export type Product = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  restaurant: string;
  category: ProductDetailsCategory;
  inventory?: Inventory;
  title: string;
  desc: string;
  attribute?: string;
  variants?: ProductVariant[];
  price: number;
  order: number | null;
  isActive: boolean;
  images: string[];
  score?: number;
  discount: number;
  isSpecialOffer: boolean;
  maxPurchaseQuantity?: number;
};

export type GetProductsParams = {
  page?: number;
  limit?: number;
  search?: string;
  orderBy?: string;
  order?: "asc" | "desc";
  categoryId?: string;
  isActive?: boolean;
};

export type ProductDetails = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  shop: ProductDetailsShop;
  category: ProductDetailsCategory;
  inventory?: Inventory;
  title: string;
  desc: string;
  attribute?: string;
  variants?: ProductVariant[];
  price: number | null;
  order: number | null;
  isActive: boolean;
  images: string[];
  score?: number | null;
  discount: number;
  isSpecialOffer: boolean;
  maxPurchaseQuantity?: number;
};

export type PaginationMeta = {
  total: number;
  page: string;
  limit: string;
  totalPages: number;
};

export type GetCategoriesResponseType = IResponse<Category[]>;
export type GetProductsResponseType = IResponse<Product[]> & {
  meta: PaginationMeta;
};
export type GetProductDetailsResponseType = IResponse<ProductDetails>;

export type CreateCategoryType = {
  title: string;
  isActive: boolean;
  avatarUrl: string;
  order: number;
  parentId?: string;
};

export type Icon = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  url: string;
  group: string;
};

export type IconGroup = {
  id: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  name: string;
  icons: Icon[];
};

export type GetIconsResponseType = IResponse<IconGroup[]>;
