import { v4 as uuidv4 } from "uuid";
import chalk from "chalk";

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

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

const createProfileCommand: Command = {
  name: "create-profile",
  description:
    "Creates a new profile with the given name. Usage: create-profile <profile name>",
  execute: async (args: string[]) => {
    if (args.length < 1) {
      console.log(chalk.red("Please provide a profile name"));
      return;
    }

    const profileName = args.join(" ");
    console.log(chalk.yellow(`Creating profile for ${profileName}...`));
    const id = uuidv4();

    const assetsFolder = profileName.split(" ").join("-").toLowerCase();

    const profileData: ProfileFile = {
      id,
      name: profileName,
      assetsFolder,
      enabled: false,
    };

    await helpers.profile.save(id, JSON.stringify(profileData));
    await helpers.asset.init(assetsFolder);

    console.log(chalk.green(`Profile "${profileName}" created with id: ${id}`));
  },
};

commandHandler.registerCommand(createProfileCommand);
