package com.ai.da.controller;

import com.ai.da.common.response.Response;
import com.ai.da.common.task.SubscriptionReminderTask;
import com.ai.da.mapper.primary.entity.SubscriptionPlan;
import com.ai.da.model.dto.SubscriptionPlanDTO;
import com.ai.da.model.dto.SubscriptionPlanPageQuery;
import com.ai.da.model.dto.UpdateSubscriptionPlanDTO;
import com.ai.da.model.vo.SubscriptionPlanVO;
import com.ai.da.service.SubscriptionPlanService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订阅计划模块")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/subscription_plan")
public class SubscriptionPlanController {

    private final SubscriptionPlanService subscriptionPlanService;

    private final SubscriptionReminderTask subscriptionReminderTask;

    @Operation(summary = "创建订阅计划")
    @PostMapping("/createPlan")
    public Response<String> createPlan(@Valid @RequestBody SubscriptionPlanDTO subscriptionPlanDTO) {
        subscriptionPlanService.createPlan(subscriptionPlanDTO);
        return Response.success();
    }

    @Operation(summary = "更新订阅计划")
    @PostMapping("/updatePlan")
    public Response<String> updatePlan(@Valid @RequestBody UpdateSubscriptionPlanDTO updateDTO) {
        subscriptionPlanService.updatePlan(updateDTO);
        return Response.success();
    }

    @Operation(summary = "搜索订阅计划")
    @PostMapping("/searchByOrganizationIdAndStatus")
    public Response<List<SubscriptionPlan>> searchByOrganizationIdAndStatus(@Valid @RequestBody SubscriptionPlanPageQuery subscriptionPlanPageQuery) {
        return Response.success(subscriptionPlanService.searchByOrganizationIdAndStatus(subscriptionPlanPageQuery));
    }

    @Operation(summary = "分页搜索订阅计划")
    @PostMapping("/searchByPage")
    public Response<IPage<SubscriptionPlanVO>> searchByPage(@Valid @RequestBody SubscriptionPlanPageQuery subscriptionPlanPageQuery) {
        IPage<SubscriptionPlanVO> subscriptionPlanVOIPage = subscriptionPlanService.searchByPage(subscriptionPlanPageQuery);
        return Response.success(subscriptionPlanVOIPage);
    }

    @Operation(summary = "删除订阅计划")
    @GetMapping("/deletePlan")
    public Response<String> deletePlan(@RequestParam Long id) {
        subscriptionPlanService.deletePlan(id);
        return Response.success();
    }

    @Operation(summary = "管理员切换订阅计划")
    @GetMapping("/switchSubscriptionPlan")
    public Response<String> switchSubscriptionPlan(@RequestParam Long targetSubscriptionPlanId, @RequestParam(required = false) Long adminAccId) {
        subscriptionPlanService.switchSubscriptionPlan(targetSubscriptionPlanId, adminAccId);
        return Response.success();
    }

    @Operation(summary = "子账号切换订阅计划")
    @GetMapping("/switchSubAccSubscriptionPlan")
    public Response<String> switchSubAccSubscriptionPlan(@RequestParam Long targetSubscriptionPlanId, @RequestParam Long subAccId) {
        subscriptionPlanService.switchSubAccSubscriptionPlan(targetSubscriptionPlanId, subAccId);
        return Response.success();
    }

//    @Hidden
    @Operation(summary = "activeSubscriptionPlan")
    @GetMapping("/activeSubscriptionPlan")
    public Response<String> activeSubscriptionPlan() {
        subscriptionPlanService.activeSubscriptionPlan();
        return Response.success();
    }

//    @Hidden
    @Operation(summary = "expireSubscription")
    @GetMapping("/expireSubscription")
    public Response<String> expireSubscription() {
        subscriptionPlanService.expireSubscription();
        return Response.success();
    }

    @Operation(summary = "subscriptionReminder")
    @GetMapping("/subscriptionReminder")
    public Response<String> subscriptionReminder() {
        subscriptionReminderTask.subscriptionReminder();
        return Response.success();
    }

    @Operation(summary = "trialReminder")
    @GetMapping("/trialReminder")
    public Response<String> trialReminder() {
        subscriptionReminderTask.trialReminder();
        return Response.success();
    }


}
