package com.dora.controller;

import com.dora.dto.OrderDto;
import com.dora.service.OrderService;
import com.dora.util.SecurityUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/user/v1/orders")
@RequiredArgsConstructor
@Tag(name = "订单管理", description = "用户订单相关接口")
public class OrderController {

    private final OrderService orderService;

    /**
     * 获取用户订单列表
     */
    @GetMapping
    @Operation(summary = "获取用户订单列表", description = "获取当前用户的订单列表，支持分页")
    public OrderDto.OrderListResponse getUserOrders(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") Integer size) {
        
        Long userId = SecurityUtil.getCurrentUserId();
        if (userId == null) {
            OrderDto.OrderListResponse response = new OrderDto.OrderListResponse();
            response.setSuccess(false);
            response.setMessage("用户未登录");
            return response;
        }
        
        return orderService.getUserOrders(userId, page, size);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{orderId}")
    @Operation(summary = "获取订单详情", description = "获取指定订单的详细信息")
    public OrderDto.OrderDetailResponse getOrderDetail(
            @Parameter(description = "订单ID", example = "1") @PathVariable Long orderId) {
        
        Long userId = SecurityUtil.getCurrentUserId();
        if (userId == null) {
            OrderDto.OrderDetailResponse response = new OrderDto.OrderDetailResponse();
            response.setSuccess(false);
            response.setMessage("用户未登录");
            return response;
        }
        
        return orderService.getOrderDetail(userId, orderId);
    }
} 