package com.moka.gdtauto.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告计划创意管理控制器
 * 
 * @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 = "创建新的广告计划创意")
    @PostMapping("/create")
    public Result<Long> createAdPlanCreative(@RequestBody AdPlanCreative adPlanCreative) {
        try {
            Long creativeId = adPlanCreativeService.createAdPlanCreative(adPlanCreative);
            return Result.success(creativeId, "创建广告计划创意成功");
        } catch (Exception e) {
            log.error("创建广告计划创意失败", e);
            return Result.fail("创建广告计划创意失败: " + e.getMessage());
        }
    }

    /**
     * 批量创建广告计划创意
     */
    @Operation(summary = "批量创建广告计划创意", description = "批量创建多个广告计划创意")
    @PostMapping("/batch-create")
    public Result<Void> batchCreateAdPlanCreatives(@RequestBody List<AdPlanCreative> adPlanCreatives) {
        try {
            boolean success = adPlanCreativeService.batchCreateAdPlanCreatives(adPlanCreatives);
            if (success) {
                return Result.success("批量创建广告计划创意成功");
            } else {
                return Result.fail("批量创建广告计划创意失败");
            }
        } catch (Exception e) {
            log.error("批量创建广告计划创意失败", e);
            return Result.fail("批量创建广告计划创意失败: " + e.getMessage());
        }
    }

    /**
     * 更新广告计划创意
     */
    @Operation(summary = "更新广告计划创意", description = "更新指定的广告计划创意")
    @PostMapping("/update")
    public Result<Void> updateAdPlanCreative(@RequestBody AdPlanCreative adPlanCreative) {
        try {
            boolean success = adPlanCreativeService.updateAdPlanCreative(adPlanCreative);
            if (success) {
                return Result.success("更新广告计划创意成功");
            } else {
                return Result.fail("广告计划创意不存在");
            }
        } catch (Exception e) {
            log.error("更新广告计划创意失败", e);
            return Result.fail("更新广告计划创意失败: " + e.getMessage());
        }
    }

    /**
     * 删除广告计划创意
     */
    @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);
            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 planId,
            @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.getAdPlanCreatives(
                    planId, status, 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());
        }
    }
}
