package com.ruoyi.app.flashdelivery.controller;

import com.ruoyi.app.flashdelivery.dto.FlashDeliveryPricingRequest;
import com.ruoyi.app.flashdelivery.dto.FlashDeliveryReasonRequest;
import com.ruoyi.app.flashdelivery.service.FlashDeliveryApplicationService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
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;

/** 平台端闪送管理接口。 */
@RestController
@RequestMapping("/system/flashDelivery/admin")
public class FlashDeliveryAdminController extends BaseController {
    private final FlashDeliveryApplicationService service;

    public FlashDeliveryAdminController(FlashDeliveryApplicationService service) {
        this.service = service;
    }

    @PreAuthorize("@ss.hasPermi('flash:pricing:list')")
    @GetMapping("/pricing")
    public AjaxResult pricing() {
        return success(service.adminPricing());
    }

    @PreAuthorize("@ss.hasPermi('flash:pricing:edit')")
    @Log(title = "闪送时段运价", businessType = BusinessType.INSERT)
    @PostMapping("/pricing")
    public AjaxResult createPricing(@RequestBody FlashDeliveryPricingRequest request) {
        return success(service.createPricing(SecurityUtils.getUserId(), request));
    }

    @PreAuthorize("@ss.hasPermi('flash:pricing:edit')")
    @Log(title = "闪送时段运价", businessType = BusinessType.UPDATE)
    @PutMapping("/pricing/{id}")
    public AjaxResult updatePricing(@PathVariable Long id,
                                    @RequestBody FlashDeliveryPricingRequest request) {
        return success(service.updatePricing(SecurityUtils.getUserId(), id, request));
    }

    @PreAuthorize("@ss.hasPermi('flash:pricing:edit')")
    @Log(title = "闪送时段运价", businessType = BusinessType.DELETE)
    @DeleteMapping("/pricing/{id}")
    public AjaxResult deletePricing(@PathVariable Long id) {
        service.deletePricing(id);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('flash:order:list')")
    @GetMapping("/orders")
    public AjaxResult orders(@RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "10") int pageSize,
                             @RequestParam(required = false) String status,
                             @RequestParam(required = false) String serviceType,
                             @RequestParam(required = false) String orderNo,
                             @RequestParam(required = false) Long userId,
                             @RequestParam(required = false) Long riderId) {
        return success(service.adminOrders(pageNum, pageSize, status, serviceType, orderNo, userId, riderId));
    }

    @PreAuthorize("@ss.hasPermi('flash:order:query')")
    @GetMapping("/orders/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        return success(service.adminDetail(id));
    }

    @PreAuthorize("@ss.hasPermi('flash:order:cancel')")
    @Log(title = "闪送订单取消", businessType = BusinessType.UPDATE)
    @PostMapping("/orders/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id, @RequestBody FlashDeliveryReasonRequest request) {
        service.adminCancel(SecurityUtils.getUserId(), id, request);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('flash:order:complete')")
    @Log(title = "闪送订单完成", businessType = BusinessType.UPDATE)
    @PostMapping("/orders/{id}/complete")
    public AjaxResult complete(@PathVariable Long id) {
        if (!service.complete(SecurityUtils.getUserId(), "ADMIN", id)) {
            return error(MessageUtils.message("flash.delivery.complete.not.allowed"));
        }
        return success();
    }
}
