import { Category } from "../../categories/core/_models";
import { ProductOption } from "../../product-options/core/_models";

export interface ProductImage {
  id: string;
  url: string;
  alt?: string;
  order: number;
  type?: string;
  mime_type?: string;
  size?: number;
}

export interface ProductVariant {
  id?: number;
  product_id?: number;
  enabled: boolean;
  fee_percentage: number;
  profit_percentage: number;
  tax_percentage: number;
  stock_limit: number;
  stock_managed: boolean;
  stock_number: number;
  weight: number;
  attributes?: Record<string, any>;
  meta?: Record<string, any>;
  file_ids?: ProductImage[];
  created_at?: string;
  updated_at?: string;
}

export interface Product {
  id: number;
  name: string;
  description?: string;
  design_style?: string;
  enabled: boolean;
  category_ids?: number[];
  categories?: Category[];
  product_categories?: Category[];
  category?: Category;
  product_option_id?: number;
  product_option?: ProductOption;
  total_sold: number;
  type: number;
  sku?: string;
  price?: number;
  status?: string;
  attributes?: Record<string, any>;
  file_ids: ProductImage[];
  explorer_file_ids?: number[];
  explorer_files?: ProductImage[];
  variants?: ProductVariant[];
  product_variants?: ProductVariant[];
  created_at: string;
  updated_at: string;
}

export interface ProductFormData {
  name: string;
  description: string;
  design_style: string;
  enabled: boolean;
  category_ids: number[];
  product_option_id?: number;
  total_sold: number;
  type: number;
  variant_attribute_name?: string;
  file_ids: ProductImage[];
  variants: ProductVariantFormData[];
  explorer_file_ids?: ProductImage[];
  is_delete_latest_explorer_files?: boolean;
  product_cover_image_id?: string;
  sku?: string;
}

export interface ProductVariantFormData {
  id?: number;
  enabled: boolean;
  fee_percentage: number;
  profit_percentage: number;
  tax_percentage: number;
  stock_limit: number;
  stock_managed: boolean;
  stock_number: number;
  weight: number;
  attributes: Record<string, any>;
  meta: Record<string, any>;
  file_ids: ProductImage[];
  gold_price_per_gram?: number;
  factory_fee_percentage?: number;
}

export interface ProductFilters {
  search?: string;
  category_id?: number;
  status?: string;
  min_price?: number;
  max_price?: number;
  page?: number;
  limit?: number;
}

export interface CreateProductRequest {
  name: string;
  description?: string;
  design_style?: string;
  enabled: boolean;
  category_ids: number[];
  product_option_id?: number;
  total_sold: number;
  type: number;
  attributes?: Record<string, any>;
  file_ids?: number[];
  explorer_file_ids?: number[];
  variants?: CreateVariantRequest[];
  sku?: string;
}

export interface UpdateProductRequest {
  id: number;
  name: string;
  description?: string;
  design_style?: string;
  enabled: boolean;
  category_ids: number[];
  product_option_id?: number;
  total_sold: number;
  type: number;
  attributes?: Record<string, any>;
  file_ids?: number[];
  explorer_file_ids?: number[];
  is_delete_latest_explorer_files?: boolean;
  variants?: UpdateVariantRequest[];
  sku?: string;
}

export interface CreateVariantRequest {
  product_id?: number;
  enabled: boolean;
  fee_percentage: number;
  profit_percentage: number;
  tax_percentage: number;
  stock_limit: number;
  stock_managed: boolean;
  stock_number: number;
  weight: number;
  attributes?: Record<string, any>;
  meta?: Record<string, any>;
  file_ids?: number[];
  gold_price_per_gram?: number;
  factory_fee_percentage?: number;
}

export interface UpdateVariantRequest {
  id: number;
  enabled: boolean;
  fee_percentage: number;
  profit_percentage: number;
  tax_percentage: number;
  stock_limit: number;
  stock_managed: boolean;
  stock_number: number;
  weight: number;
  attributes?: Record<string, any>;
  meta?: Record<string, any>;
  file_ids?: number[];
  gold_price_per_gram?: number;
  factory_fee_percentage?: number;
}

export interface ProductsResponse {
  products: Product[] | null;
  total?: number;
  page?: number;
  per_page?: number;
}

export interface ProductResponse {
  product: Product;
}

export interface CreateProductResponse {
  product: Product;
}

export interface UpdateProductResponse {
  product: Product;
}

export interface DeleteProductResponse {
  message: string;
}

export interface ProductVariantsResponse {
  variants: ProductVariant[] | null;
}

export interface CreateVariantResponse {
  variant: ProductVariant;
}

export interface UpdateVariantResponse {
  variant: ProductVariant;
}

export interface DeleteVariantResponse {
  message: string;
}

export const PRODUCT_TYPES = {
  SIMPLE: 0,
  VARIABLE: 1,
  GROUPED: 2,
  EXTERNAL: 3,
} as const;

export const PRODUCT_TYPE_LABELS = {
  [PRODUCT_TYPES.SIMPLE]: "محصول ساده",
  [PRODUCT_TYPES.VARIABLE]: "محصول متغیر",
  [PRODUCT_TYPES.GROUPED]: "محصول گروهی",
  [PRODUCT_TYPES.EXTERNAL]: "محصول خارجی",
} as const;
