import * as crypto from "crypto"
import * as fs from "fs/promises"
import * as path from "path"
import * as ts from "typescript"
import { pathToFileURL } from "url"
import { z } from "zod"
import { diracHomeDir } from "@/shared/config/environment"
import { getErrorMessage } from "@/shared/errors"
import type { WorkspaceCodeSnapshot } from "@/core/security/WorkspaceCodeApproval"
import { safeParseJson } from "@/shared/safe-json-parse"
import { Logger } from "@/shared/services/Logger"
import type { DiracToolSpec } from "@/shared/tools"
import type { IDiracTool } from "../interfaces/IDiracTool"
import { CONFIGURABLE_TOOL_EXPOSURE, type DiscoveredTool, type ToolSource } from "./DiscoveredTool"

export interface UserToolLoadResult {
	tool?: DiscoveredTool
	error?: string
}

const LOADER_VERSION = "user-tool-loader-v1"
const TOOL_ID_PATTERN = /^[a-z][a-z0-9_]*$/

const userToolManifestSchema = z.object({
	schemaVersion: z.number().int().min(1),
	id: z.string().regex(TOOL_ID_PATTERN, "Manifest id must be a snake_case identifier."),
	name: z.string().regex(TOOL_ID_PATTERN, "Manifest name must be a snake_case identifier."),
	scope: z.enum(["global", "workspace", "task"]),
	entry: z.literal("tool.ts"),
	createdBy: z.literal("dirac"),
	createdAt: z.string().optional(),
	description: z.string().optional(),
	// Passed through unvalidated: `instruction` may be a function, which no JSON manifest can carry,
	// and these parameters are display-only until the tool module itself is loaded and validated.
	parameters: z.custom<DiracToolSpec["parameters"]>().optional(),
})

type UserToolManifest = z.infer<typeof userToolManifestSchema>

interface UserToolModule {
	spec?: DiracToolSpec
	create?: (config?: any) => IDiracTool
}

export class UserToolLoader {
	static async load(toolDir: string, source: ToolSource): Promise<DiscoveredTool | undefined> {
		const result = await this.loadWithDiagnostics(toolDir, source)
		return result.tool
	}

	/** Manifest-only descriptor: never transpiles or imports workspace code. */
	static async describe(toolDir: string, source: ToolSource): Promise<DiscoveredTool | undefined> {
		try {
			const manifest = await this.readManifest(toolDir, source)
			const modulePath = path.join(toolDir, manifest.entry)
			const sourceCode = await fs.readFile(modulePath, "utf8")
			return {
				id: manifest.id,
				name: manifest.name,
				source,
				exposure: CONFIGURABLE_TOOL_EXPOSURE,
				spec: {
					id: manifest.id,
					name: manifest.name,
					description: manifest.description || "Review this tool's source before enabling it.",
					parameters: manifest.parameters || [],
				},
				factory: () => { throw new Error(`Unapproved workspace tool '${manifest.id}' cannot run.`) },
				modulePath,
				sourceHash: this.hashToolSource(modulePath, `${JSON.stringify(manifest)}\0${sourceCode}`),
				executable: false,
			}
		} catch (error) {
			Logger.warn(`[UserToolLoader] Skipping invalid user tool at '${toolDir}'.`, error)
			return undefined
		}
	}

	static async loadWithDiagnostics(toolDir: string, source: ToolSource, approvedSource?: WorkspaceCodeSnapshot): Promise<UserToolLoadResult> {
		try {
			const manifest = await this.readManifest(toolDir, source, approvedSource?.manifestSource)

			const sourcePath = path.join(toolDir, manifest.entry)
			const sourceCode = approvedSource ? approvedSource.source.toString("utf8") : await fs.readFile(sourcePath, "utf8")
			const sourceHash = this.hashToolSource(sourcePath, sourceCode)
			const compiledPath = await this.compileTool(manifest.id, sourceCode, sourceHash)

			let mod: Required<UserToolModule>
			try {
				mod = await this.importCompiledTool(compiledPath)
			} catch (importError) {
				if (importError instanceof SyntaxError) {
					try {
						const compiledContent = await fs.readFile(compiledPath, "utf8")
						const nonAscii = [...compiledContent].filter((c) => c.charCodeAt(0) > 127)
						Logger.warn(`[UserToolLoader] SyntaxError importing compiled tool. Non-ASCII chars: ${nonAscii.length}`)
						if (nonAscii.length > 0) {
							Logger.warn(`[UserToolLoader] Non-ASCII codepoints: ${nonAscii.map((c) => `U+${c.charCodeAt(0).toString(16).padStart(4, "0")}`).join(", ")}`)
						}
						Logger.verbose(`[UserToolLoader] Compiled output (first 500 chars):\n${compiledContent.substring(0, 500)}`)
					} catch {
						// Best-effort logging; swallow if we can't read the file
					}
				}
				throw importError
			}

			this.validateModule(manifest, mod)

			return {
				tool: {
					id: manifest.id,
					name: manifest.name,
					source,
					exposure: CONFIGURABLE_TOOL_EXPOSURE,
					spec: mod.spec,
					factory: mod.create,
					modulePath: sourcePath,
					sourceHash,
				},
			}
		} catch (error) {
			const message = getErrorMessage(error)
			Logger.warn(`[UserToolLoader] Skipping invalid user tool at '${toolDir}'.`, error)
			return { error: message }
		}
	}

	private static async readManifest(toolDir: string, source: ToolSource, approvedManifest?: Buffer): Promise<UserToolManifest> {
		const manifestPath = path.join(toolDir, "dirac-tool.json")
		const raw = approvedManifest ? approvedManifest.toString("utf8") : await fs.readFile(manifestPath, "utf8")
		const parsed = safeParseJson(userToolManifestSchema, raw, `user tool manifest '${manifestPath}'`)

		if (parsed.schemaVersion > 1) {
			Logger.warn(`[UserToolLoader] Tool at '${toolDir}' declares schemaVersion ${parsed.schemaVersion}. Expected 1. Proceeding with caution.`)
		}
		// Scope must match where the tool was discovered — contextual, not schema-checkable.
		const validScopes: Record<string, string[]> = {
			global: ["global"],
			workspace: ["workspace"],
			task: ["task"]
		}
		if (!validScopes[parsed.scope]?.includes(source)) {
			throw new Error(`Manifest scope '${parsed.scope}' does not match discovered source '${source}'.`)
		}

		return parsed
	}

	private static async compileTool(toolId: string, sourceCode: string, sourceHash: string): Promise<string> {
		const hash = sourceHash
		const cacheDir = path.join(this.getDiracHomePath(), "cache", "tools")
		await fs.mkdir(cacheDir, { recursive: true })

		const compiledPath = path.join(cacheDir, `${toolId}-${hash}.mjs`)
		try {
			await fs.access(compiledPath)
			return compiledPath
		} catch {
			// Compile below when the content-addressed output is missing.
		}

		const result = ts.transpileModule(sourceCode, {
			compilerOptions: {
				module: ts.ModuleKind.ESNext,
				target: ts.ScriptTarget.ES2020,
				moduleResolution: ts.ModuleResolutionKind.Node10,
			},
			fileName: `${toolId}.ts`,
			reportDiagnostics: true,
		})

		// Reject output that would produce invalid JavaScript
		const diagnostics = result.diagnostics?.filter((d) => d.category === ts.DiagnosticCategory.Error) ?? []
		if (diagnostics.length > 0) {
			const messages = diagnostics.map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"))
			throw new Error(`TypeScript transpile produced ${diagnostics.length} error(s): ${messages.join("; ")}`)
		}
		await fs.writeFile(compiledPath, result.outputText, "utf8")

		// Evict stale cache entries for this tool (different hash, same id)
		const stalePrefix = `${toolId}-`
		const cacheEntries = await fs.readdir(cacheDir)
		for (const entry of cacheEntries) {
			if (entry.startsWith(stalePrefix) && entry.endsWith(".mjs") && entry !== path.basename(compiledPath)) {
				await fs.unlink(path.join(cacheDir, entry)).catch(() => { })
			}
		}
		return compiledPath
	}

	private static async importCompiledTool(compiledPath: string): Promise<Required<UserToolModule>> {
		const dynamicImport = new Function("specifier", "return import(specifier)") as (
			specifier: string,
		) => Promise<UserToolModule>
		const mod = await dynamicImport(pathToFileURL(compiledPath).href)
		if (!mod.spec || typeof mod.spec !== "object") {
			throw new Error("User tool module must export object 'spec'.")
		}
		if (typeof mod.create !== "function") {
			throw new Error("User tool module must export function 'create'.")
		}
		return mod as Required<UserToolModule>
	}

	private static validateModule(manifest: UserToolManifest, mod: Required<UserToolModule>): void {
		if (mod.spec.id !== manifest.id) {
			throw new Error(`Manifest id '${manifest.id}' does not match spec id '${mod.spec.id}'.`)
		}
		if (mod.spec.name !== manifest.name) {
			throw new Error(`Manifest name '${manifest.name}' does not match spec name '${mod.spec.name}'.`)
		}
		if (typeof mod.spec.description !== "string" || mod.spec.description.trim().length === 0) {
			throw new Error("User tool spec must include a non-empty description.")
		}
	}

	private static hashToolSource(sourcePath: string, sourceCode: string): string {
		return crypto
			.createHash("sha256")
			.update(LOADER_VERSION)
			.update("\0")
			.update(sourcePath)
			.update("\0")
			.update(sourceCode)
			.digest("hex")
			.slice(0, 16)
	}


	/**
	 * Remove compiled cache files whose toolId is not in the active set.
	 * Called after tool discovery to clean up files for deleted tools.
	 */
	static async purgeStaleCache(activeToolIds: string[]): Promise<void> {
		const cacheDir = path.join(this.getDiracHomePath(), "cache", "tools")
		let entries: string[]
		try {
			entries = await fs.readdir(cacheDir)
		} catch {
			return // Cache dir doesn't exist yet
		}

		const activeSet = new Set(activeToolIds)
		for (const entry of entries) {
			if (!entry.endsWith(".mjs")) continue
			// Filename format: <toolId>-<hash>.mjs — toolId cannot contain hyphens (enforced by TOOL_ID_PATTERN)
			const dashIndex = entry.lastIndexOf("-")
			const toolId = dashIndex > 0 ? entry.slice(0, dashIndex) : entry.replace(/\.mjs$/, "")
			if (!activeSet.has(toolId)) {
				await fs.unlink(path.join(cacheDir, entry)).catch(() => { })
			}
		}
	}

	private static getDiracHomePath(): string {
		return diracHomeDir()
	}
}
