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.Result;
import com.moka.gdtauto.entity.BaseDailyCreativeReport;
import com.moka.gdtauto.service.DailyCreativeReportService;

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;

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

    private final DailyCreativeReportService dailyReportService;

    /**
     * 分页查询日报表
     */
    @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());
        }
    }
}
