"use strict";

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

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

exports.getSignalById = asyncHandler(async (req, res) => {
  const { signalId } = req.params;
  const data = await adminSignalService.getSignalById(signalId);
  ApiResponse.success(res, data);
});

exports.createSignal = asyncHandler(async (req, res) => {
  const signal = await adminSignalService.createSignal(req.body, req.admin?.id);
  ApiResponse.success(res, { signal }, "Signal created successfully");
});

exports.updateSignal = asyncHandler(async (req, res) => {
  const { signalId } = req.params;
  const signal = await adminSignalService.updateSignal(signalId, req.body, req.admin?.id);
  ApiResponse.success(res, { signal }, "Signal updated successfully");
});

exports.deleteSignal = asyncHandler(async (req, res) => {
  const { signalId } = req.params;
  await adminSignalService.deleteSignal(signalId, req.admin?.id);
  ApiResponse.success(res, {}, "Signal deleted successfully");
});

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

exports.getProviderById = asyncHandler(async (req, res) => {
  const { providerId } = req.params;
  const data = await adminSignalService.getProviderById(providerId);
  ApiResponse.success(res, data);
});

exports.createProvider = asyncHandler(async (req, res) => {
  const provider = await adminSignalService.createProvider(req.body, req.file?.filename ?? undefined);
  ApiResponse.success(res, { provider }, "Provider created successfully");
});

exports.updateProvider = asyncHandler(async (req, res) => {
  const { providerId } = req.params;
  const provider = await adminSignalService.updateProvider(providerId, req.body,req.file?.filename ?? undefined);
  ApiResponse.success(res, { provider }, "Provider updated successfully");
});

exports.deleteProvider = asyncHandler(async (req, res) => {
  const { providerId } = req.params;
  await adminSignalService.deleteProvider(providerId);
  ApiResponse.success(res, {}, "Provider deleted successfully");
});