import { UUID } from '../../../../shared/types/primitive.types';

export type AdminProductStockStatus = 'in_stock' | 'low_stock' | 'out_of_stock';
export type AdminProductSort = 'title' | 'price' | 'priority' | 'stock' | 'updated';
export type AdminProductEditorMode = 'create' | 'edit' | 'duplicate';

export interface AdminProductMedia {
  images: string[];
  gallery: string[];
  videos: string[];
}

export interface AdminProductSpecification {
  key: string;
  value: string;
}

/**
 * Matches the production variant shape: a flat list of attribute-value
 * combinations, each priced per currency, e.g. {color:'0x8B4513', size:'S',
 * price:62560, currency:'RUB', remaining:100}. The admin UI groups rows that
 * share the same attribute combo into one AdminProductVariant with multiple
 * prices, then flattens back to that shape on save (see
 * admin-product-form.factory.ts variantsToBackendRows/variantsFromBackendRows).
 */
export interface AdminProductVariantPrice {
  currency: string;
  price: number;
}

export interface AdminProductVariant {
  id: string;
  /** attribute key -> selected value, e.g. { color: '0x8B4513', size: 'S' } */
  attributes: Record<string, string>;
  sku: string;
  image: string;
  remaining: number;
  prices: AdminProductVariantPrice[];
}

export interface AdminProductVariantAttributeDef {
  /** lowercase, used as the backend field name (e.g. 'color', 'size') */
  key: string;
  label: string;
  isColor: boolean;
  values: string[];
}

export interface AdminProductAttribute {
  key: string;
  value: string;
}

export interface AdminProductTranslation {
  name?: string;
  shortDescription?: string;
  htmlDescription?: string;
  seoTitle?: string;
  seoDescription?: string;
}

export interface AdminProductSeo {
  metaTitle: string;
  metaDescription: string;
  keywords: string;
}

export interface AdminProductReview {
  id: string;
  author: string;
  rating: number;
  text: string;
}

export interface AdminProductQuestion {
  id: string;
  question: string;
  answer?: string;
}

export interface AdminProduct {
  id: string;
  name: string;
  slug: string;
  sku: string;
  barcode: string;
  brand: string;
  categoryId: string;
  visible: boolean;
  archived: boolean;
  priority: number;
  media: AdminProductMedia;
  price: number;
  discount: number;
  currency: string;
  quantity: number;
  stockStatus: AdminProductStockStatus;
  availability: string;
  shortDescription: string;
  htmlDescription: string;
  specifications: AdminProductSpecification[];
  attributes: AdminProductAttribute[];
  variantAttributes: AdminProductVariantAttributeDef[];
  visits: number;
  variants: AdminProductVariant[];
  relatedProductIds: string[];
  translations: Record<string, AdminProductTranslation>;
  seo: AdminProductSeo;
  featured: boolean;
  recommended: boolean;
  isNew: boolean;
  bestseller: boolean;
  badges: string[];
  reviews: AdminProductReview[];
  questions: AdminProductQuestion[];
  createdAt: string;
  updatedAt: string;
  /**
   * Future optional seller ownership (Seller Management preparation).
   * Absent means marketplace-owned, exactly like every product today -
   * nothing reads this field yet, nothing breaks by it being undefined.
   */
  sellerId?: UUID;
}

export interface AdminProductListFilters {
  search: string;
  categoryId: string | null;
  visibility: 'all' | 'visible' | 'hidden';
  stock: 'all' | AdminProductStockStatus;
  includeArchived: boolean;
  sort: AdminProductSort;
  page: number;
  pageSize: number;
}

export interface AdminProductsListResult {
  items: AdminProduct[];
  total: number;
  page: number;
  pageSize: number;
}

export interface AdminProductCategoryOption {
  id: string;
  title: string;
}
