"use strict";

const adminNetworkService = require("../../services/admin/networkService");
const ApiResponse = require("../../utils/apiResponse");
const { asyncHandler } = require("../../middleware/errorHandler");

/**
 * Networks
 */
exports.listNetworks = asyncHandler(async (req, res) => {
  const data = await adminNetworkService.listNetworks(req.query);
  ApiResponse.success(res, data);
});

exports.getNetworkById = asyncHandler(async (req, res) => {
  const data = await adminNetworkService.getNetworkById(req.params.id);
  ApiResponse.success(res, data);
});

exports.createNetwork = asyncHandler(async (req, res) => {
  const network = await adminNetworkService.createNetwork(req.body, req.admin?.id);
  ApiResponse.success(res, { network }, "Network created");
});

exports.updateNetwork = asyncHandler(async (req, res) => {
  const network = await adminNetworkService.updateNetwork(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, { network }, "Network updated");
});

exports.deleteNetwork = asyncHandler(async (req, res) => {
  await adminNetworkService.deleteNetwork(req.params.id, req.admin?.id);
  ApiResponse.success(res, {}, "Network deleted");
});

/**
 * Network Addresses
 */
exports.listNetworkAddresses = asyncHandler(async (req, res) => {
  const data = await adminNetworkService.listNetworkAddresses(req.query);
  ApiResponse.success(res, data);
});

exports.getNetworkAddressById = asyncHandler(async (req, res) => {
  const data = await adminNetworkService.getNetworkAddressById(req.params.id);
  ApiResponse.success(res, data);
});

exports.createNetworkAddress = asyncHandler(async (req, res) => {
  const row = await adminNetworkService.createNetworkAddress(req.body, req.admin?.id);
  ApiResponse.success(res, { address: row }, "Network address created");
});

exports.updateNetworkAddress = asyncHandler(async (req, res) => {
  const row = await adminNetworkService.updateNetworkAddress(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, { address: row }, "Network address updated");
});

exports.deleteNetworkAddress = asyncHandler(async (req, res) => {
  await adminNetworkService.deleteNetworkAddress(req.params.id, req.admin?.id);
  ApiResponse.success(res, {}, "Network address deleted");
});

/**
 * Coin Networks
 */
exports.listCoinNetworks = asyncHandler(async (req, res) => {
  const data = await adminNetworkService.listCoinNetworks(req.query);
  ApiResponse.success(res, data);
});

exports.getCoinNetworkById = asyncHandler(async (req, res) => {
  const data = await adminNetworkService.getCoinNetworkById(req.params.id);
  ApiResponse.success(res, data);
});

exports.createCoinNetwork = asyncHandler(async (req, res) => {
  const row = await adminNetworkService.createCoinNetwork(req.body, req.admin?.id);
  ApiResponse.success(res, { coinNetwork: row }, "Coin network created");
});

exports.updateCoinNetwork = asyncHandler(async (req, res) => {
  const row = await adminNetworkService.updateCoinNetwork(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, { coinNetwork: row }, "Coin network updated");
});

exports.deleteCoinNetwork = asyncHandler(async (req, res) => {
  await adminNetworkService.deleteCoinNetwork(req.params.id, req.admin?.id);
  ApiResponse.success(res, {}, "Coin network deleted");
});