import assert from "node:assert/strict";
import {
	mkdtempSync,
	readFileSync,
	rmSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, it } from "vitest";
import { resolveMilestoneFile } from "../paths.js";
import { deriveState } from "../state.js";

describe("smart-entry-draft", () => {
	it("deriveState returns needs-discussion for draft-only milestone", async () => {
		const tmpBase = mkdtempSync(join(tmpdir(), "sf-smart-entry-draft-test-"));
		const sf = join(tmpBase, ".sf");

		// eslint-disable-next-line @typescript-eslint/no-unused-vars
		const _m001dir = join(sf, "milestones", "M001");
		// Use mkdirSync here
		const { mkdirSync } = await import("node:fs");
		mkdirSync(join(sf, "milestones", "M001"), { recursive: true });

		const draftContent = `# M001: Test Milestone — Context\n\n**Status:** Draft\n\nSeed material from a prior discussion.\n`;
		const { writeFileSync } = await import("node:fs");
		writeFileSync(
			join(sf, "milestones", "M001", "M001-CONTEXT-DRAFT.md"),
			draftContent,
		);

		try {
			const state = await deriveState(tmpBase);

			assert.strictEqual(state.phase, "needs-discussion");
			assert.strictEqual(state.activeMilestone?.id, "M001");
		} finally {
			rmSync(tmpBase, { recursive: true, force: true });
		}
	});

	it("resolveMilestoneFile resolves CONTEXT-DRAFT", async () => {
		const tmpBase = mkdtempSync(join(tmpdir(), "sf-smart-entry-draft-test2-"));
		const sf = join(tmpBase, ".sf");

		const { mkdirSync, writeFileSync } = await import("node:fs");
		mkdirSync(join(sf, "milestones", "M001"), { recursive: true });
		writeFileSync(
			join(sf, "milestones", "M001", "M001-CONTEXT-DRAFT.md"),
			"# Draft",
		);

		try {
			const draftFile = resolveMilestoneFile(tmpBase, "M001", "CONTEXT-DRAFT");
			assert.ok(draftFile !== null && draftFile !== undefined);
			assert.ok(draftFile!.endsWith("M001-CONTEXT-DRAFT.md"));
		} finally {
			rmSync(tmpBase, { recursive: true, force: true });
		}
	});

	it("resolveMilestoneFile does NOT resolve CONTEXT when only CONTEXT-DRAFT exists", async () => {
		const tmpBase = mkdtempSync(join(tmpdir(), "sf-smart-entry-draft-test3-"));
		const sf = join(tmpBase, ".sf");

		const { mkdirSync, writeFileSync } = await import("node:fs");
		mkdirSync(join(sf, "milestones", "M001"), { recursive: true });
		writeFileSync(
			join(sf, "milestones", "M001", "M001-CONTEXT-DRAFT.md"),
			"# Draft",
		);

		try {
			const contextFile = resolveMilestoneFile(tmpBase, "M001", "CONTEXT");
			assert.ok(
				contextFile === null || contextFile === undefined,
				`got: "${contextFile}"`,
			);
		} finally {
			rmSync(tmpBase, { recursive: true, force: true });
		}
	});

	it("guided-flow.ts has needs-discussion branch", () => {
		const guidedFlowSource = readFileSync(
			join(import.meta.dirname, "..", "guided-flow.ts"),
			"utf-8",
		);

		assert.ok(
			guidedFlowSource.includes('state.phase === "needs-discussion"'),
		);

		const branchIdx = guidedFlowSource.indexOf(
			'state.phase === "needs-discussion"',
		);
		const branchChunk = guidedFlowSource.slice(branchIdx, branchIdx + 4000);

		assert.ok(branchChunk.includes("discuss_draft"));
		assert.ok(branchChunk.includes("discuss_fresh"));
		assert.ok(branchChunk.includes("skip_milestone"));
		assert.ok(branchChunk.includes("CONTEXT-DRAFT"));
		assert.ok(
			branchChunk.includes("Draft Seed") ||
				branchChunk.includes("draftContent"),
		);
		assert.ok(branchChunk.includes("return"));
	});
});
