import type { TTenderAddress, TTenderDetails } from "@/lib/api/types/Tenders";

export function isNonEmptyTrimmed(value: string | undefined | null): boolean {
  return Boolean(value?.trim());
}

export function isValidPositiveUnix(value: number | undefined | null): boolean {
  return typeof value === "number" && Number.isFinite(value) && value > 0;
}

export function isValidHttpUrl(value: string | undefined | null): boolean {
  const t = value?.trim();
  if (!t) return false;
  try {
    const u = new URL(t);
    return u.protocol === "http:" || u.protocol === "https:";
  } catch {
    return false;
  }
}

export function isValidIsoDateTimeString(
  value: string | undefined | null,
): boolean {
  const t = value?.trim();
  if (!t) return false;
  const ms = Date.parse(t);
  return Number.isFinite(ms);
}

export function hasAddressContent(addr: TTenderAddress | undefined): boolean {
  if (!addr) return false;
  return (
    isNonEmptyTrimmed(addr.street_name) ||
    isNonEmptyTrimmed(addr.city_name) ||
    isNonEmptyTrimmed(addr.postal_zone) ||
    isNonEmptyTrimmed(addr.country_subentity_code) ||
    isNonEmptyTrimmed(addr.country_code)
  );
}

export function hasWinningTendererContent(tender: TTenderDetails): boolean {
  const w = tender.winning_tenderer;
  if (!w) return false;
  if (isNonEmptyTrimmed(w.name)) return true;
  return hasAddressContent(w.address);
}

export function shouldShowLotsSection(tender: TTenderDetails): boolean {
  const lots = tender.lots ?? [];
  return lots.some(
    (lot) =>
      isNonEmptyTrimmed(lot.lot_id) ||
      isNonEmptyTrimmed(lot.title) ||
      isNonEmptyTrimmed(lot.description) ||
      isNonEmptyTrimmed(lot.main_nature_of_contract) ||
      isNonEmptyTrimmed(lot.main_classification) ||
      isNonEmptyTrimmed(lot.main_classification_display) ||
      (typeof lot.estimated_value === "number" &&
        Number.isFinite(lot.estimated_value) &&
        lot.estimated_value > 0) ||
      isNonEmptyTrimmed(lot.currency),
  );
}

export function hasBuyerExtendedDetails(tender: TTenderDetails): boolean {
  const b = tender.buyer_organization;
  return (
    isNonEmptyTrimmed(b.company_id) ||
    isValidHttpUrl(b.website_uri) ||
    isNonEmptyTrimmed(b.contact_name) ||
    isNonEmptyTrimmed(b.contact_telephone) ||
    isNonEmptyTrimmed(b.contact_email) ||
    hasAddressContent(b.address)
  );
}

export function shouldShowDescriptionSection(tender: TTenderDetails): boolean {
  return isNonEmptyTrimmed(tender.description);
}

export function shouldShowTranslationSection(tender: TTenderDetails): boolean {
  return (
    isNonEmptyTrimmed(tender.language) ||
    isNonEmptyTrimmed(tender.notice_language_code)
  );
}

export function shouldShowAiSummarySection(tender: TTenderDetails): boolean {
  return isNonEmptyTrimmed(tender.overall_summary);
}

export function shouldShowExternalLinksBlock(tender: TTenderDetails): boolean {
  return (
    isValidHttpUrl(tender.document_url) ||
    isValidHttpUrl(tender.tender_url) ||
    isValidHttpUrl(tender.buyer_profile_url)
  );
}

export function isValidRegionCode(code: string | undefined | null): boolean {
  const t = code?.trim() ?? "";
  if (t.length < 2) return false;
  return /^[A-Z0-9]{2,10}$/i.test(t);
}
