import { describe, it, expect } from 'vitest';
import { renderJson, renderTable, renderMarkdown } from './render';
import { buildSummary } from './summary';
import type { FetchedStats } from './github';

const fetched: FetchedStats = {
  repo: {
    repo: { owner: 'vercel', name: 'next.js' },
    description: 'The React Framework',
    primaryLanguage: 'TypeScript',
    license: 'MIT',
    stars: 130000,
    forks: 28000,
    watchers: 4200,
    openIssues: 2500,
    defaultBranch: 'canary',
    topics: ['react', 'framework', 'ssr'],
    createdAt: '2016-10-05T00:00:00Z',
    updatedAt: '2026-05-30T00:00:00Z',
    pushedAt: '2026-06-01T00:00:00Z',
    sizeKb: 1024 * 256,
    hasWiki: false,
    hasPages: true,
    archived: false,
    homepage: 'https://nextjs.org',
  },
  issues: [
    {
      number: 1, title: 'Crash on startup', state: 'open', user: 'alice',
      createdAt: '2026-05-30T00:00:00Z', updatedAt: '2026-05-31T00:00:00Z',
      comments: 12, labels: ['bug', 'help wanted'], pullRequest: false,
    },
    {
      number: 2, title: 'Feature request: dark mode', state: 'open', user: 'bob',
      createdAt: '2026-05-25T00:00:00Z', updatedAt: '2026-05-30T00:00:00Z',
      comments: 4, labels: ['enhancement'], pullRequest: false,
    },
  ],
  pulls: [
    {
      number: 100, title: 'Fix crash', state: 'merged', user: 'carol',
      createdAt: '2026-05-29T00:00:00Z', updatedAt: '2026-05-30T00:00:00Z',
      mergedAt: '2026-05-30T00:00:00Z', draft: false, reviewComments: 3,
      additions: 50, deletions: 10, changedFiles: 4,
    },
  ],
  releases: [
    { tagName: 'v15.0.0', name: 'v15', publishedAt: '2026-05-01T00:00:00Z',
      draft: false, prerelease: false, author: 'vercel' },
    { tagName: 'v14.2.5', name: 'v14.2.5', publishedAt: '2026-04-01T00:00:00Z',
      draft: false, prerelease: false, author: 'vercel' },
  ],
  contributors: [
    { login: 'timneutkens', contributions: 5000 },
    { login: 'ijjk', contributions: 1500 },
    { login: 'shadcn', contributions: 800 },
  ],
};

const summary = buildSummary(fetched);

describe('renderJson', () => {
  it('produces a JSON object that round-trips through JSON.parse', () => {
    const out = renderJson(fetched);
    const parsed = JSON.parse(out);
    expect(parsed.repo.repo.owner).toBe('vercel');
    expect(parsed.contributors).toHaveLength(3);
  });
});

describe('renderTable', () => {
  it('includes the owner/name and key metrics', () => {
    const out = renderTable(summary);
    expect(out).toContain('vercel/next.js');
    expect(out).toContain('stars:');
    expect(out).toContain('forks:');
    expect(out).toContain('open issues:');
  });
  it('lists recent issues and PRs', () => {
    const out = renderTable(summary);
    expect(out).toMatch(/#1 \[open\]/);
    expect(out).toMatch(/#100 \[merged\]/);
  });
});

describe('renderMarkdown', () => {
  it('produces a markdown heading and a bullet list of contributors', () => {
    const out = renderMarkdown(summary);
    expect(out).toMatch(/^# vercel\/next\.js/m);
    expect(out).toContain('@timneutkens');
  });
});
