import React, { useState } from 'react';
import { triggerEvolverGenerate, triggerEvolverEvaluate } from '../../../api';

export default function EvolverActions({ onChange }) {
    const [busy, setBusy] = useState(null);
    const [out, setOut] = useState(null);

    async function run(kind) {
        setBusy(kind);
        setOut(null);
        try {
            const fn = kind === 'generate' ? triggerEvolverGenerate : triggerEvolverEvaluate;
            const res = await fn();
            setOut(res);
            onChange && onChange();
        } catch (e) {
            setOut({ error: String(e) });
        } finally {
            setBusy(null);
        }
    }

    return (
        <div className="evolver-card actions-card">
            <h2>수동 트리거 (dev)</h2>
            <div className="action-buttons">
                <button disabled={!!busy} onClick={() => run('generate')}>
                    {busy === 'generate' ? '생성 중...' : 'generate-now (월요일 후보 생성)'}
                </button>
                <button disabled={!!busy} onClick={() => run('evaluate')}>
                    {busy === 'evaluate' ? '평가 중...' : 'evaluate-now (회고 + base 갱신)'}
                </button>
            </div>
            {out && <pre className="action-output">{JSON.stringify(out, null, 2)}</pre>}
        </div>
    );
}
