package com.ruoyi.web.modules.booking.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.modules.booking.domain.AppBookingResourceQuery;
import com.ruoyi.web.modules.booking.domain.AppBookingSubmitBody;
import com.ruoyi.web.modules.booking.domain.vo.AppBookingDateBookedVo;
import com.ruoyi.web.modules.booking.domain.vo.AppBookingDateVo;
import com.ruoyi.web.modules.booking.domain.vo.AppointmentResourceVo;
import com.ruoyi.web.modules.booking.service.IAppBookingService;
import com.ruoyi.web.modules.booking.support.AppBookingPageSupport;
import com.ruoyi.web.modules.booking.support.AppBookingResourceQuerySupport;
import com.ruoyi.web.modules.farming.domain.BizServiceAppointmentReview;

/**
 * 移动端预约服务（兽医 / 专家 / 诊疗机构）。
 */
@RestController
@RequestMapping("/app/bookingService")
public class BizAppBookingServiceController extends BaseController
{
    @Autowired
    private IAppBookingService appBookingService;

    /**
     * 近 7 日日期条（含当天）。
     */
    @GetMapping("/dates")
    public AjaxResult dates()
    {
        List<AppBookingDateVo> list = appBookingService.selectBookingDateList();
        return success(list);
    }

    /**
     * 可预约资源列表（v_appointment_resource）。
     */
    @GetMapping("/resource/list")
    public TableDataInfo resourceList(AppBookingResourceQuery query)
    {
        AppBookingResourceQuery effective = AppBookingResourceQuerySupport.normalize(query);
        AppBookingPageSupport.startPage(effective);
        List<AppointmentResourceVo> list = appBookingService.selectAppointmentResourceList(effective);
        return getDataTable(list);
    }

    /**
     * 专家历史评价（按评价时间倒序）。
     */
    @GetMapping("/expert/{expertResourceId}/reviews")
    public TableDataInfo expertReviews(@PathVariable("expertResourceId") Long expertResourceId,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize)
    {
        AppBookingPageSupport.startPage(pageNum, pageSize);
        List<BizServiceAppointmentReview> list = appBookingService.selectExpertReviewList(expertResourceId);
        return getDataTable(list);
    }

    /**
     * 当前用户在该服务方、所选日期是否已有有效预约。
     */
    @GetMapping("/appointment/booked")
    public AjaxResult checkBooked(@RequestParam("providerType") Integer providerType,
            @RequestParam("providerId") Long providerId,
            @RequestParam("appointDate") String appointDate)
    {
        AppBookingDateBookedVo vo = appBookingService.checkBookedOnDate(providerType, providerId, appointDate,
                SecurityUtils.getUsername());
        return success(vo);
    }

    /**
     * 提交服务预约。
     */
    @Log(title = "移动端服务预约", businessType = BusinessType.INSERT)
    @PostMapping("/appointment")
    public AjaxResult submit(@RequestBody AppBookingSubmitBody body)
    {
        Long id = appBookingService.submitAppointment(body, SecurityUtils.getUsername());
        return success(id);
    }
}
