package com.reading.platform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.entity.ProductBundle;
import com.reading.platform.service.ProductBundleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员 - 套餐管理控制器
 */
@Tag(name = "管理员 - 套餐管理", description = "产品套餐管理接口（管理员专用）")
@RestController
@RequestMapping("/api/v1/admin/bundles")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminProductBundleController {

    private final ProductBundleService productBundleService;

    @Operation(summary = "获取套餐列表")
    @GetMapping
    public Result<PageResult<ProductBundle>> getBundles(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Page<ProductBundle> result = productBundleService.getBundlePage(page, pageSize, keyword, status);
        return Result.success(PageResult.of(result));
    }

    @Operation(summary = "根据 ID 获取套餐")
    @GetMapping("/{id}")
    public Result<ProductBundle> getBundle(@PathVariable String id) {
        return Result.success(productBundleService.getBundleById(id));
    }

    @Operation(summary = "创建套餐")
    @PostMapping
    public Result<ProductBundle> createBundle(@RequestBody ProductBundle bundle) {
        return Result.success(productBundleService.createBundle(bundle));
    }

    @Operation(summary = "更新套餐")
    @PutMapping("/{id}")
    public Result<ProductBundle> updateBundle(@PathVariable String id, @RequestBody ProductBundle bundle) {
        return Result.success(productBundleService.updateBundle(id, bundle));
    }

    @Operation(summary = "删除套餐")
    @DeleteMapping("/{id}")
    public Result<Void> deleteBundle(@PathVariable String id) {
        productBundleService.deleteBundle(id);
        return Result.success();
    }

    @Operation(summary = "提交套餐审核")
    @PostMapping("/{id}/submit")
    public Result<Void> submitBundle(@PathVariable String id) {
        productBundleService.submitBundle(id);
        return Result.success();
    }

    @Operation(summary = "审核套餐（通过或拒绝）")
    @PostMapping("/{id}/review")
    public Result<Void> reviewBundle(
            @PathVariable String id,
            @RequestBody java.util.Map<String, Object> body) {
        Boolean approvedObj = (Boolean) body.get("approved");
        boolean approved = approvedObj != null && approvedObj;
        String comment = (String) body.get("comment");
        productBundleService.reviewBundle(id, approved, comment);
        return Result.success();
    }

    @Operation(summary = "发布套餐")
    @PostMapping("/{id}/publish")
    public Result<Void> publishBundle(@PathVariable String id) {
        productBundleService.publishBundle(id);
        return Result.success();
    }

    @Operation(summary = "下架套餐")
    @PostMapping("/{id}/offline")
    public Result<Void> offlineBundle(@PathVariable String id) {
        productBundleService.offlineBundle(id);
        return Result.success();
    }

    @Operation(summary = "获取系统套餐列表")
    @GetMapping("/system")
    public Result<PageResult<ProductBundle>> getSystemBundles(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String gradeLevel) {
        Page<ProductBundle> result = productBundleService.getSystemBundles(page, pageSize, gradeLevel);
        return Result.success(PageResult.of(result));
    }
}
