"use server";

import FileOp from "@/lib/cpanel/api2/Fileman/fileop";
import { ServerResponse } from "@/lib/actions";
import { getCpanelVars } from "@/models/cpanel";
import { ServerAction } from "@/lib/actions";

export default async function renameSubdomainFiles(formData: FormData) {
  const { error, con, dict } = await ServerAction();

  if (error) {
    return ServerResponse({
      operation: false,
      data: error,
    });
  }

  const name = formData.get("name") as string;
  const path = formData.get("path") as string;
  const sysPath = formData.get("sysPath") as string;
  const project = formData.get("project") as string;

  try {
    const basePath = `/home/fedgovad/FEDSITE_${project}/`;

    const newPath = `${basePath}${path ? path + "/" : ""}${name}`;

    const response = await FileOp({
      operation: "rename",
      sourceFiles: sysPath,
      destFiles: newPath,
    });

    if (response.cpanelresult.status === 0) {
      return ServerResponse({
        operation: false,
        message: dict.alerts.rename_file.error,
      });
    }

    return ServerResponse({
      operation: true,
      message: dict.alerts.rename_file.success,
    });
  } catch (error) {
    return ServerResponse({
      operation: false,
      message: dict.alerts.general.unexpected_error,
      data: error,
    });
  } finally {
    con.release();
  }
}
