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

interface Photo {
  photo?: string;
  video?: string;
  url: string;
  type?: string;
}

export type ProductMediaType = 'image' | 'video' | 'pdf' | 'manual' | 'warranty';

export interface ProductMedia {
  id?: string;
  type: ProductMediaType;
  url: string;
  thumbnailUrl?: string;
  alt?: string;
  title?: string;
  labels?: Record<string, string>;
}

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

interface Comment {
  id?: string;
  text: string;
  author?: string;
  stars?: number;
  createdAt?: string;
}

interface ItemTranslation {
  name?: string;
  simpleDescription?: string;
  description?: DescriptionField[];
}

interface Review {
  rating?: number;
  content?: string;
  userID?: string;
  answer?: string;
  timestamp?: string;
}

interface Question {
  question: string;
  answer: string;
  upvotes: number;
  downvotes: number;
  like?: number;
  dislike?: number;
}

/** Localized name entry from backend */
export interface ItemName {
  language: string;
  value: string;
}

/** Localized description entry from backend */
interface ItemDescription {
  language: string;
  value: string;
}

/** Key-value attribute pair */
interface ItemAttribute {
  key: string;
  value: string;
}

interface LocalizedValue {
  [language: string]: string;
}

export interface ProductSpecificationField {
  key: string;
  value: string;
  label?: string;
  labels?: LocalizedValue;
  unit?: string;
}

export interface ProductSpecificationGroup {
  id?: string;
  key: string;
  label?: string;
  labels?: LocalizedValue;
  attributes: ProductSpecificationField[];
}

export interface ProductVariantOption {
  value: string;
  label?: string;
  labels?: LocalizedValue;
  available?: boolean;
}

export interface ProductVariantOptionGroup {
  key: string;
  label?: string;
  labels?: LocalizedValue;
  options: ProductVariantOption[];
}

export interface RelatedProductCollection {
  id: string;
  title: string;
  titles?: LocalizedValue;
  products: number[];
}

export interface DeliveryOption {
  deliveryPrice: number;
  deliveryPlace: string;
  deliveryTime: string;
}

type DeliveryMode = 'selectable' | 'digital';

/** Item variant detail (price, size, colour per variant) */
export interface ItemDetail {
  color?: string;
  colour?: string;
  size?: string;
  price: number;
  deliveryPrice?: number;
  currency: string;
  remaining: number;
}

export interface Item {
  categoryID: number;
  itemID: number;
  name: string;
  photos: Photo[] | null;
  description: string;
  currency: string;
  price: number;
  deliveryPrice?: number;
  discount: number;
  remainings?: string;
  rating: number;
  callbacks: Review[] | null;
  questions: Question[] | null;
  quantity?: number;

  // Backend API fields
  colour?: string;
  size?: string;
  deliveryOptions?: DeliveryOption[];
  deliveryMode?: DeliveryMode;
  deliverySelectionRequired?: boolean;
  language?: string;
  names?: ItemName[];
  descriptions?: ItemDescription[];
  attributes?: ItemAttribute[];
  media?: ProductMedia[];
  specificationGroups?: ProductSpecificationGroup[];
  variantOptions?: ProductVariantOptionGroup[];
  relatedCollections?: RelatedProductCollection[];

  // BackOffice API fields
  id?: string;
  visible?: boolean;
  priority?: number;
  imgs?: string[];
  tags?: string[];
  badges?: string[];
  simpleDescription?: string;
  descriptionFields?: DescriptionField[];
  subcategoryId?: string;
  translations?: Record<string, ItemTranslation>;
  comments?: Comment[];
  visits?: number;
  itemDetails?: ItemDetail[];

  /**
   * 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 CartItem extends Item {
  quantity: number;
  selectedDelivery?: DeliveryOption | null;
}
