import { describe, it } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync, existsSync } from 'node:fs';
import { resolve } from 'node:path';

const pagePath = resolve('src/pages/index.astro');

function readPage() {
  return readFileSync(pagePath, 'utf-8');
}

describe('Index page', () => {
  it('should exist at src/pages/index.astro', () => {
    assert.ok(existsSync(pagePath), 'Index page file should exist');
  });

  describe('Imports', () => {
    it('should import getCollection from astro:content', () => {
      const content = readPage();
      assert.match(content, /import\s+.*getCollection.*from\s+['"]astro:content['"]/, 'should import getCollection');
    });

    it('should import BaseLayout', () => {
      const content = readPage();
      assert.match(content, /import\s+BaseLayout\s+from\s+['"].*BaseLayout\.astro['"]/, 'should import BaseLayout');
    });

    it('should import NavBar', () => {
      const content = readPage();
      assert.match(content, /import\s+NavBar\s+from\s+['"].*NavBar\.astro['"]/, 'should import NavBar');
    });

    it('should import ArticleCard', () => {
      const content = readPage();
      assert.match(content, /import\s+ArticleCard\s+from\s+['"].*ArticleCard\.astro['"]/, 'should import ArticleCard');
    });

    it('should import getReadingTime', () => {
      const content = readPage();
      assert.match(content, /import\s+.*getReadingTime.*from\s+['"].*reading-time['"]/, 'should import getReadingTime');
    });
  });

  describe('Data fetching', () => {
    it('should fetch all posts with getCollection', () => {
      const content = readPage();
      assert.match(content, /getCollection\s*\(\s*['"]posts['"]\s*\)/, 'should call getCollection("posts")');
    });

    it('should sort posts descending by date', () => {
      const content = readPage();
      assert.match(content, /sort\s*\(/, 'should call sort');
      assert.match(content, /b\.data\.date\.getTime\(\)\s*-\s*a\.data\.date\.getTime\(\)/, 'should sort descending by date');
    });
  });

  describe('Template', () => {
    it('should use BaseLayout with title Crash Test Dev', () => {
      const content = readPage();
      assert.match(content, /<BaseLayout\s+title\s*=\s*['"]Crash Test Dev['"]/, 'should have BaseLayout with title');
    });

    it('should include NavBar in nav slot', () => {
      const content = readPage();
      assert.match(content, /<NavBar\s+slot\s*=\s*["']nav["']/, 'should have NavBar with slot="nav"');
    });

    it('should have a section with class post-list and aria-label', () => {
      const content = readPage();
      assert.match(content, /<section\s+class\s*=\s*["']post-list["']/, 'should have section.post-list');
      assert.match(content, /aria-label\s*=\s*["']All posts["']/, 'should have aria-label="All posts"');
    });

    it('should map sortedPosts to ArticleCard components', () => {
      const content = readPage();
      assert.match(content, /sortedPosts\.map/, 'should map sortedPosts');
      assert.match(content, /<ArticleCard/, 'should render ArticleCard');
    });

    it('should pass title from post.data.title', () => {
      const content = readPage();
      assert.match(content, /title\s*=\s*\{post\.data\.title\}/, 'should pass title');
    });

    it('should pass date from post.data.date', () => {
      const content = readPage();
      assert.match(content, /date\s*=\s*\{post\.data\.date\}/, 'should pass date');
    });

    it('should pass type from post.data.type', () => {
      const content = readPage();
      assert.match(content, /type\s*=\s*\{post\.data\.type\}/, 'should pass type');
    });

    it('should pass summary from post.data.summary', () => {
      const content = readPage();
      assert.match(content, /summary\s*=\s*\{post\.data\.summary\}/, 'should pass summary');
    });

    it('should pass slug as post.id', () => {
      const content = readPage();
      assert.match(content, /slug\s*=\s*\{post\.id\}/, 'should pass slug as post.id');
    });

    it('should compute and pass readingTime from post.body', () => {
      const content = readPage();
      assert.match(content, /getReadingTime\s*\(\s*post\.body/, 'should call getReadingTime(post.body...)');
      assert.match(content, /readingTime\s*=\s*\{/, 'should pass readingTime prop');
    });
  });

  describe('Style', () => {
    it('should have .post-list with prose max-width', () => {
      const content = readPage();
      assert.match(content, /\.post-list\s*\{/, 'should have .post-list selector');
      assert.match(content, /max-width\s*:\s*var\(--width-prose\)/, 'should have prose max-width');
    });

    it('should have space-6 top and space-12 bottom margins', () => {
      const content = readPage();
      assert.match(content, /margin-top\s*:\s*var\(--space-6\)/, 'should have space-6 top margin');
      assert.match(content, /margin-bottom\s*:\s*var\(--space-12\)/, 'should have space-12 bottom margin');
    });

    it('should have auto left and right margins', () => {
      const content = readPage();
      assert.match(content, /margin-left\s*:\s*auto/, 'should have auto left margin');
      assert.match(content, /margin-right\s*:\s*auto/, 'should have auto right margin');
    });

    it('should have flex column display', () => {
      const content = readPage();
      assert.match(content, /display\s*:\s*flex/, 'should have flex display');
      assert.match(content, /flex-direction\s*:\s*column/, 'should have column direction');
    });

    it('should have space-2 gap', () => {
      const content = readPage();
      assert.match(content, /gap\s*:\s*var\(--space-2\)/, 'should have space-2 gap');
    });
  });
});