package com.carmaintenance.controller;

import com.carmaintenance.dto.Result;
import com.carmaintenance.entity.Appointment;
import com.carmaintenance.repository.AppointmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约管理控制器
 */
@RestController
@RequestMapping("/appointments")
@CrossOrigin
public class AppointmentController {

    @Autowired
    private AppointmentRepository appointmentRepository;

    /**
     * 获取所有预约
     */
    @GetMapping
    public Result<List<Appointment>> getAllAppointments() {
        List<Appointment> appointments = appointmentRepository.findAll();
        return Result.success(appointments);
    }

    /**
     * 根据ID获取预约
     */
    @GetMapping("/{id}")
    public Result<Appointment> getAppointmentById(@PathVariable Integer id) {
        Appointment appointment = appointmentRepository.findById(id).orElse(null);
        if (appointment == null) {
            return Result.notFound("预约不存在");
        }
        return Result.success(appointment);
    }

    /**
     * 根据客户ID获取预约列表
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<Appointment>> getAppointmentsByCustomerId(@PathVariable Integer customerId) {
        List<Appointment> appointments = appointmentRepository.findByCustomerId(customerId);
        return Result.success(appointments);
    }

    /**
     * 根据状态获取预约列表
     */
    @GetMapping("/status/{status}")
    public Result<List<Appointment>> getAppointmentsByStatus(@PathVariable String status) {
        try {
            Appointment.AppointmentStatus appointmentStatus = Appointment.AppointmentStatus.valueOf(status);
            List<Appointment> appointments = appointmentRepository.findByStatus(appointmentStatus);
            return Result.success(appointments);
        } catch (IllegalArgumentException e) {
            return Result.error("状态参数错误");
        }
    }

    /**
     * 创建预约
     */
    @PostMapping
    public Result<Appointment> createAppointment(@RequestBody Appointment appointment) {
        appointment.setStatus(Appointment.AppointmentStatus.pending);
        Appointment savedAppointment = appointmentRepository.save(appointment);
        return Result.success("预约成功", savedAppointment);
    }

    /**
     * 更新预约
     */
    @PutMapping("/{id}")
    public Result<Appointment> updateAppointment(@PathVariable Integer id, @RequestBody Appointment appointment) {
        Appointment existingAppointment = appointmentRepository.findById(id).orElse(null);
        if (existingAppointment == null) {
            return Result.notFound("预约不存在");
        }

        if (appointment.getAppointmentTime() != null)
            existingAppointment.setAppointmentTime(appointment.getAppointmentTime());
        if (appointment.getContactPhone() != null)
            existingAppointment.setContactPhone(appointment.getContactPhone());
        if (appointment.getDescription() != null)
            existingAppointment.setDescription(appointment.getDescription());
        if (appointment.getStatus() != null)
            existingAppointment.setStatus(appointment.getStatus());
        if (appointment.getOrderId() != null)
            existingAppointment.setOrderId(appointment.getOrderId());

        Appointment updatedAppointment = appointmentRepository.save(existingAppointment);
        return Result.success("更新成功", updatedAppointment);
    }

    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    public Result<Appointment> cancelAppointment(@PathVariable Integer id) {
        Appointment appointment = appointmentRepository.findById(id).orElse(null);
        if (appointment == null) {
            return Result.notFound("预约不存在");
        }

        appointment.setStatus(Appointment.AppointmentStatus.cancelled);
        Appointment updatedAppointment = appointmentRepository.save(appointment);
        return Result.success("取消成功", updatedAppointment);
    }

    /**
     * 删除预约
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteAppointment(@PathVariable Integer id) {
        if (!appointmentRepository.existsById(id)) {
            return Result.notFound("预约不存在");
        }
        appointmentRepository.deleteById(id);
        return Result.<Void>success("删除成功", null);
    }
}
