'use client';

import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Card } from '@/components/ui/card';
import { API_BASE } from '@/lib/config';

export default function NewPromptPage() {
  const router = useRouter();
  const [form, setForm] = useState({ title: '', content: '', description: '', tags: '', model: '' });
  const [submitting, setSubmitting] = useState(false);

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault();
    if (!form.title.trim() || !form.content.trim()) return;
    setSubmitting(true);
    try {
      const token = localStorage.getItem('adminToken');
      const res = await fetch(`${API_BASE}/prompts`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
        body: JSON.stringify(form),
      });
      if (res.ok) router.push('/admin/prompts');
    } catch {}
    setSubmitting(false);
  }

  return (
    <>
      <div className="border-b border-border bg-card px-4 py-4">
        <h1 className="text-2xl font-bold text-foreground">新建提示词</h1>
      </div>
      <div className="max-w-2xl mx-auto p-6">
        <Card className="p-6">
          <form onSubmit={handleSubmit} className="space-y-4">
            <div>
              <label className="block text-sm font-medium text-foreground mb-1">标题</label>
              <Input value={form.title} onChange={e => setForm(f => ({ ...f, title: e.target.value }))} placeholder="提示词标题" required />
            </div>
            <div>
              <label className="block text-sm font-medium text-foreground mb-1">描述</label>
              <Input value={form.description} onChange={e => setForm(f => ({ ...f, description: e.target.value }))} placeholder="简短描述" />
            </div>
            <div>
              <label className="block text-sm font-medium text-foreground mb-1">提示词内容</label>
              <textarea value={form.content} onChange={e => setForm(f => ({ ...f, content: e.target.value }))}
                rows={6} className="w-full px-3 py-2 bg-background border border-input rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-ring font-mono"
                placeholder="输入提示词内容..." required />
            </div>
            <div>
              <label className="block text-sm font-medium text-foreground mb-1">标签（逗号分隔）</label>
              <Input value={form.tags} onChange={e => setForm(f => ({ ...f, tags: e.target.value }))} placeholder="AI,提示词,编程" />
            </div>
            <div>
              <label className="block text-sm font-medium text-foreground mb-1">适用模型</label>
              <Input value={form.model} onChange={e => setForm(f => ({ ...f, model: e.target.value }))} placeholder="GPT-4, Claude 等" />
            </div>
            <div className="flex gap-2 pt-2">
              <Button type="submit" disabled={submitting || !form.title.trim() || !form.content.trim()}>
                {submitting ? '创建中...' : '创建提示词'}
              </Button>
              <Link href="/admin/prompts"><Button type="button" variant="outline">取消</Button></Link>
            </div>
          </form>
        </Card>
      </div>
    </>
  );
}
