/** API response shapes — mirror docs/api-contract.md §1 (and the website/admin TS types). */
import type { Catalogue, ChemicalInfo, FormIconKey, InfoKey, Lang, Review } from './site';

export type Ref = { slug: string; name: string };
/** Pack image. `srcset`: resized WebP copies (200w / 400w / 800w) made at upload (SEO Phase 10). */
export type PackImage = {
  url: string;
  credit: string;
  date: string;
  width?: number;
  height?: number;
  srcset?: { url: string; width: number }[];
};

export type BrandSummary = {
  id: string;
  slug: string;
  name: string;
  form: string;
  formIcon: FormIconKey;
  strength: string;
  catalogue: Catalogue;
  genericSlug: string;
  companySlug: string;
  generic: Ref;
  company: Ref;
  pack: string | null;
  addedAt: string;
  popularity: number;
};

/** Quick summary written by a pharmacist (SEO Phase 8). Empty strings / null = not written yet. */
export type QuickSummary = {
  use: Lang;
  adultDose: Lang;
  foodTiming: 'before' | 'after' | 'with' | 'any' | null;
  prescription: 'required' | 'not_required' | null;
};

/** One row of the "Other brands of {generic}" price table (same generic, strength and form). */
export type PriceRow = {
  slug: string;
  name: string;
  company: Ref;
  pack: string | null;
  unitPrice: number | null;
  packPrice: number | null;
  /** the brand whose page this is */
  current: boolean;
};

export type BrandDetail = BrandSummary & {
  unitPrice: number | null;
  packPrice: number | null;
  priceUpdated: string | null;
  priceSource: string;
  packImage: PackImage | null;
  packImages?: PackImage[];
  status: 'published' | 'discontinued';
  siblings: { slug: string; strength: string }[];
  info: Record<InfoKey, Lang & { source: 'brand' | 'generic' }>;
  genericDetail: {
    slug: string;
    name: string;
    classSlug: string;
    className: string;
    review: Review;
    sources: string[];
    structureImageUrl?: string | null;
    chemicalInfo?: ChemicalInfo | null;
  } | null;
  otherBrands: BrandSummary[];
  /** every brand with the same generic + strength + form, cheapest first (prices: brand pages only) */
  alternatives: PriceRow[];
  alternativesTotal?: number;
  summary: QuickSummary | null;
  /** links to related pages (SEO Phase 9) */
  dosageForm: Ref | null;
  conditions: Ref[];
};

export type GenericSummary = {
  id: string;
  slug: string;
  name: string;
  classSlug: string;
  className: string;
  tagline?: string;
  brandCount: number;
};
export type Facet = { value: string; label: string; count: number };
export type CatalogueProduct = {
  slug: string;
  name: string;
  catalogue: 'vet' | 'agro';
  type: string;
  animalsOrCrops: string[];
  form: string;
  formIcon: FormIconKey;
  strength: string;
  genericName: string;
  companySlug: string;
  company: Ref;
};

/** Vet or agro product page (SEO Phase 12). Prices appear only here, like brand pages. */
export type CatalogueProductDetail = {
  slug: string;
  name: string;
  catalogue: 'vet' | 'agro';
  type: Ref | null;
  animalsOrCrops: Ref[];
  form: string;
  formIcon: string;
  strength: string;
  genericName: string;
  company: Ref;
  pack: string | null;
  unitPrice: number | null;
  packPrice: number | null;
  priceUpdated: string | null;
  packImage: PackImage | null;
  withdrawalPeriod: string | null;
  targetPest: string | null;
  doseRate: string | null;
  updatedAt: string | null;
};
