package com.dora.controller;

import com.dora.annotation.RequireAdminOrStaff;
import com.dora.dto.ApiResponse;
import com.dora.dto.AdminOrderDto;
import com.dora.service.AdminOrderService;
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.*;

import jakarta.validation.Valid;

/**
 * 管理员会员套餐管理控制器
 */
@RestController
@RequestMapping("/admin/membership-plans")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "管理员会员套餐管理", description = "管理员会员套餐管理相关接口")
@RequireAdminOrStaff
public class AdminMembershipController {

    private final AdminOrderService adminOrderService;

    @GetMapping("/list")
    @Operation(summary = "获取会员套餐列表", description = "管理员查看所有会员套餐")
    public ApiResponse<AdminOrderDto.MembershipPlanListResponse> getMembershipPlanList(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size,
            @Parameter(description = "是否仅显示上架套餐") @RequestParam(defaultValue = "false") Boolean activeOnly) {
        
        log.info("收到获取会员套餐列表请求");
        
        AdminOrderDto.MembershipPlanListRequest request = new AdminOrderDto.MembershipPlanListRequest();
        request.setPage(page);
        request.setSize(size);
        request.setActiveOnly(activeOnly);
        
        return adminOrderService.getMembershipPlanList(request);
    }

    @PostMapping
    @Operation(summary = "创建会员套餐", description = "管理员创建新的会员套餐")
    public ApiResponse<String> createMembershipPlan(
            @Valid @RequestBody AdminOrderDto.MembershipPlanCreateRequest request) {
        
        log.info("收到创建会员套餐请求，套餐名称：{}", request.getName());
        return adminOrderService.createMembershipPlan(request);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新会员套餐", description = "管理员更新现有会员套餐")
    public ApiResponse<String> updateMembershipPlan(
            @Parameter(description = "套餐ID") @PathVariable Long id,
            @Valid @RequestBody AdminOrderDto.MembershipPlanUpdateRequest request) {
        
        log.info("收到更新会员套餐请求，套餐ID：{}", id);
        request.setId(id);
        return adminOrderService.updateMembershipPlan(request);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除会员套餐", description = "管理员删除指定会员套餐")
    public ApiResponse<String> deleteMembershipPlan(
            @Parameter(description = "套餐ID") @PathVariable Long id) {
        
        log.info("收到删除会员套餐请求，套餐ID：{}", id);
        return adminOrderService.deleteMembershipPlan(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取套餐详情", description = "获取指定套餐的详细信息")
    public ApiResponse<AdminOrderDto.MembershipPlanDetailResponse> getMembershipPlanDetail(
            @Parameter(description = "套餐ID") @PathVariable Long id) {
        
        log.info("收到获取套餐详情请求，套餐ID：{}", id);
        return adminOrderService.getMembershipPlanDetail(id);
    }
}
