package com.pig4cloud.pig.marketing.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.exception.BusinessException;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.marketing.api.dto.app.*;
import com.pig4cloud.pig.marketing.api.vo.app.PageMarketingAppsVO;
import com.pig4cloud.pig.marketing.api.vo.app.PageStatAppFirstMktDataVO;
import com.pig4cloud.pig.marketing.api.vo.app.PageStatAppSecondMktDataVO;
import com.pig4cloud.pig.marketing.service.MarketingAppsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
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-21
 * @description: 营销应用列表
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app")
@Tag(description = "marketingApp", name = "营销应用管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class MarketingAppsController {

	private final MarketingAppsService marketingAppsService;


	/**
	 * 分页查询应用列表
	 * @param reqDto 分页参数对象
	 * @return 分页查询结果
	 */
	@GetMapping("/page")
	@Operation(summary = "分页查询应用列表")
	public R pageMarketingApps(@ParameterObject PageMarketingAppsDTO reqDto) {
		Page page = marketingAppsService.pageMarketingApps(reqDto, true);
		return R.ok(page);
	}

	@GetMapping("/detail")
	@Operation(summary = "查询应用详情")
	public R<PageMarketingAppsVO> getMarketingAppById(@ParameterObject Long id) {
		if (id == null){
			throw new BusinessException("应用ID不能为空");
		}
		PageMarketingAppsVO res = marketingAppsService.getMarketingAppById(id);
		return R.ok(res);
	}

	/**
	 * 分页查询拉黑应用列表
	 * @param reqDto 分页参数对象
	 * @return 分页查询结果
	 */
	@GetMapping("/pageDel")
	@Operation(summary = "分页查询拉黑应用列表")
	public R pageDelMarketingApps(@ParameterObject PageMarketingAppsDTO reqDto) {
		Page page = marketingAppsService.pageMarketingApps(reqDto, false);
		return R.ok(page);
	}

	/**
	 * 批量更新应用信息
	 * @param reqDtoList 批量更新参数
	 * @return 保存结果
	 */
	@PostMapping("/update")
	@Operation(summary = "批量更新应用信息")
	public R batchUpdateMarketingApps(@Valid @RequestBody List<BatchUpdateMarketingAppsDTO> reqDtoList) {
		return marketingAppsService.batchUpdateMarketingApps(reqDtoList);
	}

	/**
	 * 设置应用信息
	 * @param reqDto 应用信息
	 * @return 保存结果
	 */
	@PostMapping("/setInfo")
	@Operation(summary = "设置应用信息")
	public R setMarketingAppsInfo(@Valid @RequestBody SetMarketingAppsDTO reqDto) {
		// 先查询域名是否存在
		Boolean result = marketingAppsService.setMarketingAppsInfo(reqDto);
		return result?R.ok():R.failed();
	}

	/**
	 * 设置应用状态
	 * @param reqDto 应用信息
	 * @return R
	 */
	@PostMapping("/setStatus")
	@Operation(summary = "设置应用状态")
	public R setAppStatusById(@Valid @RequestBody SetMarketingAppsStatusDTO reqDto)  {
		Boolean res = marketingAppsService.setAppStatusById(reqDto);
		return res?R.ok():R.failed();
	}

	@PostMapping("/synced")
	@Operation(summary = "同步应用列表")
	public R syncedAppList() {
		return R.ok(marketingAppsService.syncedAppList());
	}

	/**
	 * 修改应用IP列表
	 * @param reqDto IP参数
	 * @return 保存结果
	 */
	@PostMapping("/modIp")
	@Operation(summary = "修改应用IP集合")
	public R modMarketingAppsIp(@Valid @RequestBody ModMarketingAppsIpDTO reqDto) {
		return marketingAppsService.modMarketingAppsIps(reqDto);
	}

	/**
	 * 修改应用域名集合
	 * @param reqDto 域名参数
	 * @return 保存结果
	 */
	@PostMapping("/modDomain")
	@Operation(summary = "修改应用域名集合")
	public R modMarketingAppsIp(@Valid @RequestBody ModMarketingAppsDomainDTO reqDto) {
		return marketingAppsService.modMarketingAppsDomains(reqDto);
	}

	/**
	 * 分页统计应用一级营销数据
	 * @param reqDto 查询参数
	 * @return 一级营销数据
	 */
	@GetMapping("/stat/page")
	@Operation(summary = "分页统计应用一级营销数据")
	public R<Page<PageStatAppFirstMktDataVO>> pageStatAppFirstMktData(@Valid @ParameterObject PageStatAppMktDataDTO reqDto) {
		return R.ok(marketingAppsService.pageStatAppFirstMktData(reqDto));
	}

	/**
	 * 分页统计应用二级营销数据
	 * @param reqDto 统计参数
	 * @return 二级营销数据
	 */
	@GetMapping("/stat/second/page")
	@Operation(summary = "分页统计应用二级营销数据")
	public R<Page<PageStatAppSecondMktDataVO>> pageStatAppSecondMktData(@Valid @ParameterObject PageStatAppMktDataDTO reqDto,
																		@Valid
									  @ParameterObject
									  @NotBlank(message = "ip不能为空")
									  @Schema(description = "ip") String ip) {
		return R.ok(marketingAppsService.pageStatAppSecondMktData(reqDto, ip));
	}
}
