package com.starry.admin.modules.withdraw.controller;

import com.starry.admin.common.aspect.ClerkUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.withdraw.entity.ClerkPayeeProfileEntity;
import com.starry.admin.modules.withdraw.service.IClerkPayeeProfileService;
import com.starry.common.result.TypedR;
import javax.annotation.Resource;
import lombok.Data;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx/withdraw/payee")
public class WxWithdrawPayeeController {

    private static final String DEFAULT_CHANNEL = "ALIPAY_QR";

    @Resource
    private IClerkPayeeProfileService clerkPayeeProfileService;

    @ClerkUserLogin
    @GetMapping
    public TypedR<PayeeProfileResponse> getProfile() {
        PlayClerkUserInfoEntity clerk = ThreadLocalRequestDetail.getClerkUserInfo();
        ClerkPayeeProfileEntity profile = clerkPayeeProfileService.getByClerk(clerk.getTenantId(), clerk.getId());
        return TypedR.ok(toResponse(profile));
    }

    @ClerkUserLogin
    @PostMapping
    public TypedR<PayeeProfileResponse> upsert(@RequestBody PayeeProfileRequest body) {
        if (body == null || !StringUtils.hasText(body.getQrCodeUrl())) {
            throw new CustomException("请上传支付宝收款二维码");
        }
        PlayClerkUserInfoEntity clerk = ThreadLocalRequestDetail.getClerkUserInfo();
        String channel = StringUtils.hasText(body.getChannel()) ? body.getChannel() : DEFAULT_CHANNEL;
        String displayName = StringUtils.hasText(body.getDisplayName()) ? body.getDisplayName() : clerk.getNickname();
        ClerkPayeeProfileEntity profile = clerkPayeeProfileService.upsertProfile(
                clerk.getTenantId(), clerk.getId(), channel, body.getQrCodeUrl(), displayName, clerk.getId());
        return TypedR.ok(toResponse(profile));
    }

    @ClerkUserLogin
    @PostMapping("/confirm")
    public TypedR<PayeeProfileResponse> confirm() {
        PlayClerkUserInfoEntity clerk = ThreadLocalRequestDetail.getClerkUserInfo();
        ClerkPayeeProfileEntity profile = clerkPayeeProfileService.confirmProfile(
                clerk.getTenantId(), clerk.getId(), clerk.getId());
        if (profile == null || !StringUtils.hasText(profile.getQrCodeUrl())) {
            throw new CustomException("请先上传支付宝收款二维码");
        }
        return TypedR.ok(toResponse(profile));
    }

    private PayeeProfileResponse toResponse(ClerkPayeeProfileEntity profile) {
        if (profile == null) {
            return null;
        }
        PayeeProfileResponse resp = new PayeeProfileResponse();
        resp.setChannel(profile.getChannel());
        resp.setQrCodeUrl(profile.getQrCodeUrl());
        resp.setDisplayName(profile.getDisplayName());
        resp.setLastConfirmedAt(profile.getLastConfirmedAt());
        return resp;
    }

    @Data
    public static class PayeeProfileRequest {
        private String channel;
        private String qrCodeUrl;
        private String displayName;
    }

    @Data
    public static class PayeeProfileResponse {
        private String channel;
        private String qrCodeUrl;
        private String displayName;
        private java.time.LocalDateTime lastConfirmedAt;
    }
}
