import { useState } from 'react';
import { bulkPurchase } from '../../../../api';
import { MODES } from './TierModeToggle';

export default function BulkPurchaseButton({ drawNo, tierMode, onSuccess }) {
    const [busy, setBusy] = useState(false);
    const mode = MODES.find(m => m.key === tierMode) || MODES[0];

    const onClick = async () => {
        if (busy) return;
        setBusy(true);
        try {
            await bulkPurchase({
                draw_no: drawNo,
                tier_mode: tierMode,
                sets: mode.sets,
                amount: mode.amount,
            });
            onSuccess?.();
            alert(`${mode.sets}세트 구매 기록 완료!`);
        } catch (e) {
            alert(`구매 기록 실패: ${e?.message || e}`);
        } finally {
            setBusy(false);
        }
    };

    return (
        <button className="lc-btn lc-btn--prim" onClick={onClick} disabled={busy || !drawNo}>
            {busy ? '저장 중...' : `이대로 ${mode.sets}세트 구매했음`}
        </button>
    );
}
