package com.example.demo.order;

import com.example.demo.common.AppDefaultsProperties;
import com.example.demo.order.dto.OrderDtos;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class OrderController {

    private final OrderService orderService;
    private final AppDefaultsProperties defaults;

    public OrderController(OrderService orderService, AppDefaultsProperties defaults) {
        this.orderService = orderService;
        this.defaults = defaults;
    }

    @PostMapping("/orders")
    public ResponseEntity<?> createOrder(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                         @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                         @RequestBody OrderDtos.CreateOrderRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        return ResponseEntity.ok(orderService.create(sid, uid, req));
    }

    @PostMapping("/payments/{biz}")
    public ResponseEntity<?> createPayments(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                            @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                            @PathVariable("biz") String biz,
                                            @RequestBody java.util.List<OrderDtos.PaymentItem> req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        return ResponseEntity.ok(orderService.createPayments(sid, uid, req, biz));
    }

    @PostMapping("/orders/{id}/void")
    public ResponseEntity<?> voidOrder(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                       @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                       @PathVariable("id") Long id,
                                       @RequestParam("type") String type) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        orderService.voidOrder(sid, uid, id, type);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/orders")
    public ResponseEntity<?> list(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                  @RequestParam(name = "biz", required = false) String biz,
                                  @RequestParam(name = "type", required = false) String type,
                                  @RequestParam(name = "kw", required = false) String kw,
                                  @RequestParam(name = "page", defaultValue = "1") int page,
                                  @RequestParam(name = "size", defaultValue = "20") int size,
                                  @RequestParam(name = "startDate", required = false) String startDate,
                                  @RequestParam(name = "endDate", required = false) String endDate) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(orderService.list(sid, biz, type, kw, Math.max(0, page-1), size, startDate, endDate));
    }

    @GetMapping("/orders/{id}")
    public ResponseEntity<?> getOrderDetail(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                            @PathVariable("id") Long id) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(orderService.getSalesOrderDetail(sid, id));
    }

    // 兼容前端直接调用 /api/purchase-orders
    @GetMapping("/purchase-orders")
    public ResponseEntity<?> purchaseOrders(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                            @RequestParam(name = "status", required = false) String status,
                                            @RequestParam(name = "kw", required = false) String kw,
                                            @RequestParam(name = "page", defaultValue = "1") int page,
                                            @RequestParam(name = "size", defaultValue = "20") int size,
                                            @RequestParam(name = "startDate", required = false) String startDate,
                                            @RequestParam(name = "endDate", required = false) String endDate) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        String type = ("returned".equalsIgnoreCase(status) ? "purchase.return" : "purchase.in");
        return ResponseEntity.ok(orderService.list(sid, "purchase", type, kw, Math.max(0, page-1), size, startDate, endDate));
    }

    @GetMapping("/purchase-orders/{id}")
    public ResponseEntity<?> getPurchaseOrderDetail(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                                    @PathVariable("id") Long id) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(orderService.getPurchaseOrderDetail(sid, id));
    }

    @GetMapping("/payments")
    public ResponseEntity<?> listPayments(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                          @RequestParam(name = "direction", required = false) String direction,
                                          @RequestParam(name = "bizType", required = false) String bizType,
                                          @RequestParam(name = "accountId", required = false) Long accountId,
                                          @RequestParam(name = "kw", required = false) String kw,
                                          @RequestParam(name = "page", defaultValue = "1") int page,
                                          @RequestParam(name = "size", defaultValue = "20") int size,
                                          @RequestParam(name = "startDate", required = false) String startDate,
                                          @RequestParam(name = "endDate", required = false) String endDate) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(orderService.listPayments(sid, direction, bizType, accountId, kw, Math.max(0, page-1), size, startDate, endDate));
    }

    @GetMapping("/other-transactions")
    public ResponseEntity<?> listOtherTransactions(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                                   @RequestParam(name = "type", required = false) String type,
                                                   @RequestParam(name = "accountId", required = false) Long accountId,
                                                   @RequestParam(name = "kw", required = false) String kw,
                                                   @RequestParam(name = "page", defaultValue = "1") int page,
                                                   @RequestParam(name = "size", defaultValue = "20") int size,
                                                   @RequestParam(name = "startDate", required = false) String startDate,
                                                   @RequestParam(name = "endDate", required = false) String endDate) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(orderService.listOtherTransactions(sid, type, accountId, kw, Math.max(0, page-1), size, startDate, endDate));
    }

    @PostMapping("/other-transactions")
    public ResponseEntity<?> createOtherTransaction(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                                    @RequestHeader(name = "X-User-Id", required = false) Long userId,
                                                    @RequestBody OrderDtos.CreateOtherTransactionRequest req) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        Long uid = (userId == null ? defaults.getUserId() : userId);
        return ResponseEntity.ok(orderService.createOtherTransaction(sid, uid, req));
    }

    @GetMapping("/inventories/logs")
    public ResponseEntity<?> listInventoryLogs(@RequestHeader(name = "X-Shop-Id", required = false) Long shopId,
                                               @RequestParam(name = "productId", required = false) Long productId,
                                               @RequestParam(name = "reason", required = false) String reason,
                                               @RequestParam(name = "kw", required = false) String kw,
                                               @RequestParam(name = "page", defaultValue = "1") int page,
                                               @RequestParam(name = "size", defaultValue = "20") int size,
                                               @RequestParam(name = "startDate", required = false) String startDate,
                                               @RequestParam(name = "endDate", required = false) String endDate) {
        Long sid = (shopId == null ? defaults.getShopId() : shopId);
        return ResponseEntity.ok(orderService.listInventoryLogs(sid, productId, reason, kw, Math.max(0, page-1), size, startDate, endDate));
    }
}


