import type { Config } from "release-it";

interface ConventionalCommitLike {
	type?: string;
	notes?: unknown[];
}

const config: Config = {
	git: {
		requireBranch: "main",
		requireCleanWorkingDir: true,
		requireUpstream: true,
		commit: false,
		tag: true,
		push: true,
		tagName: "v${version}",
		pushArgs: ["--follow-tags"],
	},
	github: {
		release: true,
		releaseName: "v${version}",
		skipChecks: true,
		tokenRef: "GITHUB_TOKEN",
	},
	npm: {
		publish: false,
	},
	plugins: {
		"@release-it/conventional-changelog": {
			infile: false,
			preset: {
				name: "conventionalcommits",
				commitUrlFormat:
					"https://github.com/gohugo-ananke/ananke/commit/{{hash}}",
				compareUrlFormat:
					"https://github.com/gohugo-ananke/ananke/compare/{{previousTag}}...{{currentTag}}",
				types: [
					{ type: "feat", section: "Features" },
					{ type: "fix", section: "Bug Fixes" },
					{ type: "build", section: "Build" },
					{ type: "chore", section: "Chores" },
					{ type: "ci", section: "CI" },
					{ type: "docs", section: "Documentation" },
					{ type: "perf", section: "Performance" },
					{ type: "refactor", section: "Refactoring" },
					{ type: "revert", section: "Reverts" },
					{ type: "style", section: "Styles" },
					{ type: "test", section: "Tests" },
					{ type: "ai", section: "AI Instruction Files" },
				],
			},
			whatBump(commits: ConventionalCommitLike[]) {
				let level: 2 | 1 | 0 | null = null;

				for (const commit of commits) {
					const notes = Array.isArray(commit.notes) ? commit.notes : [];
					const type = typeof commit.type === "string" ? commit.type : "";

					if (notes.length > 0) {
						return {
							level: 0,
							reason: "There are BREAKING CHANGES.",
						};
					}

					if (type === "feat") {
						level = 1;
						continue;
					}

					if (
						level === null &&
						[
							"fix",
							"build",
							"chore",
							"ci",
							"docs",
							"perf",
							"refactor",
							"revert",
							"style",
							"test",
							"ai",
						].includes(type)
					) {
						level = 2;
					}
				}

				if (level === null) {
					return false;
				}

				return {
					level,
					reason:
						level === 1
							? "There are feature commits."
							: "There are patch-level changes.",
				};
			},
		},
	},
} satisfies Config;

export default config;
