import type { Brand, BrandProduct, Opportunity, ProductMatch, OpportunityReason } from "../../domain/types";

export const productRadarBrandA: Brand = {
  id: "brand-a", display_name: "晴日居家", brief: "居家修繕服務", target_audience: "忙碌的租屋與自住家庭",
};
export const productRadarBrandB: Brand = {
  id: "brand-b", display_name: "遠方工作室", brief: "遠端工作工具", target_audience: "小型團隊",
};
export const productRadarProducts: BrandProduct[] = [
  { id: "product-a1", brand_id: "brand-a", label: "安心抓漏", product_context: "協助處理屋內漏水與抓漏", pain_points: ["漏水", "壁癌"], match_tags: ["水管", "天花板"], provider_capability_terms: ["抓漏", "防水"], provider_exclude_terms: ["徵才"], created_at: 1, updated_at: 2 },
  { id: "product-a2", brand_id: "brand-a", label: "浴室翻新", product_context: "改善老舊浴室的使用情境", pain_points: ["浴室老舊", "收納不足"], match_tags: ["浴室", "翻修"], provider_capability_terms: ["泥作", "衛浴"], provider_exclude_terms: [], created_at: 1, updated_at: 2 },
  { id: "product-b1", brand_id: "brand-b", label: "協作工具", product_context: "讓小型團隊集中管理遠端工作", pain_points: ["協作混亂"], match_tags: ["遠端", "團隊"], provider_capability_terms: ["專案管理"], provider_exclude_terms: [], created_at: 1, updated_at: 2 },
];

const reasons = (fit = 10): OpportunityReason[] => [
  { dimension: "authenticity", score: 20, reason: "個人公開需求" },
  { dimension: "intent", score: 20, reason: "明確詢問解法" },
  { dimension: "region", score: 10, reason: "地區未明確，保留未知" },
  { dimension: "freshness", score: 20, reason: "近期貼文" },
  { dimension: "fit", score: fit, reason: "與目前服務有關" },
];

const match = (productId: string, score: number, opts: Partial<ProductMatch> = {}): ProductMatch => ({
  brand_id: productId === "product-b1" ? "brand-b" : "brand-a",
  product_id: productId,
  brand_name_snapshot: productId === "product-b1" ? "遠方工作室" : "晴日居家",
  product_label_snapshot: productRadarProducts.find((p) => p.id === productId)?.label ?? productId,
  brand_updated_at: 2,
  product_updated_at: 2,
  product_fit_score: score,
  product_fit_band: score >= 75 ? "strong" : score >= 45 ? "possible" : "weak",
  eligible: score >= 45 && !opts.excluded,
  excluded: false,
  reasons: [
    { dimension: "pain", score: Math.min(35, score), reason: score ? "貼文描述對應痛點" : "沒有痛點證據", candidate_excerpt: score ? "家裡一直漏水" : undefined, product_basis: "漏水" },
    { dimension: "scenario", score: score >= 45 ? 20 : 0, reason: score >= 45 ? "有明確使用情境" : "沒有情境證據", candidate_excerpt: score >= 45 ? "想找人處理" : undefined, product_basis: "居家修繕" },
    { dimension: "audience", score: score >= 60 ? 15 : 0, reason: score >= 60 ? "文字支持目標受眾" : "受眾不確定", product_basis: "自住家庭" },
    { dimension: "capability", score: score >= 60 ? 15 : 0, reason: score >= 60 ? "能力詞可處理需求" : "能力證據不足", product_basis: "抓漏" },
  ],
  risks: opts.excluded ? ["明確命中排除詞"] : [],
  watch_ids: ["watch-a"], matched_terms: ["找抓漏"], matched_at: 5,
  ...opts,
});

const NOW = Date.parse("2026-08-10T12:00:00Z") * 1_000_000;
const base = (id: string, text: string, score: number, postedAt: number): Opportunity => ({
  id, watch_id: "watch-a", source: "threads", external_id: `threads:${id}`, permalink: `https://www.threads.net/@u/post/${id}`,
  author_handle: "seeker", text, posted_at: postedAt, status: "qualified", intent_score: score,
  intent_band: score >= 80 ? "high" : score >= 50 ? "mid" : "low", reasons: reasons(score >= 80 ? 15 : 10),
  region_match: "unknown", freshness_hours: Math.max(1, Math.round((NOW - postedAt) / 3_600_000_000_000)), matched_terms: ["需求"], created_at: postedAt,
});

/** Fixed examples cover the accepted Phase B scenarios, not random generated data. */
export const productRadarOpportunityFixtures: Opportunity[] = [
  { ...base("multi", "家裡漏水又想一起翻新浴室，有推薦嗎？", 92, NOW - 3_600_000_000_000), product_matches: [match("product-a1", 88), match("product-a2", 76)], primary_brand_id: "brand-a", primary_product_id: "product-a1", primary_brand_name: "晴日居家", primary_product_label: "安心抓漏", primary_product_fit_score: 88, primary_product_fit_band: "strong" },
  { ...base("stale", "二十天前的漏水求助仍可查看", 86, NOW - 20 * 86_400_000_000_000), product_matches: [match("product-a1", 90)] },
  { ...base("excluded", "徵才：尋找抓漏師傅加入團隊", 84, NOW - 8_000_000_000_000), product_matches: [match("product-a1", 80, { excluded: true, eligible: false, exclude_reason: "命中徵才排除詞" })] },
  { ...base("pain-only", "天花板一直滴水，該怎麼處理？", 78, NOW - 4_000_000_000_000), product_matches: [match("product-a1", 72)] },
  { ...base("same-name", "有人推薦安心抓漏這個名字嗎？", 45, NOW - 5_000_000_000_000), product_matches: [match("product-a1", 20)] },
  { ...base("legacy", "請問有人推薦水電嗎？", 82, NOW - 2_000_000_000_000), product_matches: [], primary_product_id: undefined },
];

export const productRadarSeed = {
  brands: [productRadarBrandA, productRadarBrandB],
  products: productRadarProducts,
  opportunities: productRadarOpportunityFixtures,
  profile: { exists: true },
};
