import { dirname, resolve } from "path";
import { InitStepError } from "./baseStep";
import { MIN_CODEFLASH_VERSION } from "../constants/cf_min_version";

export const GIT_ERRORS = {
  NO_REPO: "NO_REPO",
  NO_INIT_COMMIT: "NO_INIT_COMMIT",
  BARE_REPO: "BARE_REPO",
};

export const ERROR_CATEGORIES = {
  CODEFLASH_INIT: "CODEFLASH_INIT",
};

export const getPythonNotSelectedError = (): InitStepError => {
  const err = new InitStepError("No Python interpreter selected");
  err.details =
    "No Python interpreter selected in the current workspace, are you sure you have python installed?";
  err.vscodeActionCommand = {
    title: "",
    btnText: "Select interpreter",
    command: "python.setInterpreter",
  };
  return err;
};

export const getIncorrectPythonInterpreterPath = (
  incorrectPath: string,
): InitStepError => {
  const err = new InitStepError("Incorrect Python interpreter path");
  err.details = `The Python interpreter path (<code>${incorrectPath}</code>) is invalid or not executable. Please select a valid Python interpreter.`;
  err.vscodeActionCommand = {
    title: "",
    btnText: "Select interpreter",
    command: "python.setInterpreter",
  };
  return err;
};

export const getCodeflashVersionNotValidError = (
  interpreterPath: string,
  upgradeCmd?: string,
): InitStepError => {
  const err = new InitStepError("Codeflash version check failed");
  err.details = `Failed to determine the installed Codeflash version in the current active Python environment (<code>${interpreterPath}</code>).`;
  const installCommand = upgradeCmd ?? "pip install --upgrade codeflash";
  err.installAction = {
    btnText: "Reinstall Codeflash",
    command: installCommand,
    showCommand: true,
  };
  err.vscodeActionCommand = {
    title: "Or you can select another interpreter:",
    btnText: "Select interpreter",
    command: "python.setInterpreter",
  };
  return err;
};

export const getCodeflashVersionIncompatibleError = (
  installedVersion: string,
  interpreterPath: string,
  withMinVersion: boolean = true,
  upgradeCmd?: string,
): InitStepError => {
  const err = new InitStepError("Incompatible Codeflash version installed");
  err.details = `The installed Codeflash version (<code>${installedVersion}</code>) is incompatible with this extension.${withMinVersion ? ` Please upgrade to at least version <code>${MIN_CODEFLASH_VERSION}</code>.` : ""}<br /><br />Your active Python interpreter is located at: <code>${interpreterPath}</code>.`;
  const installCommand = upgradeCmd ?? "pip install --upgrade codeflash";
  err.installAction = {
    btnText: "Upgrade Codeflash",
    command: installCommand,
    showCommand: true,
  };
  return err;
};

export const getCodeflashNotInstalledError = (
  interpreterPath: string,
  installCmd: string,
): InitStepError => {
  const err = new InitStepError("Almost there!");
  err.details = `This extension requires the codeflash python package to be installed in your active Python environment.`;
  err.installAction = {
    btnText: "Install codeflash Python package",
    command: installCmd,
    showCommand: true,
    disclaimer: interpreterPath,
  };
  err.vscodeActionCommand = {
    title: "",
    btnText: "Select a different interpreter",
    command: "python.setInterpreter",
  };
  return err;
};

export const getLspServerFailedToStartError = (
  lspError: string,
  upgradeCmd?: string,
): InitStepError => {
  const err = new InitStepError("Codeflash server failed to start");
  err.details = `The Codeflash language server failed to start: <code>${lspError}</code>.<br />This may be caused by an incompatibility between your Codeflash server and the installed extension version.`;
  const installCommand = upgradeCmd ?? "pip install --upgrade codeflash";
  err.installAction = {
    btnText: "Reinstall Codeflash",
    command: installCommand,
    showCommand: true,
  };
  err.vscodeActionCommand = {
    title: "Or you can select another interpreter:",
    btnText: "Select interpreter",
    command: "python.setInterpreter",
  };
  return err;
};

export const getNoGitRepoError = (): InitStepError => {
  const err = new InitStepError("No git repository found");
  err.details =
    "Codeflash requires a Git repository to be initialized. It leverages Git’s worktree feature to safely test generated code in an isolated environment, allowing you to continue working seamlessly in your original code.";
  // TODO: create a command to initialize and create initial commit in the current workspace
  err.helperCmdText =
    "You can initialize a new git repository by using this command:";
  err.helperCmd = "git init && git add . && git commit -m 'Initial commit'";
  return err;
};

export const getBareRepoError = (): InitStepError => {
  const err = new InitStepError("Bare Git repository detected");
  err.details =
    "Codeflash cannot operate inside a bare Git repository. Bare repositories do not have a working directory, which is required to safely test and modify generated code.";
  err.helperCmdText =
    "You can clone this repository with a working directory using:";
  err.helperCmd = "git clone <repo_url> <target_folder>";
  return err;
};

export const getNoInitCommitError = (): InitStepError => {
  const err = new InitStepError("Repository has no initial commit");
  err.details =
    "Codeflash requires at least one commit in your Git repository to create a worktree for testing generated code. Without an initial commit, Git worktree cannot function.";
  err.helperCmdText =
    "You can create the first commit with the following command:";
  err.helperCmd = "git add . && git commit -m 'Initial commit'";
  return err;
};

export const getCodeflashConfiguredPyprojectNotFound = (
  configuredPath: string,
): InitStepError => {
  const err = new InitStepError("Configured pyproject file not found");
  err.details = `The config file at <code>${configuredPath}</code> was not found. Please ensure the path is correct or update the configuration to point to an existing file.`;
  err.vscodeActionCommand = {
    title: "",
    btnText: "Configure codeflash config path",
    command: "workbench.action.openSettings",
    args: ["codeflash.configFilePath"],
  };
  return err;
};

export const getCodeflashPyprojectNotFound = (): InitStepError => {
  const err = new InitStepError("No pyproject.toml file");
  err.category = ERROR_CATEGORIES.CODEFLASH_INIT;
  err.details =
    "Codeflash needs a <code>pyproject.toml</code> file to identify your module, tests, and other configs";
  err.icon = "warning";
  err.iconFill = "var(--vscode-editorWarning-foreground)";
  return err;
};

export const getCodeflashPyprojectConfigNotDefined = (
  pyprojectPath: string,
): InitStepError => {
  const selectedFolder = dirname(resolve(pyprojectPath));
  const err = new InitStepError("Codeflash is not configured");
  err.category = ERROR_CATEGORIES.CODEFLASH_INIT;
  err.details = `Found a <code>pyproject.toml</code> file at <code>${selectedFolder}</code>, but it does not have codeflash settings configured`;
  err.icon = "warning";
  err.iconFill = "var(--vscode-editorWarning-foreground)";

  err.helperCmdText = `You can set it up by running this command:`;
  err.helperCmd = "codeflash init";
  return err;
};

export const getCodeflashPyprojectConfigNotValid = (
  pyprojectPath: string,
  errorMessage: string,
): InitStepError => {
  const err = new InitStepError("Codeflash config is not valid");
  err.category = ERROR_CATEGORIES.CODEFLASH_INIT;
  err.details = `Codeflash configuration found at <code>${pyprojectPath}</code>, but it's not valid.<br />${errorMessage}`;
  err.icon = "warning";
  err.iconFill = "var(--vscode-editorWarning-foreground)";
  err.helperCmdText =
    "Please run this command to re-initialize your codeflash config";
  err.helperCmd = "codeflash init";
  return err;
};
