// controllers/admin/supportController.js
const supportService = require("../../services/admin/supportService");
const ApiResponse = require("../../utils/apiResponse");
const { asyncHandler } = require("../../middleware/errorHandler");

exports.getTickets = asyncHandler(async (req, res) => {
  const result = await supportService.getTickets(req.query);
  ApiResponse.paginated(res, result, result.pagination);
});

exports.getTicketById = asyncHandler(async (req, res) => {
  const ticket = await supportService.getTicketById(req.params.id);
  ApiResponse.success(res, { ticket });
});

exports.createTicket = asyncHandler(async (req, res) => {
  const ticket = await supportService.createTicket(req.body);
  ApiResponse.created(res, { ticket }, "Ticket created successfully");
});

exports.replyToTicket = asyncHandler(async (req, res) => {
  const { message, attachments, isInternal } = req.body;
  // req.admin is set by authenticateAdmin middleware
  const ticketMessage = await supportService.replyToTicket(
    req.admin.id,
    req.admin.fullName || req.admin.email,
    req.params.id,
    message,
    attachments,
    isInternal
  );
  ApiResponse.created(res, { message: ticketMessage }, "Reply sent successfully");
});

exports.closeTicket = asyncHandler(async (req, res) => {
  const ticket = await supportService.closeTicket(req.params.id);
  ApiResponse.success(res, { ticket }, "Ticket closed successfully");
});

exports.reopenTicket = asyncHandler(async (req, res) => {
  const ticket = await supportService.reopenTicket(req.params.id);
  ApiResponse.success(res, { ticket }, "Ticket reopened successfully");
});

exports.updateTicketStatus = asyncHandler(async (req, res) => {
  const ticket = await supportService.updateTicketStatus(req.params.id, req.body.status);
  ApiResponse.success(res, { ticket }, "Ticket status updated");
});

exports.updateTicketPriority = asyncHandler(async (req, res) => {
  const ticket = await supportService.updateTicketPriority(req.params.id, req.body.priority);
  ApiResponse.success(res, { ticket }, "Ticket priority updated");
});

exports.deleteTicket = asyncHandler(async (req, res) => {
  await supportService.deleteTicket(req.params.id);
  ApiResponse.success(res, null, "Ticket deleted successfully");
});

exports.deleteTicketMessage = asyncHandler(async (req, res) => {
  await supportService.deleteTicketMessage(req.params.id, req.params.msgId);
  ApiResponse.success(res, null, "Message deleted successfully");
});

exports.rateTicket = asyncHandler(async (req, res) => {
  const { rating, comment } = req.body;
  const ticket = await supportService.rateTicket(req.params.id, rating, comment);
  ApiResponse.success(res, { ticket }, "Thank you for your feedback");
});

exports.getFAQCategories = asyncHandler(async (req, res) => {
  const categories = await supportService.getFAQCategories();
  ApiResponse.success(res, { categories });
});

exports.searchFAQ = asyncHandler(async (req, res) => {
  const faqs = await supportService.searchFAQ(req.query.q);
  ApiResponse.success(res, { faqs });
});

exports.getTicketStats = asyncHandler(async (req, res) => {
  const stats = await supportService.getTicketStats();
  ApiResponse.success(res, { statistics: stats });
});