package com.moka.gdtauto.controller;

import com.moka.gdtauto.common.Result;
import com.moka.gdtauto.entity.TencentAdsAdgroup;
import com.moka.gdtauto.service.TencentAdsAdgroupService;
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;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.moka.gdtauto.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moka.gdtauto.annotation.AuthUser;


/**
 * 腾讯广告组API控制器
 * 
 * @author moka
 * @since 2026-02-04
 */
@Slf4j
@RestController
@RequestMapping("/adgroups")
@RequiredArgsConstructor
@Tag(name = "广告组管理", description = "腾讯广告组数据同步与查询")
public class TencentAdsAdgroupController {

    private final TencentAdsAdgroupService adgroupService;

    /**
     * 同步广告组数据
     */
    @Operation(summary = "同步广告组数据", description = "从腾讯广告平台同步指定账户的广告组数据到数据库")
    @PostMapping("/sync")
    public Result<Void> syncAdgroups(
            @Parameter(description = "授权账号ID", required = true, example = "66612379")
            @RequestParam Long orgAccountId,
            @Parameter(description = "账户ID", required = false, example = "66612379")
            @RequestParam Long accountId,
            @Parameter(description = "开始时间戳（秒）", example = "1708520382")
            @RequestParam(required = false) Long startTime,
            @Parameter(description = "结束时间戳（秒）", example = "1708568262")
            @RequestParam(required = false) Long endTime, 
            @AuthUser SysUser user) {
        
        try {
            int count = adgroupService.syncAdgroups(user.getJobNumber(), orgAccountId, accountId, startTime, endTime);
            return Result.<Void>success("同步成功，共同步" + count + "条广告组数据")
                    .withCount(count);
        } catch (Exception e) {
            log.error("同步广告组数据失败", e);
            return Result.fail("同步失败：" + e.getMessage());
        }
    }

    /**
     * 查询广告组列表
     */
    @Operation(summary = "查询广告组列表", description = "分页查询广告组数据")
    @GetMapping("/list")
    public Result<List<TencentAdsAdgroup>> listAdgroups(
            @Parameter(description = "账户ID（可选）", example = "66612379")
            @RequestParam(required = false) Long accountId,
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量", example = "20")
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        try {
            LambdaQueryWrapper<TencentAdsAdgroup> wrapper = new LambdaQueryWrapper<>();
            if (accountId != null) {
                wrapper.eq(TencentAdsAdgroup::getAccountId, accountId);
            }
            Page<TencentAdsAdgroup> pageResult = adgroupService.page(new Page<>(page, pageSize), wrapper);
            List<TencentAdsAdgroup> list = pageResult.getRecords();
            long total = pageResult.getTotal();
            return Result.success(list, "查询成功")
                    .withPage(page.longValue(), pageSize.longValue(), total);
        } catch (Exception e) {
            log.error("查询广告组列表失败", e);
            return Result.fail("查询失败：" + e.getMessage());
        }
    }
}
