import { assertEquals } from '@std/assert'
import {
  allPostDirs,
  changedPostDirs,
  filterPostDirs,
  type GitRunner,
} from '../src/core/change-detection.ts'

Deno.test('filterPostDirs: extrahiert post-ordner aus dateipfaden (content/posts)', () => {
  const lines = [
    'content/posts/de/a/index.md',
    'content/posts/de/b/image.png',
    'content/posts/de/c/other.md',
    'README.md',
    'app/src/lib/x.ts',
  ]
  assertEquals(
    filterPostDirs(lines, 'content/posts').sort(),
    ['content/posts/de/a', 'content/posts/de/b'],
  )
})

Deno.test('filterPostDirs: respektiert alternativen root (blog/)', () => {
  const lines = [
    'blog/de/x/index.md',
    'blog/en/y/pic.png',
    'content/posts/de/z/index.md',
    'README.md',
  ]
  assertEquals(filterPostDirs(lines, 'blog').sort(), ['blog/de/x', 'blog/en/y'])
})

Deno.test('filterPostDirs: ignoriert _drafts und non-index.md', () => {
  const lines = [
    'content/posts/de/a/index.md',
    'content/posts/de/a/extra.md',
    'content/posts/de/_drafts/x/index.md',
  ]
  assertEquals(filterPostDirs(lines, 'content/posts'), ['content/posts/de/a'])
})

Deno.test('changedPostDirs: nutzt git diff --name-only A..B', async () => {
  const runner: GitRunner = (args) => {
    assertEquals(args[0], 'diff')
    assertEquals(args[1], '--name-only')
    assertEquals(args[2], 'HEAD~1..HEAD')
    return Promise.resolve('content/posts/de/x/index.md\nREADME.md\n')
  }
  const dirs = await changedPostDirs({
    from: 'HEAD~1',
    to: 'HEAD',
    contentRoot: 'content/posts',
    runner,
  })
  assertEquals(dirs, ['content/posts/de/x'])
})

Deno.test('filterPostDirs: extrahiert post-ordner mit sprach-ebene', () => {
  const lines = [
    'content/posts/de/a/index.md',
    'content/posts/en/b/image.png',
    'content/posts/de/c/index.md',
    'README.md',
  ]
  assertEquals(
    filterPostDirs(lines, 'content/posts').sort(),
    ['content/posts/de/a', 'content/posts/de/c', 'content/posts/en/b'],
  )
})

Deno.test('filterPostDirs: ignoriert dateien direkt unter lang-ordner', () => {
  const lines = [
    'content/posts/de/index.md',
    'content/posts/de/README.md',
    'content/posts/de/x/index.md',
  ]
  assertEquals(filterPostDirs(lines, 'content/posts'), ['content/posts/de/x'])
})

Deno.test('filterPostDirs: _drafts unter sprach-ebene wird ignoriert', () => {
  const lines = [
    'content/posts/de/_drafts/x/index.md',
    'content/posts/de/real/index.md',
  ]
  assertEquals(filterPostDirs(lines, 'content/posts'), ['content/posts/de/real'])
})

Deno.test('changedPostDirs: normalisiert contentRoot mit ../-präfix', async () => {
  const runner: GitRunner = () =>
    Promise.resolve('content/posts/de/alpha/index.md\ncontent/posts/en/beta/index.md\n')
  const dirs = await changedPostDirs({
    from: 'HEAD~1',
    to: 'HEAD',
    contentRoot: '../content/posts',
    runner,
  })
  assertEquals(dirs.sort(), ['../content/posts/de/alpha', '../content/posts/en/beta'])
})

Deno.test('changedPostDirs: normalisiert contentRoot mit mehrfachem ../-präfix', async () => {
  const runner: GitRunner = () =>
    Promise.resolve('content/posts/de/alpha/index.md\n')
  const dirs = await changedPostDirs({
    from: 'HEAD~1',
    to: 'HEAD',
    contentRoot: '../../content/posts',
    runner,
  })
  assertEquals(dirs, ['../../content/posts/de/alpha'])
})

Deno.test('changedPostDirs: gibt pfade mit original-contentRoot zurück (CWD-relativ)', async () => {
  const runner: GitRunner = () =>
    Promise.resolve('content/posts/de/alpha/index.md\n')
  const dirs = await changedPostDirs({
    from: 'HEAD~1',
    to: 'HEAD',
    contentRoot: '../content/posts',
    runner,
  })
  assertEquals(dirs, ['../content/posts/de/alpha'])
})

Deno.test('allPostDirs: findet posts in sprach-unterordnern', async () => {
  const tmp = await Deno.makeTempDir()
  try {
    await Deno.mkdir(`${tmp}/de/alpha`, { recursive: true })
    await Deno.writeTextFile(`${tmp}/de/alpha/index.md`, '---\n---')
    await Deno.mkdir(`${tmp}/de/beta`, { recursive: true })
    await Deno.writeTextFile(`${tmp}/de/beta/index.md`, '---\n---')
    await Deno.mkdir(`${tmp}/en/alpha`, { recursive: true })
    await Deno.writeTextFile(`${tmp}/en/alpha/index.md`, '---\n---')
    await Deno.mkdir(`${tmp}/de/_draft`, { recursive: true })
    await Deno.writeTextFile(`${tmp}/de/_draft/index.md`, '---\n---')

    const result = await allPostDirs(tmp)
    assertEquals(
      result.sort(),
      [`${tmp}/de/alpha`, `${tmp}/de/beta`, `${tmp}/en/alpha`].sort(),
    )
  } finally {
    await Deno.remove(tmp, { recursive: true })
  }
})
