// A small canned crawl so the UI renders in a plain browser without the Rust
// backend. Real crawls in the Tauri app replace all of this.

import type { CrawlResult, GeoSignals, Issue, Page } from "./types";

const host = "https://acme.example";

const GOOD_GEO: GeoSignals = {
  semanticHtml: true,
  structuredData: true,
  hasAuthor: true,
  hasDate: true,
  faqSchema: false,
  questionHeadings: 2,
  structuredBlocks: 3,
  answerable: true,
  score: 84,
};
const WEAK_GEO: GeoSignals = {
  semanticHtml: false,
  structuredData: false,
  hasAuthor: false,
  hasDate: false,
  faqSchema: false,
  questionHeadings: 0,
  structuredBlocks: 1,
  answerable: false,
  score: 22,
};

function page(p: Partial<Page> & { url: string }): Page {
  return {
    finalUrl: p.url,
    status: 200,
    redirectChain: [],
    contentType: "text/html; charset=utf-8",
    responseTimeMs: 180,
    sizeBytes: 48_000,
    depth: 1,
    server: "vercel",
    contentEncoding: "br",
    cacheControl: "public, max-age=3600",
    xRobotsTag: null,
    hsts: true,
    title: "Acme — Page",
    metaDescription: "An example page description that sits comfortably in range for search snippets.",
    h1: ["Heading"],
    h2Count: 3,
    h3Count: 4,
    wordCount: 640,
    textRatio: 0.32,
    canonical: p.url,
    metaRobots: null,
    lang: "en",
    hasViewport: true,
    indexable: true,
    indexability: null,
    canonicalized: false,
    imagesTotal: 4,
    imagesMissingAlt: 0,
    internalLinks: [],
    externalLinks: [],
    inlinks: 5,
    linkScore: 45,
    seoScore: 82,
    ogTitle: "Acme",
    ogImage: `${host}/og.png`,
    twitterCard: "summary_large_image",
    schemaTypes: ["Organization", "WebPage"],
    hreflang: [],
    mixedContent: 0,
    geo: GOOD_GEO,
    contentHash: null,
    duplicateOf: null,
    error: null,
    ...p,
  };
}

const pages: Page[] = [
  page({ url: `${host}/`, depth: 0, title: "Acme — Modern Tools for Teams", wordCount: 920, inlinks: 9, linkScore: 100, geo: GOOD_GEO }),
  page({ url: `${host}/pricing`, responseTimeMs: 240, schemaTypes: [] }),
  page({ url: `${host}/blog`, title: null, metaDescription: null, h1: [], geo: WEAK_GEO }),
  page({ url: `${host}/blog/launch`, title: "Launch", wordCount: 120, canonical: `${host}/blog`, canonicalized: true, indexable: false, indexability: "Canonicalised", geo: WEAK_GEO }),
  page({ url: `${host}/about`, metaDescription: null, imagesTotal: 6, imagesMissingAlt: 3, geo: { ...GOOD_GEO, hasAuthor: false, score: 74 } }),
  page({ url: `${host}/contact`, depth: 2, responseTimeMs: 2400, schemaTypes: [], geo: WEAK_GEO }),
  page({ url: `${host}/old-page`, status: 404, indexable: false, indexability: "Client Error", title: null, h1: [], canonical: null, geo: WEAK_GEO }),
  page({ url: `${host}/legacy`, status: 301, redirectChain: [{ from: `${host}/legacy`, to: `${host}/new`, status: 301 }], indexable: false, indexability: "Redirect", title: null, h1: [], canonical: null, geo: WEAK_GEO }),
  page({ url: `${host}/docs`, depth: 2, metaRobots: "noindex, follow", indexable: false, indexability: "Noindex" }),
  page({ url: `${host}/careers`, depth: 3, inlinks: 0, title: "Careers at Acme — Join Us", geo: { ...WEAK_GEO, score: 40 } }),
];

const issues: Issue[] = [
  { rule: "client-error", title: "Client Error (4xx)", category: "response", severity: "error", url: `${host}/old-page`, detail: "404" },
  { rule: "broken-link", title: "Broken Link", category: "links", severity: "error", url: `${host}/blog`, detail: "404 → /old-page" },
  { rule: "title-missing", title: "Missing Title", category: "titles-meta", severity: "error", url: `${host}/blog`, detail: null },
  { rule: "redirect", title: "Redirect (3xx)", category: "response", severity: "warning", url: `${host}/legacy`, detail: "301" },
  { rule: "h1-missing", title: "Missing H1", category: "headings", severity: "warning", url: `${host}/blog`, detail: null },
  { rule: "description-missing", title: "Missing Meta Description", category: "titles-meta", severity: "warning", url: `${host}/about`, detail: null },
  { rule: "image-missing-alt", title: "Images Missing Alt Text", category: "images", severity: "warning", url: `${host}/about`, detail: "3 of 6 images" },
  { rule: "noindex", title: "Noindex", category: "indexability", severity: "warning", url: `${host}/docs`, detail: null },
  { rule: "geo-no-structured-data", title: "GEO: No Machine-Readable Structure", category: "geo", severity: "warning", url: `${host}/contact`, detail: null },
  { rule: "slow-response", title: "Slow Response", category: "response", severity: "notice", url: `${host}/contact`, detail: "2400 ms" },
  { rule: "thin-content", title: "Thin Content", category: "content", severity: "notice", url: `${host}/blog/launch`, detail: "120 words" },
  { rule: "canonicalised", title: "Canonicalised", category: "canonical", severity: "notice", url: `${host}/blog/launch`, detail: `${host}/blog` },
  { rule: "orphan", title: "Orphan Page", category: "links", severity: "notice", url: `${host}/careers`, detail: "No internal inlinks" },
  { rule: "geo-not-answerable", title: "GEO: Not Answer-Ready", category: "geo", severity: "notice", url: `${host}/contact`, detail: null },
  { rule: "geo-ready", title: "GEO: AI-Ready Page", category: "geo", severity: "good", url: `${host}/`, detail: "84/100" },
];

export const DEMO_RESULT: CrawlResult = {
  config: {
    url: host,
    mode: "site",
    urls: [],
    maxPages: 500,
    maxDepth: 16,
    concurrency: 16,
    timeoutSecs: 15,
    userAgent: "crawlie/0.1.0",
    checkExternal: true,
    respectRobots: true,
    useSitemap: true,
    include: [],
    exclude: [],
  },
  pages,
  issues,
  summary: {
    totalPages: pages.length,
    errors: issues.filter((i) => i.severity === "error").length,
    warnings: issues.filter((i) => i.severity === "warning").length,
    notices: issues.filter((i) => i.severity === "notice").length,
    good: issues.filter((i) => i.severity === "good").length,
    healthScore: 68,
    geoScore: 57,
    avgResponseMs: 410,
    indexablePages: pages.filter((p) => p.indexable).length,
    duplicatePages: 0,
    byStatus: { "200": 7, "301": 1, "404": 1, "0": 0 },
    byCategory: { "Response Codes": 3, Links: 3, "Titles & Meta": 3, Headings: 1, Images: 1, Indexability: 1, Content: 2, Canonicals: 1, "Generative Engine Optimization": 2 },
    byDepth: { "0": 1, "1": 5, "2": 2, "3": 1 },
    durationMs: 1840,
  },
  robotsFound: true,
  sitemapUrls: 12,
  robotsBlocked: [`${host}/admin`],
  llmsTxtFound: false,
  startedAt: 1781800000000,
};
