import { z } from "zod"

export const rawListingSchema = z.object({
  source: z.enum([
    "BRUNO_FRITSCH",
    "CLICAR",
    "YAPO",
    "MARITANO",
    "SALFA",
    "SALAZAR_ISRAEL",
    "PORTILLO_SUR",
    "SERGIO_ESCOBAR"
  ]),
  externalId: z.string(),
  url: z.string().url(),
  title: z.string(),
  priceClp: z.number().int().positive(),
  year: z.number().int().min(1990).max(2100),
  mileageKm: z.number().int().nonnegative().optional(),
  imageUrl: z.string().url().optional(),
  region: z.string().optional(),
  commune: z.string().optional(),
  sellerName: z.string().optional(),
  transmissionText: z.string().optional(),
  fuelText: z.string().optional(),
  rawPayload: z.unknown().optional()
})

export type RawListing = z.infer<typeof rawListingSchema>

export interface NormalizedListing {
  source: RawListing["source"]
  externalId: string
  sourceUrl: string
  title: string
  make: string
  model: string
  version?: string
  year: number
  priceClp: number
  mileageKm?: number
  imageUrl?: string
  transmission: "MANUAL" | "AUTOMATIC" | "CVT" | "DCT" | "UNKNOWN"
  fuel: "GASOLINE" | "DIESEL" | "HYBRID" | "ELECTRIC" | "LPG" | "UNKNOWN"
  region?: string
  commune?: string
  sellerName?: string
  rawPayload?: unknown
  comparisonKey: string
  normalizedLabel: string
}

export interface SourceScraper {
  source: RawListing["source"]
  scrape(): Promise<RawListing[]>
}
