package com.moka.gdtauto.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.AdPlanCreative;
import com.moka.gdtauto.service.AdPlanCreativeService;

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-creative")
@RequiredArgsConstructor
public class AdPlanCreativeController {

    private final AdPlanCreativeService adPlanCreativeService;

    /**
     * 删除广告计划创意
     */
    @Operation(summary = "删除广告计划创意", description = "删除指定的广告计划创意")
    @DeleteMapping("/{creativeId}")
    public Result<Void> deleteAdPlanCreative(
            @Parameter(description = "创意ID", required = true)
            @PathVariable Long creativeId) {
        try {
            boolean success = adPlanCreativeService.deleteAdPlanCreative(creativeId);
            if (success) {
                return Result.success("删除广告计划创意成功");
            } else {
                return Result.fail("广告计划创意不存在");
            }
        } catch (Exception e) {
            log.error("删除广告计划创意失败", e);
            return Result.fail("删除广告计划创意失败: " + e.getMessage());
        }
    }

    /**
     * 根据计划ID获取创意列表
     */
    @Operation(summary = "根据计划ID获取创意列表", description = "获取指定广告计划的所有创意")
    @GetMapping("/plan/{planId}")
    public Result<List<AdPlanCreative>> getCreativesByPlanId(
            @Parameter(description = "计划ID", required = true)
            @PathVariable Long planId) {
        try {
            List<AdPlanCreative> creatives = adPlanCreativeService.getCreativesByPlanId(planId);
            creatives.forEach(c -> {
                c.setApprovalStatus(Constant.getCreativeSetApprovalStatusValue(c.getApprovalStatus()));
            });
            return Result.success(creatives, "查询成功");
        } catch (Exception e) {
            log.error("查询广告计划创意列表失败", e);
            return Result.fail("查询广告计划创意列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取广告计划创意列表（分页）
     */
    @Operation(summary = "获取广告计划创意列表", description = "分页获取广告计划创意列表，可按计划ID和状态筛选")
    @GetMapping("/list")
    public Result<Page<AdPlanCreative>> getAdPlanCreatives(
            @Parameter(description = "配置ID，可选", example = "1")
            @RequestParam(required = false) Long adPlanConfId,
            @Parameter(description = "计划ID，可选", example = "1")
            @RequestParam(required = false) Long planId,
            @Parameter(description = "md5", example = "1")
            @RequestParam(required = false) String md5,
            @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<AdPlanCreative> page = adPlanCreativeService.lambdaQuery()
                    .eq(adPlanConfId != null, AdPlanCreative::getAdPlanConfId, adPlanConfId)
                    .eq(planId != null, AdPlanCreative::getAdPlanId, planId)
                    .eq(StringUtils.isNotBlank(md5), AdPlanCreative::getCreativeFileMd5, md5)
                    .eq(status != null, AdPlanCreative::getStatus, status)
                    .orderByDesc(AdPlanCreative::getId)
                    .page(new Page<>(pageNum, pageSize));
            return Result.success(page, "查询成功");
        } catch (Exception e) {
            log.error("查询广告计划创意列表失败", e);
            return Result.fail("查询广告计划创意列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取广告计划创意详情
     */
    @Operation(summary = "获取广告计划创意详情", description = "根据ID获取广告计划创意详细信息")
    @GetMapping("/{creativeId}")
    public Result<AdPlanCreative> getAdPlanCreative(
            @Parameter(description = "创意ID", required = true)
            @PathVariable Long creativeId) {
        try {
            AdPlanCreative adPlanCreative = adPlanCreativeService.getAdPlanCreativeById(creativeId);
            if (adPlanCreative != null) {
                return Result.success(adPlanCreative, "查询成功");
            } else {
                return Result.fail("广告计划创意不存在");
            }
        } catch (Exception e) {
            log.error("查询广告计划创意详情失败", e);
            return Result.fail("查询广告计划创意详情失败: " + e.getMessage());
        }
    }
}
