package com.moka.gdtauto.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moka.gdtauto.common.Constant;
import com.moka.gdtauto.common.Result;
import com.moka.gdtauto.entity.AdPlan;
import com.moka.gdtauto.service.AdPlanService;

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;

/**
 * 广告计划管理控制器
 * 
 * @author moka
 * @since 2026-02-24
 */
@Tag(name = "广告计划管理", description = "广告计划的创建、更新、删除及查询")
@Slf4j
@RestController
@RequestMapping("/api/ad-plan")
@RequiredArgsConstructor
public class AdPlanController {

    private final AdPlanService adPlanService;

    
    /**
     * 获取广告计划列表（分页）
     */
    @Operation(summary = "获取广告计划列表", description = "分页获取广告计划列表，可按账户ID和状态筛选")
    @GetMapping("/list")
    public Result<Page<AdPlan>> getAdPlans(
            @Parameter(description = "配置ID", example = "123456")
            @RequestParam Long confId,
            @Parameter(description = "账户ID，可选", example = "123456")
            @RequestParam(required = false) Long accountId,
            @Parameter(description = "广告ID，可选", example = "123456")
            @RequestParam(required = false) Long adgroupId,
            @Parameter(description = "状态，可选：1-待执行，2-执行中，3-失败，4-已结束", example = "1")
            @RequestParam(required = false) Integer status,
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<AdPlan> page = adPlanService.getAdPlans(confId,accountId,adgroupId, status, pageNum, pageSize);
            page.getRecords().forEach(r -> {
                r.setSystemStatus(Constant.getAdGroupStatusValue(r.getSystemStatus()));
            });
            return Result.success(page, "查询成功");
        } catch (Exception e) {
            log.error("查询广告计划列表失败", e);
            return Result.fail("查询广告计划列表失败: " + e.getMessage());
        }
    }
}
