package com.erp.tenant.controller;

import com.erp.common.core.model.ApiResponse;
import com.erp.common.core.model.PageResponse;
import com.erp.tenant.entity.PricePlan;
import com.erp.tenant.service.PricePlanService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 价格方案控制器（超级管理员使用）
 */
@Slf4j
@RestController
@RequestMapping("/api/sa/price-plans")
@RequiredArgsConstructor
public class PricePlanController {
    
    private final PricePlanService pricePlanService;
    
    /**
     * 获取价格方案列表
     */
    @GetMapping
    public ApiResponse<PageResponse<PricePlan>> listPricePlans(
            @RequestParam(required = false) String period_type,
            @RequestParam(required = false) Boolean is_active,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "15") Integer pageSize) {
        
        return ApiResponse.success(pricePlanService.listPricePlans(period_type, is_active, page, pageSize));
    }
    
    /**
     * 获取价格方案详情
     */
    @GetMapping("/{id}")
    public ApiResponse<PricePlan> getPricePlanById(@PathVariable Long id) {
        return ApiResponse.success(pricePlanService.getPricePlanById(id));
    }
    
    /**
     * 创建价格方案
     */
    @PostMapping
    public ApiResponse<PricePlan> createPricePlan(@RequestBody PricePlan pricePlan) {
        return ApiResponse.success("创建成功", pricePlanService.createPricePlan(pricePlan));
    }
    
    /**
     * 更新价格方案
     */
    @PutMapping("/{id}")
    public ApiResponse<PricePlan> updatePricePlan(@PathVariable Long id, @RequestBody PricePlan pricePlan) {
        return ApiResponse.success("更新成功", pricePlanService.updatePricePlan(id, pricePlan));
    }
    
    /**
     * 删除价格方案
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deletePricePlan(@PathVariable Long id) {
        pricePlanService.deletePricePlan(id);
        return ApiResponse.<Void>success("删除成功", null);
    }
    
    /**
     * 切换价格方案状态
     */
    @PostMapping("/{id}/toggle-status")
    public ApiResponse<Boolean> togglePricePlanStatus(@PathVariable Long id) {
        return ApiResponse.success("状态切换成功", pricePlanService.togglePricePlanStatus(id));
    }
    
    /**
     * 设置默认价格方案
     */
    @PostMapping("/{id}/set-default")
    public ApiResponse<Void> setDefaultPricePlan(@PathVariable Long id) {
        pricePlanService.setDefaultPricePlan(id);
        return ApiResponse.<Void>success("设置成功", null);
    }
}
