package com.fs.company.controller.newAdv;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.result.Result;
import com.fs.newAdv.domain.AdvEventType;
import com.fs.newAdv.domain.CallbackAccount;
import com.fs.newAdv.service.ICallbackAccountService;
import com.fs.newAdv.vo.ConversionEventVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 回传账号管理控制器
 *
 * @author zhangqin
 * @date 2025-11-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/adv/callback-account")
public class CallbackAccountController {

    @Autowired
    private ICallbackAccountService callbackAccountService;

    /**
     * 分页查询回传账号列表
     */
    @GetMapping("/page")
    public Result<IPage<CallbackAccount>> page(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String advertiserId,
            @RequestParam(required = false) String accountName) {

        Page<CallbackAccount> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<CallbackAccount> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(accountName), CallbackAccount::getAccountName, accountName);
        wrapper.eq(StrUtil.isNotBlank(advertiserId), CallbackAccount::getAdvertiserId, advertiserId);
        wrapper.orderByDesc(CallbackAccount::getCreateTime);
        IPage<CallbackAccount> result = callbackAccountService.page(page, wrapper);

        return Result.success(result);
    }

    /**
     * 根据ID查询回传账号详情
     */
    @GetMapping("/{id}")
    public Result<CallbackAccount> getById(@PathVariable @NotNull(message = "账号ID不能为空") Long id) {
        CallbackAccount callbackAccount = callbackAccountService.getById(id);
        return Result.success(callbackAccount);
    }

    /**
     * 创建回传账号
     */
    @PostMapping
    public Result<Void> create(@RequestBody @Validated CallbackAccount callbackAccount) {
        boolean success = callbackAccountService.save(callbackAccount);
        return success ? Result.success() : Result.error("创建失败");
    }

    /**
     * 更新回传账号
     */
    @PutMapping("/{id}")
    public Result<Void> update(
            @PathVariable @NotNull(message = "账号ID不能为空") Long id,
            @RequestBody @Validated CallbackAccount callbackAccount) {

        callbackAccount.setId(id);
        boolean success = callbackAccountService.updateById(callbackAccount);
        return success ? Result.success() : Result.error("更新失败");
    }

    /**
     * 删除回传账号
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable @NotNull(message = "账号ID不能为空") Long id) {
        boolean success = callbackAccountService.removeById(id);
        return success ? Result.success() : Result.error("删除失败");
    }

    /**
     * 新增回传事件
     */
    @PostMapping("/saveEventType/{id}")
    public Result<Void> saveEventType(@RequestBody List<ConversionEventVo> vo, @PathVariable("id") Long id) {
        boolean success = callbackAccountService.saveEventType(vo, id);
        return success ? Result.success() : Result.error("新增回传事件失败");
    }

    /**
     * 查询回传事件
     */
    @PostMapping("/queryEventType/{advertiserId}")
    public Result<List<AdvEventType>> queryEventType(@PathVariable("advertiserId") Long advertiserId) {
        return Result.success(callbackAccountService.queryEventType(advertiserId));
    }
}

