"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { Loader2, Sparkles } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Card, CardContent } from "@/components/ui/card";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { TONES, LANGUAGES } from "@/lib/episodes/options";
import { createSeriesAction } from "@/app/(app)/series/actions";

export function SeriesCreateForm() {
  const router = useRouter();
  const [theme, setTheme] = useState("");
  const [count, setCount] = useState("6");
  const [tone, setTone] = useState<string>(TONES[0]);
  const [audience, setAudience] = useState("");
  const [language, setLanguage] = useState("en");
  const [submitting, setSubmitting] = useState(false);

  async function submit(e: React.FormEvent) {
    e.preventDefault();
    if (theme.trim().length < 5) {
      toast.error("Describe your season theme.");
      return;
    }
    setSubmitting(true);
    const res = await createSeriesAction({
      theme: theme.trim(),
      count: Number(count),
      tone,
      audience: audience.trim() || undefined,
      language,
    });
    if (!res.ok || !res.seriesId) {
      toast.error(res.error ?? "Could not plan season");
      setSubmitting(false);
      return;
    }
    toast.success("Season planned!");
    router.push(`/series/${res.seriesId}`);
  }

  return (
    <Card className="max-w-2xl">
      <CardContent className="pt-6">
        <form onSubmit={submit} className="space-y-4">
          <div className="space-y-2">
            <Label htmlFor="theme">Season theme</Label>
            <Textarea
              id="theme"
              rows={3}
              placeholder="e.g. A beginner's journey through personal finance"
              value={theme}
              onChange={(e) => setTheme(e.target.value)}
            />
          </div>
          <div className="grid gap-4 sm:grid-cols-2">
            <div className="space-y-2">
              <Label>Episodes</Label>
              <Select value={count} onValueChange={setCount}>
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {[2, 3, 4, 5, 6, 8, 10, 12].map((n) => (
                    <SelectItem key={n} value={String(n)}>
                      {n} episodes
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
            <div className="space-y-2">
              <Label>Tone</Label>
              <Select value={tone} onValueChange={setTone}>
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {TONES.map((t) => (
                    <SelectItem key={t} value={t}>
                      {t}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
            <div className="space-y-2">
              <Label>Language</Label>
              <Select value={language} onValueChange={setLanguage}>
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {LANGUAGES.map((l) => (
                    <SelectItem key={l.code} value={l.code}>
                      {l.label}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
            <div className="space-y-2">
              <Label htmlFor="aud">Audience (optional)</Label>
              <Input id="aud" value={audience} onChange={(e) => setAudience(e.target.value)} />
            </div>
          </div>
          <Button type="submit" disabled={submitting}>
            {submitting ? <Loader2 className="h-4 w-4 animate-spin" /> : <Sparkles className="h-4 w-4" />}
            Plan season
          </Button>
        </form>
      </CardContent>
    </Card>
  );
}
