package org.dromara.main.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.vo.SysTenantVo;
import org.dromara.system.service.ISysTenantService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 门户企业账户信息接口
 *
 * 当前新版项目的企业主体挂在 sys_tenant 上，钱包余额直接从租户表读取。
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/portal/account")
public class PortalAccountController extends BaseController {

    private final ISysTenantService tenantService;

    /**
     * 获取当前企业钱包余额信息
     */
    @GetMapping("/balance")
    public R<Map<String, Object>> getAccountBalance() {
        String tenantId = LoginHelper.getTenantId();
        if (tenantId == null || tenantId.isBlank()) {
            return R.fail("未登录或token已失效");
        }

        SysTenantVo company = tenantService.queryByTenantId(tenantId);
        if (company == null) {
            return R.fail("企业信息不存在");
        }

        Map<String, Object> result = new HashMap<>(5);
        result.put("companyId", company.getId());
        result.put("companyName", company.getCompanyName());

        result.put("availableBalance", company.getAvailableBalance() == null ? BigDecimal.ZERO : company.getAvailableBalance());
        result.put("inUseBalance", company.getInUseBalance() == null ? BigDecimal.ZERO : company.getInUseBalance());
        result.put("withdrawingBalance", company.getWithdrawingBalance() == null ? BigDecimal.ZERO : company.getWithdrawingBalance());
        return R.ok(result);
    }
}
