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.TenantProductBundle;
import com.reading.platform.service.TenantProductBundleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员 - 学校套餐购买记录管理控制器
 */
@Tag(name = "管理员 - 学校套餐购买记录", description = "学校套餐购买记录管理接口")
@RestController
@RequestMapping("/api/v1/admin/tenant-bundles")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminTenantBundleController {

    private final TenantProductBundleService tenantBundleService;

    @Operation(summary = "获取学校套餐购买记录列表")
    @GetMapping
    public Result<PageResult<TenantProductBundle>> getTenantBundles(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int pageSize,
            @RequestParam(required = false) String tenantId,
            @RequestParam(required = false) String status) {
        Page<TenantProductBundle> result = tenantBundleService.getTenantBundlePage(page, pageSize, tenantId, status);
        return Result.success(PageResult.of(result));
    }

    @Operation(summary = "根据 ID 获取购买记录")
    @GetMapping("/{id}")
    public Result<TenantProductBundle> getTenantBundle(@PathVariable String id) {
        return Result.success(tenantBundleService.getTenantBundleById(id));
    }

    @Operation(summary = "获取学校的套餐列表")
    @GetMapping("/tenant/{tenantId}")
    public Result<List<TenantProductBundle>> getTenantBundlesByTenantId(@PathVariable String tenantId) {
        return Result.success(tenantBundleService.getTenantBundlesByTenantId(tenantId));
    }

    @Operation(summary = "获取学校当前有效的套餐")
    @GetMapping("/tenant/{tenantId}/active")
    public Result<TenantProductBundle> getActiveTenantBundle(@PathVariable String tenantId) {
        TenantProductBundle bundle = tenantBundleService.getActiveTenantBundle(tenantId);
        if (bundle == null) {
            return Result.success(null);
        }
        return Result.success(bundle);
    }

    @Operation(summary = "创建购买记录")
    @PostMapping
    public Result<TenantProductBundle> createTenantBundle(@RequestBody TenantProductBundle record) {
        return Result.success(tenantBundleService.createTenantBundle(record));
    }

    @Operation(summary = "更新购买记录")
    @PutMapping("/{id}")
    public Result<TenantProductBundle> updateTenantBundle(
            @PathVariable String id,
            @RequestBody TenantProductBundle record) {
        return Result.success(tenantBundleService.updateTenantBundle(id, record));
    }

    @Operation(summary = "激活套餐")
    @PostMapping("/{id}/activate")
    public Result<Void> activateBundle(@PathVariable String id) {
        tenantBundleService.activateBundle(id);
        return Result.success();
    }

    @Operation(summary = "过期套餐")
    @PostMapping("/{id}/expire")
    public Result<Void> expireBundle(@PathVariable String id) {
        tenantBundleService.expireBundle(id);
        return Result.success();
    }

    @Operation(summary = "暂停套餐")
    @PostMapping("/{id}/suspend")
    public Result<Void> suspendBundle(@PathVariable String id) {
        tenantBundleService.suspendBundle(id);
        return Result.success();
    }
}
