package com.moka.gdtauto.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moka.gdtauto.annotation.AuthUser;
import com.moka.gdtauto.common.Result;
import com.moka.gdtauto.entity.BaseDailyCreativeReport;
import com.moka.gdtauto.entity.SysUser;
import com.moka.gdtauto.service.DailyReportService;
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.*;

/**
 * 动态创意日报表 Controller
 *
 * @author moka
 * @since 2026-03-03
 */
@Tag(name = "动态创意日报表", description = "双口径（REQUEST_TIME/REPORTING_TIME）动态创意日报表查询与同步")
@Slf4j
@RestController
@RequestMapping("/api/gdt/daily-report")
@RequiredArgsConstructor
public class DailyReportController {

    private final DailyReportService dailyReportService;

    /**
     * 手动触发同步（指定账户、日期范围、时间口径）
     */
    @Operation(summary = "手动同步日报表", description = "指定账户和日期范围，将腾讯广告日报数据同步到本地数据库")
    @PostMapping("/sync")
    public Result<Void> sync(
            @Parameter(description = "工号") @RequestParam String jobNumber,
            @Parameter(description = "组织账户ID") @RequestParam Long orgAccountId,
            @Parameter(description = "广告账户ID") @RequestParam Long accountId,
            @Parameter(description = "账号组ID") @RequestParam(defaultValue = "0") Long groupId,
            @Parameter(description = "账号组名称") @RequestParam(required = false) String groupName,
            @Parameter(description = "开始日期 yyyy-MM-dd") @RequestParam String startDate,
            @Parameter(description = "结束日期 yyyy-MM-dd") @RequestParam String endDate,
            @Parameter(description = "时间口径 REQUEST_TIME / REPORTING_TIME")
            @RequestParam(defaultValue = "REQUEST_TIME") String timeLine,
            @AuthUser SysUser user) {
        try {
            int saved = dailyReportService.syncReport(jobNumber, orgAccountId, accountId,
                    groupId, groupName, startDate, endDate, timeLine);
            return Result.<Void>success("同步成功，共写入 " + saved + " 条数据").withCount(saved);
        } catch (Exception e) {
            log.error("日报表同步失败 accountId={} timeLine={}", accountId, timeLine, e);
            return Result.fail("同步失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询日报表
     */
    @Operation(summary = "查询日报表列表", description = "分页查询指定口径的动态创意日报数据")
    @GetMapping("/list")
    public Result<?> list(
            @Parameter(description = "时间口径 REQUEST_TIME / REPORTING_TIME")
            @RequestParam(defaultValue = "REQUEST_TIME") String timeLine,
            @Parameter(description = "账号组ID（可选）") @RequestParam(required = false) Long groupId,
            @Parameter(description = "广告账户ID（可选）") @RequestParam(required = false) Long accountId,
            @Parameter(description = "动态创意ID（可选）") @RequestParam(required = false) Long dynamicCreativeId,
            @Parameter(description = "开始日期 yyyy-MM-dd（可选）") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期 yyyy-MM-dd（可选）") @RequestParam(required = false) String endDate,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") long page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "20") long pageSize) {
        try {
            Page<? extends BaseDailyCreativeReport> result =
                    dailyReportService.listReports(timeLine, groupId, accountId, dynamicCreativeId, startDate, endDate, page, pageSize);
            return Result.success(result.getRecords(), "查询成功")
                    .withPage(page, pageSize, result.getTotal());
        } catch (Exception e) {
            log.error("查询日报表失败 timeLine={} groupId={} accountId={} dynamicCreativeId={}", timeLine, groupId, accountId, dynamicCreativeId, e);
            return Result.fail("查询失败：" + e.getMessage());
        }
    }
}
