import {type Nullable} from "@/shared/utils/display";
import {get} from "@/shared/services/http.ts";

const BillingUrlObj = {
    getBalance: '/api/v1/billing/balance',
    getLedger: '/api/v1/billing/ledger',
    ExportLedger: "/api/v1/billing/ledger/export"
} as const;

export interface BalanceResponseBody {
    user_id: string,
    balance_cent: Nullable<number>,
    gift_balance_cent: Nullable<number>,
    currency: "CNY",
    today_spent_cent: number,
    today_calls: number
}


export interface LedgerReqeustParam {
    month: Nullable<Date>,
    page: number,
    page_size: number
}

export interface LedgerItemResponseBody {
    id: string,
    entry_type: string,
    balance_cent: string,
    amount_cent: number,
    currency: string,
    balance_after_cent: number,
    task_id: string,
    description: "string",
    accounting_month: "string",
    accounting_day: "string",
    created_at: Date
}

export interface ExportLedgerRequstParam {
    start_time: Date,
    end_time: Date,
    entry_type: Nullable<string>
    debug?: boolean
}

export class BillingAPI {
    static getBalance(signal?: AbortSignal): Promise<BalanceResponseBody> {
        return get<BalanceResponseBody>(BillingUrlObj.getBalance, undefined, {signal});
    }

    static getLedger(param: LedgerReqeustParam, signal?: AbortSignal): Promise<LedgerItemResponseBody[]> {
        return get<LedgerItemResponseBody[]>(BillingUrlObj.getLedger, param as unknown as Record<string, unknown>, {signal});
    }

    static exportUrl = BillingUrlObj.ExportLedger;
}

// ============================================================
// 账单条目类型常量
// ============================================================

export const EntryTypeEnum = {
    TaskHold: "task_hold",
    Recharge: "recharge",
    Gift: "gift",
    HoldRelease: "hold_release",
    HoldSettle: "hold_settle",
} as const;

export type EntryTypeValue = typeof EntryTypeEnum[keyof typeof EntryTypeEnum];

export const EntryTypeLabelMap = {
    [EntryTypeEnum.TaskHold]: "任务消耗",
    [EntryTypeEnum.Recharge]: "充值",
    [EntryTypeEnum.Gift]: "赠送",
    [EntryTypeEnum.HoldRelease]: "退款",
    [EntryTypeEnum.HoldSettle]: "结算",
} as const;


export const EntryTypeColorMap = {
    [EntryTypeEnum.TaskHold]: "orange",
    [EntryTypeEnum.Recharge]: "green",
    [EntryTypeEnum.Gift]: "green",
    [EntryTypeEnum.HoldRelease]: "blue",
    [EntryTypeEnum.HoldSettle]: "orange",
} as const;

export type EntryUITypeValue = typeof EntryTypeLabelMap[keyof typeof EntryTypeLabelMap];
export type ColorValue = typeof EntryTypeColorMap[keyof typeof EntryTypeColorMap];
