import chalk from "chalk";

import { commandHandler, helpers } from "../core";

import type { Command } from "../core";

const changeProfileEnabled: Command = {
  name: "change-profile-enabled",
  description:
    "Enable profile with the given name. Usage: change-profile-enabled [true/false] <profile name>",
  execute: async (args: string[]) => {
    if (args.length < 2) {
      console.log(chalk.red("Please provide profile status and name"));
      return;
    }

    const status = String(args[0]).toLowerCase() === "true";
    const profileName = args.slice(1).join(" ");

    const profiles = await helpers.profile.getAll();
    const profile = profiles.find((profile) => profile.name === profileName);
    if (!profile) {
      console.log(chalk.red(`Profile "${profileName}" not found`));
      return;
    }

    profile.enabled = status;
    await helpers.profile.save(profile.id, JSON.stringify(profile));
    console.log(
      chalk.yellow(
        `Profile "${profileName}" is now ${status ? chalk.green("enabled") : chalk.red("disabled")}`,
      ),
    );
  },
};

commandHandler.registerCommand(changeProfileEnabled);
