import { apiGetPublic } from "@/lib/api/client";
import type { CafeDiscoverProfile } from "@/lib/cafe-discover-profile";

export type CafeDiscoverBadge = {
  key: string;
  label: string;
  icon: string;
};

export type DaySchedule = {
  isOpen: boolean;
  open: string | null;
  close: string | null;
};

export type WorkingHours = {
  sat: DaySchedule | null;
  sun: DaySchedule | null;
  mon: DaySchedule | null;
  tue: DaySchedule | null;
  wed: DaySchedule | null;
  thu: DaySchedule | null;
  fri: DaySchedule | null;
};

export type PublicCafeDiscover = {
  id: string;
  name: string;
  slug: string;
  city: string | null;
  address: string | null;
  logoUrl: string | null;
  coverImageUrl: string | null;
  isVerified: boolean;
  averageRating: number;
  reviewCount: number;
  discoverProfile: CafeDiscoverProfile;
  badges?: CafeDiscoverBadge[];
  galleryUrls: string[];
  isOpenNow: boolean;
  instagramHandle: string | null;
  websiteUrl: string | null;
  relevanceScore: number;
};

export type NlpHints = {
  themes: string[];
  vibes: string[];
  occasions: string[];
  spaceFeatures: string[];
  noiseLevel: string | null;
  priceTier: string | null;
  size: string | null;
};

export type DiscoverTaxonomy = {
  themes: string[];
  sizes: string[];
  floors: string[];
  vibes: string[];
  occasions: string[];
  spaceFeatures: string[];
  noiseLevels: string[];
  priceTiers: string[];
};

export type DiscoverSearchParams = {
  city?: string;
  q?: string;
  minRating?: number;
  sort?: string;
  themes?: string[];
  vibes?: string[];
  occasions?: string[];
  spaceFeatures?: string[];
  noise?: string;
  priceTier?: string;
  size?: string;
  requireProfile?: boolean;
  openNow?: boolean;
};

function toQuery(params: DiscoverSearchParams): string {
  const q = new URLSearchParams();
  if (params.city) q.set("city", params.city);
  if (params.q) q.set("q", params.q);
  if (params.minRating != null) q.set("minRating", String(params.minRating));
  if (params.sort) q.set("sort", params.sort);
  if (params.themes?.length) q.set("themes", params.themes.join(","));
  if (params.vibes?.length) q.set("vibes", params.vibes.join(","));
  if (params.occasions?.length) q.set("occasions", params.occasions.join(","));
  if (params.spaceFeatures?.length) q.set("spaceFeatures", params.spaceFeatures.join(","));
  if (params.noise) q.set("noise", params.noise);
  if (params.priceTier) q.set("priceTier", params.priceTier);
  if (params.size) q.set("size", params.size);
  if (params.requireProfile !== false) q.set("requireProfile", "true");
  if (params.openNow) q.set("openNow", "true");
  const s = q.toString();
  return s ? `?${s}` : "";
}

export async function fetchPublicDiscover(
  params: DiscoverSearchParams
): Promise<PublicCafeDiscover[]> {
  return apiGetPublic<PublicCafeDiscover[]>(`/api/public/discover${toQuery(params)}`);
}

export async function fetchDiscoverTaxonomy(): Promise<DiscoverTaxonomy> {
  return apiGetPublic<DiscoverTaxonomy>("/api/public/discover-profile/taxonomy");
}

export type PublicCafeReview = {
  id: string;
  authorName: string;
  rating: number;
  comment: string | null;
  ownerReply: string | null;
  createdAt: string;
};

export async function fetchPublicCafeReviews(slug: string) {
  return apiGetPublic<PublicCafeReview[]>(
    `/api/public/cafes/${encodeURIComponent(slug)}/reviews?pageSize=20`
  );
}

export type PublicCafeDetail = {
  id: string;
  name: string;
  slug: string;
  city: string | null;
  address: string | null;
  phone: string | null;
  logoUrl: string | null;
  coverImageUrl: string | null;
  description: string | null;
  averageRating: number;
  reviewCount: number;
  discoverProfile: CafeDiscoverProfile;
  galleryUrls: string[];
  isOpenNow: boolean;
  instagramHandle: string | null;
  websiteUrl: string | null;
  workingHours: WorkingHours | null;
};

export async function fetchPublicCafe(slug: string): Promise<PublicCafeDetail> {
  return apiGetPublic<PublicCafeDetail>(`/api/public/cafes/${encodeURIComponent(slug)}`);
}

export async function fetchNlpHints(q: string): Promise<NlpHints> {
  return apiGetPublic<NlpHints>(`/api/public/discover/nlp-parse?q=${encodeURIComponent(q)}`);
}
