const botService = require("../services/botService");
const ApiResponse = require("../utils/apiResponse");

exports.getMarketplaceBots = async (req, res) => {
  const bots = await botService.marketplace(req.user.id);
  return ApiResponse.success(res, bots);
};

exports.getMarketplaceBotDetails = async (req, res) => {
  const { marketplaceBotId } = req.params;

  if (!marketplaceBotId) {
    return ApiResponse.notFound(res, "Invalid bot");
  }

  const bot = await botService.getMarketplaceBotDetails(
    req.user.id,
    marketplaceBotId
  );

  return ApiResponse.success(res, bot);
};

exports.deployBot = async (req, res) => {
  const { marketplaceBotId } = req.params;

  if (!marketplaceBotId) {
    return ApiResponse.notFound(res, "Invalid bot");
  }

  const payload = req.body || {};

  const instance = await botService.deployBot(
    req.user.id,
    marketplaceBotId,
    payload
  );

  return ApiResponse.success(res, instance, "Bot deployed successfully");
};

exports.getMyBotInstances = async (req, res) => {
  const instances = await botService.getMyBots(req.user.id);
  return ApiResponse.success(res, instances);
};

exports.getBotInstanceDetails = async (req, res) => {
  const { instanceId } = req.params;

  if (!instanceId) {
    return ApiResponse.notFound(res, "Invalid bot instance");
  }

  const instance = await botService.getBotInstanceDetails(
    req.user.id,
    instanceId
  );

  return ApiResponse.success(res, instance);
};

exports.startBotInstance = async (req, res) => {
  const { instanceId } = req.params;

  if (!instanceId) {
    return ApiResponse.notFound(res, "Invalid bot instance");
  }

  const instance = await botService.startBot(req.user.id, instanceId);

  return ApiResponse.success(res, instance, "Bot started successfully");
};

exports.stopBotInstance = async (req, res) => {
  const { instanceId } = req.params;

  if (!instanceId) {
    return ApiResponse.notFound(res, "Invalid bot instance");
  }

  const instance = await botService.stopBot(req.user.id, instanceId);

  return ApiResponse.success(res, instance, "Bot stopped successfully");
};

exports.pauseBotInstance = async (req, res) => {
  const { instanceId } = req.params;

  if (!instanceId) {
    return ApiResponse.notFound(res, "Invalid bot instance");
  }

  const instance = await botService.pauseBot(req.user.id, instanceId);

  return ApiResponse.success(res, instance, "Bot paused successfully");
};

exports.resumeBotInstance = async (req, res) => {
  const { instanceId } = req.params;

  if (!instanceId) {
    return ApiResponse.notFound(res, "Invalid bot instance");
  }

  const instance = await botService.resumeBot(req.user.id, instanceId);

  return ApiResponse.success(res, instance, "Bot resumed successfully");
};

exports.updateBotInstanceConfig = async (req, res) => {
  const { instanceId } = req.params;

  if (!instanceId) {
    return ApiResponse.notFound(res, "Invalid bot instance");
  }

  const payload = req.body || {};

  const instance = await botService.updateBotConfig(
    req.user.id,
    instanceId,
    payload
  );

  return ApiResponse.success(
    res,
    instance,
    "Bot configuration updated successfully"
  );
};

exports.cancelAutoRenew = async (req, res) => {
  const { instanceId } = req.params;

  const result = await botService.cancelAutoRenew(req.user.id, instanceId);

  return ApiResponse.success(res, result, "Auto-renew cancelled");
};

exports.renewSubscription = async (req, res) => {
  const { instanceId } = req.params;

  const result = await botService.renewSubscription(req.user.id, instanceId);

  return ApiResponse.success(res, result, "Subscription renewed");
};

exports.changePlan = async (req, res) => {
  const { instanceId } = req.params;

  const result = await botService.changePlan(
    req.user.id,
    instanceId,
    req.body
  );

  return ApiResponse.success(res, result, "Plan updated");
};

exports.getBotInstanceStats = async (req, res) => {
  const { instanceId } = req.params;

  const stats = await botService.getBotStats(
    req.user.id,
    instanceId,
    req.query.timeframe || "30d"
  );

  return ApiResponse.success(res, stats);
};

exports.getBotInstanceOrders = async (req, res) => {
  const { instanceId } = req.params;

  const orders = await botService.getBotOrders(
    req.user.id,
    instanceId
  );

  return ApiResponse.success(res, orders);
};

exports.getBotInstanceEvents = async (req, res) => {
  const { instanceId } = req.params;

  const events = await botService.getBotEvents(
    req.user.id,
    instanceId
  );

  return ApiResponse.success(res, events);
};

exports.deleteBotInstance = async (req, res) => {
  const { instanceId } = req.params;

  await botService.deleteBot(req.user.id, instanceId);

  return ApiResponse.success(res, null, "Bot deleted successfully");
};

exports.reviewMarketplaceBot = async (req, res) => {
  const { marketplaceBotId } = req.params;
  const { rating, review } = req.body;

  if (!rating || rating < 1 || rating > 5) {
    return ApiResponse.error(res, "Rating must be between 1 and 5");
  }

  const result = await botService.rateMarketplaceBot(
    req.user.id,
    marketplaceBotId,
    { rating, review }
  );

  return ApiResponse.success(res, result, "Review submitted successfully");
};
