'use client';
// RegenControls — R2 重生弹窗：单张/单套重生 + 可选人工提示词
import { useState } from 'react';

export interface RegenTarget {
  strategy: 'A' | 'B' | 'C';
  role?: string;        // 有=单张重生；无=整套重生
  label: string;        // 展示用（套名 或 角色名）
}

interface RegenControlsProps {
  target: RegenTarget | null;
  onClose: () => void;
  onSubmit: (target: RegenTarget, customPrompt: string) => Promise<void>;
}

export function RegenControls({ target, onClose, onSubmit }: RegenControlsProps) {
  const [prompt, setPrompt] = useState('');
  const [busy, setBusy] = useState(false);
  if (!target) return null;

  async function handleSubmit() {
    if (!target || busy) return;
    setBusy(true);
    try {
      await onSubmit(target, prompt.trim());
      setPrompt('');
      onClose();
    } finally {
      setBusy(false);
    }
  }

  return (
    <div
      className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
      onClick={onClose}
    >
      <div
        className="w-full max-w-md rounded-2xl bg-white p-5 shadow-xl"
        onClick={(e) => e.stopPropagation()}
      >
        <h3 className="mb-1 text-base font-semibold text-text-primary">
          重生{target.role ? '此张' : '整套'}：{target.label}
        </h3>
        <p className="mb-3 text-xs text-text-tertiary">
          可补充提示词指导这次重生（留空则按原策略重生）。瓶身忠于参考图，合规与真实约束不可被覆盖。
        </p>
        <textarea
          value={prompt}
          onChange={(e) => setPrompt(e.target.value)}
          maxLength={200}
          rows={3}
          placeholder="例：标题字再大一点，背景换成浅米色，突出成分卖点"
          className="mb-3 w-full resize-none rounded-lg border border-border-default p-2 text-sm focus:border-brand-orange focus:outline-none"
        />
        <div className="flex justify-end gap-2">
          <button
            onClick={onClose}
            className="rounded-lg px-4 py-1.5 text-sm text-text-secondary hover:bg-surface-secondary"
          >
            取消
          </button>
          <button
            onClick={handleSubmit}
            disabled={busy}
            className="rounded-lg bg-brand-orange px-4 py-1.5 text-sm font-medium text-white disabled:opacity-50"
          >
            {busy ? '提交中…' : '开始重生'}
          </button>
        </div>
      </div>
    </div>
  );
}
