const { validationResult } = require('express-validator');
const AdminServiceService = require('../services/adminServiceService');
const logger = require('../config/logger');

/**
 * Admin Service Controller
 * Handles admin service management endpoints
 */
class AdminServiceController {
  /**
   * Get services with pagination and filtering
   * @route GET /api/v1/admin/services
   */
  static async getServices(req, res, next) {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({
          success: false,
          error: {
            code: 'VALIDATION_ERROR',
            message: 'Invalid request parameters',
            details: errors.array(),
          },
        });
      }

      const { page = 1, limit = 10, categoryId, status, search } = req.query;

      const result = await AdminServiceService.getServices({
        page: parseInt(page),
        limit: parseInt(limit),
        categoryId,
        status,
        search,
      });

      res.json({
        success: true,
        data: result.data,
        pagination: result.pagination,
      });
    } catch (error) {
      logger.error('Error in getServices (admin):', error);
      next(error);
    }
  }

  /**
   * Get service by ID
   * @route GET /api/v1/admin/services/:id
   */
  static async getServiceById(req, res, next) {
    try {
      const { id } = req.params;

      const service = await AdminServiceService.getServiceById(id);

      if (!service) {
        return res.status(404).json({
          success: false,
          error: {
            code: 'SERVICE_NOT_FOUND',
            message: 'Service not found',
          },
        });
      }

      res.json({
        success: true,
        data: service,
      });
    } catch (error) {
      logger.error('Error in getServiceById (admin):', error);
      next(error);
    }
  }

  /**
   * Create a new service
   * @route POST /api/v1/admin/services
   */
  static async createService(req, res, next) {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        logger.error('Service creation validation failed:', { errors: errors.array(), body: req.body });
        return res.status(400).json({
          success: false,
          error: {
            code: 'VALIDATION_ERROR',
            message: 'Invalid request data',
            details: errors.array(),
          },
        });
      }

      const service = await AdminServiceService.createService(req.body);

      res.status(201).json({
        success: true,
        data: service,
        message: 'Service created successfully',
      });
    } catch (error) {
      if (error.statusCode) {
        return res.status(error.statusCode).json({
          success: false,
          error: {
            code: error.code || 'SERVICE_ERROR',
            message: error.message,
          },
        });
      }
      logger.error('Error in createService:', error);
      next(error);
    }
  }

  /**
   * Update a service
   * @route PUT /api/v1/admin/services/:id
   */
  static async updateService(req, res, next) {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(400).json({
          success: false,
          error: {
            code: 'VALIDATION_ERROR',
            message: 'Invalid request data',
            details: errors.array(),
          },
        });
      }

      const { id } = req.params;
      const service = await AdminServiceService.updateService(id, req.body);

      res.json({
        success: true,
        data: service,
        message: 'Service updated successfully',
      });
    } catch (error) {
      if (error.statusCode) {
        return res.status(error.statusCode).json({
          success: false,
          error: {
            code: error.code || 'SERVICE_ERROR',
            message: error.message,
          },
        });
      }
      logger.error('Error in updateService:', error);
      next(error);
    }
  }

  /**
   * Delete a service
   * @route DELETE /api/v1/admin/services/:id
   */
  static async deleteService(req, res, next) {
    try {
      const { id } = req.params;

      await AdminServiceService.deleteService(id);

      res.json({
        success: true,
        message: 'Service deleted successfully',
      });
    } catch (error) {
      if (error.statusCode) {
        return res.status(error.statusCode).json({
          success: false,
          error: {
            code: error.code || 'SERVICE_ERROR',
            message: error.message,
          },
        });
      }
      logger.error('Error in deleteService:', error);
      next(error);
    }
  }
}

module.exports = AdminServiceController;
