import { describe, expect, it } from 'vitest';
import { Hono } from 'hono';
import type { Env } from '../../src/types.js';
import { createApp } from '../../src/app.js';
import { showcaseRoutes } from '../../src/routes/showcase.js';
import { assertMatchesSchema } from '../helpers/schema-assert.js';

function createTestApp() {
  const app = new Hono<Env>();
  app.route('/v1', showcaseRoutes);
  return app;
}

describe('GET /v1/showcase/*', () => {
  it('serves the active showcase manifest with verification metadata', async () => {
    const app = createTestApp();
    const res = await app.request('/v1/showcase/manifest');

    expect(res.status).toBe(200);
    const json = await res.json();
    assertMatchesSchema('showcase-manifest.v1.json', json);
    expect(json.total).toBeGreaterThan(0);
    expect(Array.isArray(json.apps)).toBe(true);
    expect(json.apps.some((entry: { slug: string }) => entry.slug === 'portfolio')).toBe(true);
    const shortlistEntry = json.apps.find((entry: { slug: string }) => entry.slug === 'portfolio');
    expect(shortlistEntry?.url).toBe('/showcase/portfolio');
    expect(shortlistEntry?.thumbnail?.src).toBe('/showcase/thumbnails/portfolio.png');
    expect(shortlistEntry?.thumbnail?.width).toBe(1600);
    expect(shortlistEntry?.verification?.verificationStatus).toBe('smoke-green');
    expect(
      json.apps.every(
        (entry: {
          url?: string;
          verification?: { build?: { passed?: boolean }; smoke?: { passed?: boolean } };
        }) =>
          typeof entry.url === 'string' &&
          entry.url.startsWith('/showcase/') &&
          entry.verification?.build?.passed === true &&
          entry.verification?.smoke?.passed === true,
      ),
    ).toBe(true);
  });

  it('serves the shortlisted showcase set with verification summary', async () => {
    const app = createTestApp();
    const res = await app.request('/v1/showcase/shortlist');

    expect(res.status).toBe(200);
    const json = await res.json();
    assertMatchesSchema('showcase-shortlist.v1.json', json);
    expect(Array.isArray(json.apps)).toBe(true);
    expect(json.summary?.passedBuilds).toBeGreaterThan(0);
    expect(json.summary?.passedSmokes).toBeGreaterThan(0);
    expect(json.apps.every((entry: { goldenCandidate?: string | boolean }) => Boolean(entry.goldenCandidate))).toBe(true);
  });

  it('serves the full shortlist verification report', async () => {
    const app = createTestApp();
    const res = await app.request('/v1/showcase/shortlist-verification');

    expect(res.status).toBe(200);
    const json = await res.json();
    assertMatchesSchema('showcase-shortlist-report.v1.json', json);
    expect(json.$schema).toBe('https://decantr.ai/schemas/showcase-shortlist-report.v1.json');
    expect(Array.isArray(json.results)).toBe(true);
    expect(json.results.length).toBeGreaterThan(0);
  });

  it('remains publicly readable through the full app middleware stack', async () => {
    const app = createApp();
    const res = await app.request('/v1/showcase/shortlist-verification');

    expect(res.status).toBe(200);
    const json = await res.json();
    assertMatchesSchema('showcase-shortlist-report.v1.json', json);
    expect(json.$schema).toBe('https://decantr.ai/schemas/showcase-shortlist-report.v1.json');
  });
});
