/**
 * Bug 11 — brain_score needs a breakdown + orphan_pages metric is wrong.
 *
 * Assertions:
 *   1. getHealth() returns the new *_score breakdown fields.
 *   2. Breakdown fields sum to brain_score by construction.
 *   3. orphan_pages counts pages with zero INBOUND links, regardless of
 *      whether they have outbound links (was: required both).
 *   4. BrainHealth type now carries dead_links.
 */

import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';

let engine: PGLiteEngine;

beforeAll(async () => {
  engine = new PGLiteEngine();
  await engine.connect({});
  await engine.initSchema();
});

afterAll(async () => {
  await engine.disconnect();
});

beforeEach(async () => {
  for (const t of ['links', 'content_chunks', 'timeline_entries', 'raw_data', 'tags', 'page_versions', 'ingest_log', 'pages']) {
    await (engine as any).db.exec(`DELETE FROM ${t}`);
  }
});

describe('Bug 11 — brain_score breakdown sums to total', () => {
  test('empty brain returns full score (vacuous truth) with all breakdown fields present', async () => {
    // v0.37.10.0: empty brain = no coverage problems = full marks. Pre-fix
    // this returned 0/100, which surprised users running `gbrain doctor`
    // immediately after `gbrain init --pglite`. Each component returns its
    // max weight when pageCount === 0; the sum equals brain_score=100 by
    // construction (same invariant as the non-empty path, see next test).
    const h = await engine.getHealth();
    expect(h.brain_score).toBe(100);
    expect(h.embed_coverage_score).toBe(35);
    expect(h.link_density_score).toBe(25);
    expect(h.timeline_coverage_score).toBe(15);
    expect(h.no_orphans_score).toBe(15);
    expect(h.no_dead_links_score).toBe(10);
    // dead_links is now on the type.
    expect(h.dead_links).toBe(0);
  });

  test('breakdown fields always sum to brain_score', async () => {
    // Seed a small graph — some pages, some links, some embeds.
    for (const slug of ['a', 'b', 'c']) {
      await engine.putPage(slug, { type: 'note', title: slug, compiled_truth: `content of ${slug}`, frontmatter: {} });
    }
    const h = await engine.getHealth();
    const sum =
      h.embed_coverage_score +
      h.link_density_score +
      h.timeline_coverage_score +
      h.no_orphans_score +
      h.no_dead_links_score;
    expect(sum).toBe(h.brain_score);
  });

  test('brain_score caps at 100', async () => {
    const h = await engine.getHealth();
    expect(h.brain_score).toBeGreaterThanOrEqual(0);
    expect(h.brain_score).toBeLessThanOrEqual(100);
  });
});

describe('Bug 11 — orphan_pages is "no inbound links"', () => {
  test('a page with outbound-only links is NOT an orphan', async () => {
    // Hub page: links out to three others, but nothing links back to it.
    // Previous (buggy) behavior: hub counted as orphan because it had no
    // inbound links (correct) AND the old query also required no outbound.
    await engine.putPage('hub', { type: 'note', title: 'Hub', compiled_truth: 'index', frontmatter: {} });
    await engine.putPage('leaf1', { type: 'note', title: 'L1', compiled_truth: 'x', frontmatter: {} });
    await engine.putPage('leaf2', { type: 'note', title: 'L2', compiled_truth: 'y', frontmatter: {} });
    await engine.putPage('leaf3', { type: 'note', title: 'L3', compiled_truth: 'z', frontmatter: {} });

    const hubId = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug='hub'`)).rows[0].id;
    for (const target of ['leaf1', 'leaf2', 'leaf3']) {
      const tid = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug=$1`, [target])).rows[0].id;
      await (engine as any).db.query(
        `INSERT INTO links (from_page_id, to_page_id, link_type) VALUES ($1, $2, 'mentions')`,
        [hubId, tid],
      );
    }

    const h = await engine.getHealth();
    // hub has outbound, no inbound → NOT orphan (under the fixed definition).
    // leaf1/2/3 have inbound from hub → NOT orphan.
    // So orphan_pages should be 0.
    expect(h.orphan_pages).toBe(0);
  });

  test('a page with no links at all IS an orphan', async () => {
    await engine.putPage('loner', { type: 'note', title: 'Loner', compiled_truth: 'alone', frontmatter: {} });
    const h = await engine.getHealth();
    expect(h.orphan_pages).toBe(1);
  });

  test('a page with inbound links only is NOT an orphan', async () => {
    await engine.putPage('sink', { type: 'note', title: 'Sink', compiled_truth: 'target', frontmatter: {} });
    await engine.putPage('source', { type: 'note', title: 'Source', compiled_truth: 'origin', frontmatter: {} });
    const sinkId = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug='sink'`)).rows[0].id;
    const srcId = (await (engine as any).db.query(`SELECT id FROM pages WHERE slug='source'`)).rows[0].id;
    await (engine as any).db.query(
      `INSERT INTO links (from_page_id, to_page_id, link_type) VALUES ($1, $2, 'mentions')`,
      [srcId, sinkId],
    );

    const h = await engine.getHealth();
    // sink has 1 inbound (from source) → not orphan.
    // source has no inbound (but has outbound) → not orphan under new definition.
    expect(h.orphan_pages).toBe(0);
  });
});

describe('Bug 11 — doctor renders brain_score breakdown', () => {
  test('doctor source contains brain_score breakdown rendering', async () => {
    const source = await Bun.file(new URL('../src/commands/doctor.ts', import.meta.url)).text();
    expect(source).toContain('brain_score');
    expect(source).toContain('embed_coverage_score');
    expect(source).toContain('link_density_score');
    expect(source).toContain('no_orphans_score');
    expect(source).toContain('no_dead_links_score');
  });
});

describe('Bug 11 — BrainHealth type shape', () => {
  test('type includes dead_links + breakdown scores', async () => {
    const typesSource = await Bun.file(new URL('../src/core/types.ts', import.meta.url)).text();
    expect(typesSource).toContain('dead_links: number');
    expect(typesSource).toContain('embed_coverage_score: number');
    expect(typesSource).toContain('link_density_score: number');
    expect(typesSource).toContain('timeline_coverage_score: number');
    expect(typesSource).toContain('no_orphans_score: number');
    expect(typesSource).toContain('no_dead_links_score: number');
    // The stale "(0-10)" comment must be corrected to 0-100.
    expect(typesSource).toContain('0-100');
  });
});

describe('linkable scope — archive pages do not drag the score', () => {
  test('islanded raw/ and daily/ pages are excluded from the orphan component', async () => {
    // Curated, connected pages.
    await engine.putPage('people/alice-example', { type: 'person', title: 'Alice', compiled_truth: 'x', frontmatter: {} });
    await engine.putPage('companies/acme-example', { type: 'company', title: 'Acme', compiled_truth: 'x', frontmatter: {} });
    const { rows: ids } = await (engine as any).db.query(
      `SELECT id, slug FROM pages ORDER BY slug`,
    );
    const bySlug = Object.fromEntries(ids.map((r: any) => [r.slug, r.id]));
    await (engine as any).db.query(
      `INSERT INTO links (from_page_id, to_page_id, link_type) VALUES ($1, $2, 'works_at')`,
      [bySlug['people/alice-example'], bySlug['companies/acme-example']],
    );
    // Archive + daily-log pages: no links, no timeline — by design.
    await engine.putPage('raw/whatsapp/2025-01/log-page', { type: 'note', title: 'raw log', compiled_truth: 'x', frontmatter: {} });
    await engine.putPage('deals/acme-seed/raw/transcript', { type: 'note', title: 'raw t', compiled_truth: 'x', frontmatter: {} });
    await engine.putPage('daily/calendar/2025/2025-01-01', { type: 'note', title: 'day', compiled_truth: 'x', frontmatter: {} });

    const h = await engine.getHealth();
    // The three archive/log pages are outside the linkable scope...
    expect(h.linkable_page_count).toBe(2);
    // ...so none of them is an orphan, and the connected pair keeps 15/15.
    expect(h.orphan_pages).toBe(0);
    expect(h.no_orphans_score).toBe(15);
  });

  test('timeline coverage is measured over linkable pages only', async () => {
    await engine.putPage('people/alice-example', { type: 'person', title: 'Alice', compiled_truth: 'x', frontmatter: {} });
    await engine.addTimelineEntry('people/alice-example', { date: '2025-01-01', source: 'note', summary: 'joined' });
    // A raw archive page without timeline must not dilute coverage.
    await engine.putPage('raw/whatsapp/2025-01/log-page', { type: 'note', title: 'raw log', compiled_truth: 'x', frontmatter: {} });

    const h = await engine.getHealth();
    expect(h.linkable_page_count).toBe(1);
    expect(h.timeline_coverage_score).toBe(15); // 1/1 linkable pages covered
  });

  test('an islanded curated page still counts as an orphan', async () => {
    await engine.putPage('people/forgotten-example', { type: 'person', title: 'F', compiled_truth: 'x', frontmatter: {} });
    const h = await engine.getHealth();
    expect(h.orphan_pages).toBe(1);
    expect(h.linkable_page_count).toBe(1);
    expect(h.no_orphans_score).toBe(0);
  });
});
