package com.pig4cloud.pig.marketing.controller;


import com.pig4cloud.pig.common.core.exception.BusinessException;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.marketing.api.dto.config.*;
import com.pig4cloud.pig.marketing.api.vo.config.*;
import com.pig4cloud.pig.marketing.service.MarketingConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author: lwh
 * @date: 2025-07-15
 * @description: 营销配置管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/config")
@Tag(description = "marketingConfig", name = "营销配置管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MarketingConfigController {

	private final MarketingConfigService marketingConfigService;

	/**
	 * 添加分组
	 * @return 添加结果
	 */
	@PostMapping("/addGroup")
	@Operation(summary = "添加分组")
	public R addMarketingConfigGroup(@Valid @RequestBody AddMarketingGroupDTO reqDto) {
		return marketingConfigService.addMarketingConfigGroup(reqDto);
	}

	/**
	 * 删除分组
	 * @return 删除结果
	 */
	@PostMapping("/delGroup")
	@Operation(summary = "删除分组")
	public R<Boolean> delMarketingConfigGroup(@Valid @RequestBody DelMarketingGroupDTO reqDto) {
		return R.ok(marketingConfigService.delMarketingConfigGroup(reqDto));
	}

	/**
	 * 根据ID查询分组详情
	 * @return R
	 */
	@GetMapping("/group/detail")
	@Operation(summary = "根据ID查询分组详情")
	public R getMarketingGroupDetail(@ParameterObject  Long id) {
		if (id == null){
			throw new BusinessException("分组ID不能为空");
		}
		return marketingConfigService.getMarketingGroupDetail(id);
	}

	/**
	 * 修改域名分组
	 * @return 域名分组列表
	 */
	@PostMapping("/modDomainGroup")
	@Operation(summary = "修改域名分组")
	public R modMarketingGroupDomain(@Valid @RequestBody ModMarketingGroupDomainDTO reqDto) {
		return marketingConfigService.modMarketingGroupDomain(reqDto);
	}

	/**
	 * 获取域名分组列表
	 * @return 域名分组列表
	 */
	@GetMapping("/getDomainGroup")
	@Operation(summary = "获取域名分组列表")
	public R<List<GetMarketingDomainGroupVO>> getMarketingDomainGroupList() {
		List<GetMarketingDomainGroupVO> res = marketingConfigService.getMarketingDomainGroupList();
		return R.ok(res);
	}

	/**
	 * 修改分组IP
	 * @return IP分组列表
	 */
	@PostMapping("/modIpGroup")
	@Operation(summary = "修改IP分组")
	public R modMarketingGroupIp(@Valid @RequestBody ModMarketingGroupIPDTO reqDto) {
		return marketingConfigService.modMarketingGroupIp(reqDto);
	}

	/**
	 * 获取ip分组列表
	 * @return ip分组列表
	 */
	@GetMapping("/getIpGroup")
	@Operation(summary = "获取IP分组列表")
	public R<List<GetMarketingIpGroupVO>> getMarketingIpGroupList() {
		List<GetMarketingIpGroupVO> res = marketingConfigService.getMarketingIpGroupList();
		return R.ok(res);
	}

	/**
	 * 获取ip集合
	 * @return ip分组列表
	 */
	@GetMapping("/ipList")
	@Operation(summary = "获取IP集合")
	public R<List<GetMarketingConfigIpListVO>> getMarketingConfigIpList() {
		List<GetMarketingConfigIpListVO> res = marketingConfigService.getMarketingConfigIpList();
		return R.ok(res);
	}

	/**
	 * 添加IP集合
	 * @return R
	 */
	@PostMapping("/addIp")
	@Operation(summary = "IP集合添加IP")
	public R<Boolean> addMarketingConfigIpList(@Valid @RequestBody AddMarketingConfigIpListDTO reqDto) {
		return R.ok(marketingConfigService.addMarketingConfigIpList(reqDto));
	}

	/**
	 * 删除IP集合
	 * @return R
	 */
	@GetMapping("/delIp/{id}")
	@Operation(summary = "IP集合删除IP")
	public R<Boolean> delMarketingConfigIpList(@PathVariable Long id) {
		if (id == null) {
			throw new BusinessException("id不能为空");
		}
		return R.ok(marketingConfigService.delMarketingConfigIpList(id));
	}

	/**
	 * 获取域名集合
	 * @return 域名分组列表
	 */
	@GetMapping("/domainList")
	@Operation(summary = "获取域名集合")
	public R<List<GetMarketingConfigDomainListVO>> getMarketingConfigDomainList() {
		List<GetMarketingConfigDomainListVO> res = marketingConfigService.getMarketingConfigDomainList();
		return R.ok(res);
	}

	/**
	 * 添加域名集合
	 * @return R
	 */
	@PostMapping("/addDomain")
	@Operation(summary = "域名集合添加域名")
	public R<Boolean> addMarketingConfigDomainList(@Valid @RequestBody AddMarketingConfigDomainListDTO reqDto) {
		return R.ok(marketingConfigService.addMarketingConfigDomainList(reqDto));
	}

	/**
	 * 删除IP集合
	 * @return R
	 */
	@GetMapping("/delDomain/{id}")
	@Operation(summary = "域名集合删除域名")
	public R<Boolean> delMarketingConfigDomainList(@PathVariable Long id) {
		if (id == null){
			throw new BusinessException("id不能为空");
		}
		return R.ok(marketingConfigService.delMarketingConfigDomainList(id));
	}

	/**
	 * 查询全局配置信息
	 * @return R
	 */
	@GetMapping("/getConfig")
	@Operation(summary = "查询全局配置信息")
	public R<GetMarketingGlobalConfigVO> getMarketingGlobalConfig() {
		return R.ok(marketingConfigService.getMarketingGlobalConfig());
	}

	/**
	 * 查询全局配置信息
	 * @return R
	 */
	@PostMapping("/setConfig")
	@Operation(summary = "设置全局配置信息")
	public R<Boolean> setMarketingGlobalConfig(@Valid @RequestBody GetMarketingGlobalConfigVO reDto) {
		return R.ok(marketingConfigService.setMarketingGlobalConfig(reDto));
	}

}
