import { ApiPaginatedResponse } from '../api-paginated-response.interface';

export type CatalogItemType = 'product' | 'bundle';

export interface Product {
  id: number;
  type: CatalogItemType;
  category_id: number;
  brand_id: number | null;
  slug: string;
  nombre: string;
  descripcion: string;
  precio: string;
  category: string;
  brand: string | null;
  images: string[];
}

export interface ProductAttributeOption {
  id: number;
  validity_time_id?: number | null;
  value: string;
  label: string;
  sort_order: number;
  metadata: Record<string, unknown> | null;
}

export type ProductAttributeType =
  | 'string'
  | 'number'
  | 'boolean'
  | 'select'
  | 'multiselect'
  | 'color'
  | 'image'
  | 'event_date';

export interface ProductAttribute {
  id: number;
  codigo: string;
  nombre: string;
  is_required: boolean;
  allow_multi_select?: boolean;
  show_in_selector?: boolean;
  metadata_schema: Record<string, unknown> | null;
  type: ProductAttributeType;
  options: ProductAttributeOption[];
}

export type InventoryPolicy = 'tracked' | 'unlimited';

export interface CatalogVariantOption {
  value: string;
  label: string;
}

export type CatalogVariantValue =
  | string
  | CatalogVariantOption
  | Array<string | CatalogVariantOption>;

export interface CatalogItemVariant {
  id: number;
  descripcion?: string | null;
  precio?: string;
  event_date_id?: number | null;
  event_date_ids?: number[];
  event_dates?: string[];
  maximum_addable_quantity?: number | null;
  unavailable_message?: string | null;
  minimum_use_date?: string | null;
  maximum_use_date?: string | null;
  effective_minimum_use_date?: string | null;
  effective_maximum_use_date?: string | null;
  values: Record<string, string | string[]>;
}

export interface SelectedCatalogItemVariant extends CatalogItemVariant {
  images: string[];
}

export interface CatalogItemDetail {
  id: number;
  type: CatalogItemType;
  purpose?: 'product' | 'entry';
  category_id: number | null;
  brand_id: number | null;
  slug: string;
  nombre: string;
  descripcion: string | null;
  precio: string;
  category: string | null;
  brand: string | null;
  inventory_policy: InventoryPolicy;
  max_units_per_user?: number | null;
  has_tickets: boolean;
  minimum_use_date: string | null;
  maximum_use_date: string | null;
  attributes: ProductAttribute[];
  variants: CatalogItemVariant[];
  selected_variant?: SelectedCatalogItemVariant;
  maximum_addable_quantity?: number | null;
  images?: string[];
}

export type CatalogProductLayout =
  | 'row'
  | 'column_with_image'
  | 'column_with_cart'
  | 'ticket_selector';
export type CatalogGroupLayout = 'paginated' | 'simple' | 'simple_vertical' | 'carousel' | 'single';

export interface CatalogFeaturedItemVariant {
  id: number;
  descripcion?: string | null;
  precio?: string;
  maximum_addable_quantity?: number | null;
  unavailable_message?: string | null;
  values: Record<string, CatalogVariantValue>;
}

export interface CatalogVariantOptionsResponse {
  variants: CatalogFeaturedItemVariant[];
  selectors: CatalogVariantSelector[];
  selected_values: Record<string, string | string[]>;
  resolved_variant: CatalogFeaturedItemVariant | null;
  valid: boolean;
  available_variant_count: number;
  matching_variant_count: number;
  price_range: {
    minimum: string;
    maximum: string;
  };
}

export interface CatalogVariantSelector {
  key: string;
  label: string;
  options: CatalogVariantValue[];
  enabled: boolean;
}

export interface CatalogFeaturedItem {
  id: number;
  type: CatalogItemType;
  nombre: string;
  descripcion?: string | null;
  precio: number | string;
  image?: string | null;
  maximum_addable_quantity?: number | null;
  unavailable_message?: string | null;
  variants?: CatalogFeaturedItemVariant[];
}

export type CatalogFeaturedItems =
  | ApiPaginatedResponse<CatalogFeaturedItem[]>
  | CatalogFeaturedItem[];

export interface CatalogFeaturedGroup {
  id: number;
  code: string;
  title: string;
  layout: CatalogProductLayout;
  group_layout: CatalogGroupLayout;
  group_order: number;
  items: CatalogFeaturedItems;
}

export interface CatalogCategory {
  id: number;
  nombre: string;
  categoria_id: number | null;
}

export interface CategoryItemsResponse extends ApiPaginatedResponse<CatalogFeaturedItem[]> {
  category: CatalogCategory;
  layout: CatalogProductLayout;
  group_layout: CatalogGroupLayout;
}
