const adminAppointmentService = require('../services/adminAppointmentService');

/**
 * Admin Appointment Controller
 * Handles HTTP requests for appointment management in admin panel
 */

/**
 * Get appointment list with pagination and filters
 * @route GET /api/v1/admin/appointments
 */
const getAppointmentList = async (req, res) => {
  try {
    const {
      page,
      limit,
      search,
      status,
      categoryId,
      serviceId,
      startDate,
      endDate,
      sortBy,
      sortOrder,
    } = req.query;

    const result = await adminAppointmentService.getAppointmentList({
      page,
      limit,
      search,
      status,
      categoryId,
      serviceId,
      startDate,
      endDate,
      sortBy,
      sortOrder,
    });

    res.json({
      success: true,
      data: result,
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: {
        code: 'GET_APPOINTMENT_LIST_ERROR',
        message: 'Failed to retrieve appointment list',
        details: error.message,
      },
    });
  }
};

/**
 * Get appointment details
 * @route GET /api/v1/admin/appointments/:id
 */
const getAppointmentDetails = async (req, res) => {
  try {
    const { id } = req.params;

    const result = await adminAppointmentService.getAppointmentDetails(id);

    res.json({
      success: true,
      data: result,
    });
  } catch (error) {
    if (error.message === 'Appointment not found') {
      return res.status(404).json({
        success: false,
        error: {
          code: 'APPOINTMENT_NOT_FOUND',
          message: 'Appointment not found',
        },
      });
    }

    res.status(500).json({
      success: false,
      error: {
        code: 'GET_APPOINTMENT_DETAILS_ERROR',
        message: 'Failed to retrieve appointment details',
        details: error.message,
      },
    });
  }
};

/**
 * Update appointment status
 * @route PUT /api/v1/admin/appointments/:id
 */
const updateAppointmentStatus = async (req, res) => {
  try {
    const { id } = req.params;
    const { status } = req.body;

    if (!status) {
      return res.status(400).json({
        success: false,
        error: {
          code: 'MISSING_STATUS',
          message: 'Status is required',
        },
      });
    }

    const appointment = await adminAppointmentService.updateAppointmentStatus(id, status);

    res.json({
      success: true,
      data: {
        appointment: {
          id: appointment.id,
          status: appointment.status,
          updatedAt: appointment.updatedAt,
        },
      },
      message: 'Appointment status updated successfully',
    });
  } catch (error) {
    if (error.message === 'Appointment not found') {
      return res.status(404).json({
        success: false,
        error: {
          code: 'APPOINTMENT_NOT_FOUND',
          message: 'Appointment not found',
        },
      });
    }

    if (error.message === 'Invalid status value') {
      return res.status(400).json({
        success: false,
        error: {
          code: 'INVALID_STATUS',
          message: 'Invalid status value. Must be one of: pending, confirmed, in-progress, completed, cancelled',
        },
      });
    }

    res.status(500).json({
      success: false,
      error: {
        code: 'UPDATE_STATUS_ERROR',
        message: 'Failed to update appointment status',
        details: error.message,
      },
    });
  }
};

/**
 * Get appointment statistics
 * @route GET /api/v1/admin/appointments/statistics
 */
const getAppointmentStatistics = async (req, res) => {
  try {
    const { startDate, endDate, categoryId } = req.query;

    const result = await adminAppointmentService.getAppointmentStatistics({
      startDate,
      endDate,
      categoryId,
    });

    res.json({
      success: true,
      data: result,
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      error: {
        code: 'GET_STATISTICS_ERROR',
        message: 'Failed to retrieve appointment statistics',
        details: error.message,
      },
    });
  }
};

module.exports = {
  getAppointmentList,
  getAppointmentDetails,
  updateAppointmentStatus,
  getAppointmentStatistics,
};
