import { useCallback, useEffect, useState } from 'react';
import {
    getPurchases, getPurchaseStats, addPurchase, updatePurchase, deletePurchase,
    bulkPurchase as apiBulkPurchase,
} from '../../../api';
import { emptyPurchaseForm } from '../lottoUtils';

export default function usePurchases() {
    const [purchases, setPurchases] = useState([]);
    const [purchaseStats, setPurchaseStats] = useState(null);
    const [purchaseLoading, setPurchaseLoading] = useState(false);

    // 폼 상태
    const [purchaseFormOpen, setPurchaseFormOpen] = useState(false);
    const [purchaseForm, setPurchaseForm] = useState(emptyPurchaseForm);
    const [purchaseFormSaving, setPurchaseFormSaving] = useState(false);
    const [purchaseFormError, setPurchaseFormError] = useState('');
    const [purchaseEditId, setPurchaseEditId] = useState(null);

    const refreshPurchases = useCallback(async () => {
        setPurchaseLoading(true);
        try {
            const [recs, st] = await Promise.all([getPurchases(), getPurchaseStats()]);
            setPurchases(recs?.records ?? []);
            setPurchaseStats(st);
        } catch {}
        finally { setPurchaseLoading(false); }
    }, []);

    const handlePurchaseFormOpen = useCallback(() => {
        setPurchaseEditId(null);
        setPurchaseForm(emptyPurchaseForm());
        setPurchaseFormError('');
        setPurchaseFormOpen(true);
    }, []);

    const handlePurchaseFormClose = useCallback(() => {
        setPurchaseFormOpen(false);
        setPurchaseEditId(null);
        setPurchaseFormError('');
    }, []);

    const handlePurchaseFormChange = useCallback((field, value) => {
        setPurchaseForm((prev) => ({ ...prev, [field]: value }));
    }, []);

    const handlePurchaseEditStart = useCallback((rec) => {
        setPurchaseEditId(rec.id);
        setPurchaseForm({
            draw_no: String(rec.draw_no ?? ''),
            amount: rec.amount ?? 5000,
            sets: rec.sets ?? 5,
            prize: rec.prize ?? 0,
            note: rec.note ?? '',
        });
        setPurchaseFormError('');
        setPurchaseFormOpen(true);
    }, []);

    const handlePurchaseFormSubmit = useCallback(async (e) => {
        e.preventDefault();
        setPurchaseFormSaving(true); setPurchaseFormError('');
        const payload = {
            draw_no: Number(purchaseForm.draw_no),
            amount: Number(purchaseForm.amount),
            sets: Number(purchaseForm.sets),
            prize: Number(purchaseForm.prize),
            note: purchaseForm.note.trim(),
        };
        try {
            if (purchaseEditId != null) {
                const updated = await updatePurchase(purchaseEditId, payload);
                setPurchases((prev) =>
                    prev.map((r) => r.id === purchaseEditId ? (updated ?? { ...payload, id: purchaseEditId }) : r)
                );
            } else {
                const saved = await addPurchase(payload);
                setPurchases((prev) => [saved ?? { ...payload, id: Date.now() }, ...prev]);
            }
            try { setPurchaseStats(await getPurchaseStats()); } catch {}
            handlePurchaseFormClose();
        } catch (err) {
            setPurchaseFormError(err?.message ?? String(err));
        } finally {
            setPurchaseFormSaving(false);
        }
    }, [purchaseForm, purchaseEditId, handlePurchaseFormClose]);

    const handlePurchaseDelete = useCallback(async (id) => {
        if (!confirm('이 구매 기록을 삭제할까요?')) return;
        setPurchases((prev) => prev.filter((r) => r.id !== id));
        try {
            await deletePurchase(id);
            try { setPurchaseStats(await getPurchaseStats()); } catch {}
        } catch { refreshPurchases(); }
    }, [refreshPurchases]);

    const handleBulkPurchase = useCallback(async (params) => {
        const result = await apiBulkPurchase(params);
        await refreshPurchases();
        return result;
    }, [refreshPurchases]);

    useEffect(() => { refreshPurchases(); }, []); // eslint-disable-line react-hooks/exhaustive-deps

    return {
        purchases, purchaseStats, purchaseLoading,
        purchaseFormOpen, purchaseForm, purchaseFormSaving, purchaseFormError, purchaseEditId,
        handlePurchaseFormOpen, handlePurchaseFormClose, handlePurchaseFormChange,
        handlePurchaseFormSubmit, handlePurchaseEditStart, handlePurchaseDelete,
        handleBulkPurchase,
    };
}
