"use strict";

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

// --------------------
// Wallets CRUD
// --------------------

exports.listCoins = asyncHandler(async (req, res) => {
  const data = await walletService.listCoins(req.query);
  ApiResponse.success(res, data);
});
exports.listWallets = asyncHandler(async (req, res) => {
  const data = await walletService.listWallets(req.query);
  ApiResponse.success(res, data);
});

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

exports.createWallet = asyncHandler(async (req, res) => {
  const wallet = await walletService.createWallet(req.body, req.admin?.id);
  ApiResponse.success(res, { wallet }, "Wallet created");
});

exports.updateWallet = asyncHandler(async (req, res) => {
  const wallet = await walletService.updateWallet(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, { wallet }, "Wallet updated");
});

exports.deleteWallet = asyncHandler(async (req, res) => {
  await walletService.deleteWallet(req.params.id, req.admin?.id);
  ApiResponse.success(res, {}, "Wallet deleted");
});

// --------------------
// Wallet Addresses CRUD
// --------------------
exports.listWalletAddresses = asyncHandler(async (req, res) => {
  const data = await walletService.listWalletAddresses(req.query);
  ApiResponse.success(res, data);
});

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

exports.createWalletAddress = asyncHandler(async (req, res) => {
  const address = await walletService.createWalletAddress(req.body, req.admin?.id);
  ApiResponse.success(res, { address }, "Wallet address created");
});

exports.updateWalletAddress = asyncHandler(async (req, res) => {
  const address = await walletService.updateWalletAddress(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, { address }, "Wallet address updated");
});

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

// --------------------
// Actions (creates Transaction)
// --------------------
exports.depositToWallet = asyncHandler(async (req, res) => {
  const data = await walletService.depositToWallet(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, data, "Deposit created");
});

exports.withdrawFromWallet = asyncHandler(async (req, res) => {
  const data = await walletService.withdrawFromWallet(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, data, "Withdrawal created");
});

exports.swapWallet = asyncHandler(async (req, res) => {
  const data = await walletService.swapWallet(req.params.id, req.body, req.admin?.id);
  ApiResponse.success(res, data, "Swap completed");
});