package com.seer.teach.pay.admin.withdrawal.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayMerchantBankAccountPageReq;
import com.seer.teach.pay.admin.withdrawal.controller.request.PayMerchantBankAccountReq;
import com.seer.teach.pay.admin.withdrawal.controller.response.PayMerchantBankAccountResp;
import com.seer.teach.pay.admin.withdrawal.service.IAdminPayMerchantBankAccountService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pay/merchant/bank-account")
@Tag(name = "商户端 - 商家收款账户管理")
@Validated
@RequiredArgsConstructor
@SaCheckLogin(type = StpUtil.TYPE)
public class AdminPayMerchantBankAccountController {

    private final IAdminPayMerchantBankAccountService adminPayMerchantBankAccountService;

    @PostMapping
    @LogPrint
    @Operation(summary = "新增商家收款账户")
    @SaCheckPermission("pay:merchant:bank-account:create")
    public ResultBean<Boolean> create(@RequestBody @Valid PayMerchantBankAccountReq req) {
        log.info("新增商家收款账户，参数: {}", req);
        Boolean result = adminPayMerchantBankAccountService.create(req);
        log.info("新增商家收款账户结果: {}", result);
        return ResultBean.success(result);
    }

    @PutMapping
    @LogPrint
    @Operation(summary = "修改商家收款账户")
    @SaCheckPermission("pay:merchant:bank-account:update")
    public ResultBean<Boolean> update(@RequestBody @Valid PayMerchantBankAccountReq req) {
        log.info("修改商家收款账户，参数: {}", req);
        Boolean result = adminPayMerchantBankAccountService.update(req);
        log.info("修改商家收款账户结果: {}", result);
        return ResultBean.success(result);
    }

    @DeleteMapping("/{id}")
    @LogPrint
    @Operation(summary = "删除商家收款账户")
    @SaCheckPermission("pay:merchant:bank-account:delete")
    public ResultBean<Boolean> delete(@Parameter(description = "收款账户ID") @PathVariable Integer id) {
        log.info("删除商家收款账户，ID: {}", id);
        Boolean result = adminPayMerchantBankAccountService.delete(id);
        log.info("删除商家收款账户结果: {}", result);
        return ResultBean.success(result);
    }

    @GetMapping("/{id}")
    @LogPrint
    @Operation(summary = "根据ID查询商家收款账户")
    @SaCheckPermission("pay:merchant:bank-account:detail")
    public ResultBean<PayMerchantBankAccountResp> getById(@Parameter(description = "收款账户ID") @PathVariable Integer id) {
        log.info("根据ID查询商家收款账户，ID: {}", id);
        PayMerchantBankAccountResp resp = adminPayMerchantBankAccountService.getById(id);
        log.info("查询商家收款账户结果: {}", resp);
        return ResultBean.success(resp);
    }

    @PostMapping("/page")
    @LogPrint
    @Operation(summary = "分页查询商家收款账户")
    @SaCheckPermission("pay:merchant:bank-account:page")
    public ResultBean<PageListBean<PayMerchantBankAccountResp>> pageList(@RequestBody @Valid PayMerchantBankAccountPageReq req) {
        log.info("分页查询商家收款账户，参数: {}", req);
        PageListBean<PayMerchantBankAccountResp> respPage = adminPayMerchantBankAccountService.pageList(req);
        log.info("分页查询商家收款账户结果，共{}条", respPage.getTotal());
        return ResultBean.success(respPage);
    }

    @GetMapping("/list")
    @LogPrint
    @Operation(summary = "根据用户ID查询商家收款账户列表")
    @SaCheckPermission("pay:merchant:bank-account:list")
    public ResultBean<List<PayMerchantBankAccountResp>> getList() {
        Integer userId = StpUtil.getLoginIdAsInt();
        log.info("根据用户ID查询商家收款账户列表，用户ID: {}", userId);
        List<PayMerchantBankAccountResp> respList = adminPayMerchantBankAccountService.getListByUserId(userId);
        log.info("根据用户ID查询商家收款账户列表结果，共{}条", respList.size());
        return ResultBean.success(respList);
    }
}