package com.snack.server.module.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snack.server.common.Result;
import com.snack.server.module.order.dto.req.OrderPageReq;
import com.snack.server.module.order.service.OrderService;
import com.snack.server.module.order.vo.OrderDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理（管理端）
 */
@Tag(name = "订单管理")
@RestController
@RequestMapping("/api/admin/order")
@RequiredArgsConstructor
public class OrderAdminController {

    private final OrderService orderService;

    @Operation(summary = "订单分页（管理端）")
    @SaCheckLogin
    @GetMapping("/page")
    public Result<Page<OrderDetailVO>> page(OrderPageReq req) {
        return Result.ok(orderService.pageAllOrders(req));
    }

    @Operation(summary = "订单详情（管理端）")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<OrderDetailVO> detail(@Parameter(description = "订单 ID") @PathVariable Long id) {
        return Result.ok(orderService.getDetailForAdmin(id));
    }

    @Operation(summary = "订单发货")
    @SaCheckLogin
    @PostMapping("/{id}/deliver")
    public Result<Void> deliver(
            @Parameter(description = "订单 ID") @PathVariable Long id,
            @Parameter(description = "物流公司") @RequestParam String company,
            @Parameter(description = "物流单号") @RequestParam String trackingNo) {
        orderService.deliverOrder(id, company, trackingNo);
        return Result.ok();
    }

    @Operation(summary = "管理员取消订单")
    @SaCheckLogin
    @PostMapping("/{id}/cancel")
    public Result<Void> cancel(
            @Parameter(description = "订单 ID") @PathVariable Long id,
            @Parameter(description = "原因") @RequestParam(required = false, defaultValue = "管理员取消") String reason) {
        orderService.adminCancelOrder(id, reason);
        return Result.ok();
    }
}
