package com.yaoai.billing.service;

import com.yaoai.billing.dto.BillingChargeRequest;
import com.yaoai.domain.entity.BillingRecord;
import com.yaoai.domain.entity.TenantBalance;

import java.math.BigDecimal;
import java.util.List;

public interface BillingService {

    /**
     * 模型粒度计费（M14 起的主入口）：记录模型/用户/单元数/成功标记/JSON 元信息。
     * SOFT 只记录；STRICT 先扣余额再记录；OFF 跳过。
     */
    void charge(BillingChargeRequest request);

    /**
     * Estimate credits for a billing request without writing records or changing balance.
     */
    BigDecimal estimateCost(BillingChargeRequest request);

    /**
     * Check current balance before running an external model call. No deduction is performed.
     */
    void checkBalance(BillingChargeRequest request);

    /**
     * Check current balance against an already estimated total. No deduction is performed.
     */
    void checkBalance(Long tenantId, BigDecimal credits);

    /**
     * 旧调用兼容：自动映射到 charge(BillingChargeRequest)。仅记录 modality/operation，缺失模型信息。
     *
     * @deprecated 使用 {@link #charge(BillingChargeRequest)}，能落到模型粒度便于运营查询
     */
    @Deprecated
    default void charge(Long tenantId, Long projectId, String operation, String refId) {
        charge(BillingChargeRequest.builder()
                .tenantId(tenantId)
                .projectId(projectId)
                .operation(operation)
                .refId(refId)
                .build());
    }

    /** 充值（管理后台 / Webhook 调用） */
    void recharge(Long tenantId, BigDecimal amount, String refId);

    /** 运营手动充值，记录运营 ID */
    void rechargeByAdmin(Long tenantId, BigDecimal amount, Long adminId, String refId);

    /** 初始化余额行（租户注册时调用，已存在则幂等） */
    void initBalance(Long tenantId, BigDecimal initialCredits);

    TenantBalance getBalance(Long tenantId);

    List<BillingRecord> getRecords(Long tenantId, int limit, long offset);

    long countRecords(Long tenantId);
}
