import { afterEach, test, expect } from "bun:test"
import path from "path"
import { Skill } from "../../src/skill"
import { Instance } from "../../src/project/instance"
import { BUILTIN_SKILLS } from "../../src/overlay/skills/builtin"
import { disposeAllInstances, tmpdir } from "../fixture/fixture"

afterEach(async () => {
  await disposeAllInstances()
})

test("built-in skills are present in empty project", async () => {
  await using tmp = await tmpdir({ git: true })

  await Instance.provide({
    directory: tmp.path,
    fn: async () => {
      const skills = await Skill.all()
      for (const builtin of BUILTIN_SKILLS) {
        const found = skills.find((s) => s.name === builtin.name)
        expect(found).toBeDefined()
        expect(found!.location).toBe(Skill.BUILTIN_LOCATION)
        expect(found!.description).toBe(builtin.description)
        expect(found!.content.length).toBeGreaterThan(0)
      }
    },
  })
})

test("built-in skill has correct metadata", async () => {
  await using tmp = await tmpdir({ git: true })

  await Instance.provide({
    directory: tmp.path,
    fn: async () => {
      const skill = await Skill.get("saeeol-config")
      expect(skill).toBeDefined()
      expect(skill!.name).toBe("saeeol-config")
      expect(skill!.location).toBe(Skill.BUILTIN_LOCATION)
      expect(skill!.content).toContain("saeeol")
    },
  })
})

test("user skill overrides built-in with same name", async () => {
  await using tmp = await tmpdir({
    git: true,
    init: async (dir) => {
      const skillDir = path.join(dir, ".saeeol", "skill", "saeeol-config")
      await Bun.write(
        path.join(skillDir, "SKILL.md"),
        `---
name: saeeol-config
description: User override of saeeol-config.
---

# Custom saeeol-config

User-provided content.
`,
      )
    },
  })

  await Instance.provide({
    directory: tmp.path,
    fn: async () => {
      const skill = await Skill.get("saeeol-config")
      expect(skill).toBeDefined()
      expect(skill!.description).toBe("User override of saeeol-config.")
      expect(skill!.location).not.toBe(Skill.BUILTIN_LOCATION)
      expect(skill!.location).toContain(path.join("skill", "saeeol-config", "SKILL.md"))
    },
  })
})
