import { humanizeResourceType } from "../create/resource-defaults.js";
import { promptOneResource } from "../create/prompt-resource.js";
import { validateManifest } from "../validate/validate-manifest.js";
import fs from "node:fs";
import path from "node:path";
import { Command } from "commander";
import process from "node:process";
import { cancel, intro, outro } from "@clack/prompts";

//#region src/cli/commands/plugin/add-resource/add-resource.ts
async function runPluginAddResource(options) {
	intro("Add resource to plugin manifest");
	const cwd = process.cwd();
	const pluginDir = path.resolve(cwd, options.path ?? ".");
	const manifestPath = path.join(pluginDir, "manifest.json");
	if (!fs.existsSync(manifestPath)) {
		console.error(`manifest.json not found at ${manifestPath}`);
		process.exit(1);
	}
	let manifest;
	try {
		const raw = fs.readFileSync(manifestPath, "utf-8");
		const parsed = JSON.parse(raw);
		const result = validateManifest(parsed);
		if (!result.valid || !result.manifest) {
			console.error("Invalid manifest. Run `appkit plugin validate` for details.");
			process.exit(1);
		}
		manifest = parsed;
	} catch (err) {
		console.error("Failed to read or parse manifest.json:", err instanceof Error ? err.message : err);
		process.exit(1);
	}
	const spec = await promptOneResource();
	if (!spec) {
		cancel("Cancelled.");
		process.exit(0);
	}
	const alias = humanizeResourceType(spec.type);
	const entry = {
		type: spec.type,
		alias,
		resourceKey: spec.resourceKey,
		description: spec.description || `Required for ${alias} functionality.`,
		permission: spec.permission,
		fields: spec.fields
	};
	if (spec.required) manifest.resources.required.push(entry);
	else manifest.resources.optional.push(entry);
	fs.writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
	outro("Resource added.");
	console.log(`\nAdded ${alias} as ${spec.required ? "required" : "optional"} to ${path.relative(cwd, manifestPath)}`);
}
const pluginAddResourceCommand = new Command("add-resource").description("Add a resource requirement to an existing plugin manifest (interactive). Overwrites manifest.json in place.").option("-p, --path <dir>", "Plugin directory containing manifest.json (default: .)").action((opts) => runPluginAddResource(opts).catch((err) => {
	console.error(err);
	process.exit(1);
}));

//#endregion
export { pluginAddResourceCommand };
//# sourceMappingURL=add-resource.js.map