import { useState } from 'react';
import { Loader2, Send, X } from 'lucide-react';
import { usePluginContext } from './client';
import type { S3Credential } from './types';

const API_BASE = '/api/v1/plugin/data_analytics';

interface Props {
  credentials: S3Credential[];
  onClose: () => void;
  onCreated: () => void;
}

export function NewJobDialog({ credentials, onClose, onCreated }: Props) {
  const { client } = usePluginContext();
  const [credId, setCredId] = useState(credentials[0]?.cred_id || '');
  const [description, setDescription] = useState('');
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState('');

  const submit = async () => {
    if (!credId) {
      setError('请选择 S3 凭证');
      return;
    }
    if (!description.trim()) {
      setError('请描述要做的分析');
      return;
    }
    setBusy(true);
    setError('');
    try {
      await client.post(`${API_BASE}/jobs`, {
        cred_id: credId,
        description: description.trim(),
      });
      onCreated();
      onClose();
    } catch (e: unknown) {
      const msg = (e as { response?: { data?: { detail?: string } } }).response?.data?.detail;
      setError(msg || '提交失败');
    } finally {
      setBusy(false);
    }
  };

  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4">
      <div className="w-full max-w-lg bg-bg-card rounded-2xl border border-border-primary shadow-xl">
        <div className="flex items-center justify-between p-4 border-b border-border-primary">
          <h3 className="font-semibold text-text-primary">新建分析任务</h3>
          <button className="p-1 text-text-muted hover:text-text-primary" onClick={onClose}>
            <X size={16} />
          </button>
        </div>
        <div className="p-5 space-y-4">
          <div>
            <label className="text-xs text-text-secondary block mb-1.5">S3 凭证</label>
            {credentials.length === 0 ? (
              <div className="text-xs text-warning bg-warning-bg/50 border border-warning/20 rounded-lg p-2">
                请先在上方添加 S3 凭证。
              </div>
            ) : (
              <select
                className="w-full px-3 py-2 text-sm rounded-lg bg-bg-base border border-border-primary focus:outline-none focus:border-accent"
                value={credId}
                onChange={(e) => setCredId(e.target.value)}
              >
                {credentials.map((c) => (
                  <option key={c.cred_id} value={c.cred_id}>
                    {c.display_name} · {c.region}
                  </option>
                ))}
              </select>
            )}
          </div>
          <div>
            <label className="text-xs text-text-secondary block mb-1.5">任务描述</label>
            <textarea
              className="w-full px-3 py-2 text-sm rounded-lg bg-bg-base border border-border-primary focus:outline-none focus:border-accent min-h-[120px] resize-y"
              placeholder="例如：分析 s3://my-bucket/sales/2026-q1/ 的销售趋势，输出按月汇总"
              value={description}
              onChange={(e) => setDescription(e.target.value)}
            />
            <p className="text-[11px] text-text-muted mt-1">
              Agent 会先用 s3_peek/s3_list_objects 探查数据，然后选择 python_executor 或 ray_submit 执行分析。
            </p>
          </div>
          {error && <div className="text-xs text-danger">{error}</div>}
        </div>
        <div className="flex items-center justify-end gap-2 p-4 border-t border-border-primary">
          <button
            className="px-3 py-1.5 text-xs rounded-lg border border-border-primary text-text-secondary hover:text-text-primary"
            onClick={onClose}
          >
            取消
          </button>
          <button
            className="px-3 py-1.5 text-xs rounded-lg bg-accent text-white hover:opacity-90 disabled:opacity-50 transition flex items-center gap-1.5"
            onClick={submit}
            disabled={busy || credentials.length === 0}
          >
            {busy ? <Loader2 size={14} className="animate-spin" /> : <Send size={14} />}
            提交
          </button>
        </div>
      </div>
    </div>
  );
}
