import type { BrandProfile, Project } from "@/types/workflow";
import type { BrandIntelligence } from "./types";
import type { WebsiteIntelligence } from "@/lib/context/website-intelligence";

/** Build structured brand understanding from project, brand profile, and scraped website. */
export function buildBrandIntelligence(
  brand: BrandProfile,
  project: Project,
  website?: WebsiteIntelligence | null,
): BrandIntelligence {
  const companyName =
    brand.companyName?.trim() ||
    website?.companyName?.trim() ||
    project.name?.trim() ||
    "Your Company";
  const industry =
    brand.industry?.trim() || project.industry?.trim() || "your industry";
  const tagline =
    brand.tagline?.trim() ||
    website?.tagline?.trim() ||
    website?.description?.trim() ||
    `${companyName} — ${website?.title || industry}`;
  const voice = brand.brandVoice?.trim() || "Professional";
  const markets = project.countries?.length
    ? project.countries.join(", ")
    : "your target markets";

  const mission = website?.description?.trim() || tagline;

  const usp =
    website?.services?.[0]?.detail ||
    website?.primaryService ||
    brand.tagline?.trim() ||
    "";

  return {
    companyName,
    tagline,
    industry,
    brandVoice: voice,
    website: project.website?.trim() || website?.url || "Not provided",
    targetMarkets: markets,
    timezone: project.timezone || "UTC",
    mission,
    brandPromise: tagline,
    uniqueSellingProposition: usp,
    toneOfVoice: voice,
    writingStyle: voiceToWritingStyle(voice),
    primaryService: website?.primaryService,
    services: website?.services,
    websiteScraped: Boolean(website?.ok),
  };
}

function voiceToWritingStyle(voice: string): string {
  const norm = voice.toLowerCase();
  if (norm.includes("friendly") || norm.includes("warm")) {
    return "Conversational, approachable, uses contractions naturally, short paragraphs.";
  }
  if (norm.includes("bold") || norm.includes("energetic")) {
    return "Direct hooks, confident statements, varied sentence rhythm, minimal filler.";
  }
  if (norm.includes("technical") || norm.includes("expert")) {
    return "Precise language, teach one useful idea per post, respect reader intelligence.";
  }
  return "Professional but human — clear, grounded, no hype or AI clichés.";
}

export function formatBrandIntelligenceMarkdown(brand: BrandIntelligence): string {
  return [
    "## Brand Intelligence",
    `Company: ${brand.companyName}`,
    `Tagline: ${brand.tagline}`,
    `Industry: ${brand.industry}`,
    `Brand voice: ${brand.brandVoice}`,
    `Website: ${brand.website}`,
    `Target markets: ${brand.targetMarkets}`,
    `Mission: ${brand.mission || "— (set website or tagline)"}`,
    `Brand promise: ${brand.brandPromise}`,
    `USP: ${brand.uniqueSellingProposition || "— (scraped services not available)"}`,
    brand.primaryService ? `Primary service (website): ${brand.primaryService}` : "",
    brand.services?.length
      ? `Services:\n${brand.services.map((s) => `- ${s.label}: ${s.detail}`).join("\n")}`
      : "",
    brand.websiteScraped ? "Website data: scraped and active" : "",
    `Writing style: ${brand.writingStyle}`,
  ]
    .filter(Boolean)
    .join("\n");
}
