"use client";
import { useEffect, useState } from "react";

const FIELDS: { key: string; label: string; help: string }[] = [
  { key: "video_play", label: "Watching a video (real play, once per video)", help: "Awarded when a user actually presses play on a video for the first time." },
  { key: "daily_login", label: "Logging in each day", help: "Awarded once per calendar day, on the first login of the day." },
  { key: "login_streak", label: "Login streak bonus (3+ consecutive days)", help: "Extra bonus on top of the daily login points, once a user reaches a 3+ day streak — awarded every day the streak continues." },
  { key: "post_create", label: "Creating a community post", help: "Awarded once per post, regardless of whether it includes an attachment." },
  { key: "reaction_given", label: "Reacting to a post (I prayed / ❤️ / ✨ / 👍)", help: "Awarded to the person who reacts. Reversed automatically if they un-react." },
  { key: "reaction_received", label: "Having your post reacted to", help: "Awarded to the post's author when someone else reacts. Reversed if the reaction is removed." },
  { key: "tip_support", label: "Supporting the creators (per $10 given)", help: "Roughly 1 point per $1 given, scaled by this value ÷ 10. E.g. 10 = 1pt/$1, 20 = 2pt/$1." },
];

export default function AdminPointsPage() {
  const [values, setValues] = useState<Record<string, number>>({});
  const [saving, setSaving] = useState(false);
  const [saved, setSaved] = useState(false);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch("/api/admin/points")
      .then((r) => r.json())
      .then((d) => {
        setValues(d);
        setLoading(false);
      });
  }, []);

  async function save() {
    setSaving(true);
    setSaved(false);
    await fetch("/api/admin/points", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(values),
    });
    setSaving(false);
    setSaved(true);
  }

  if (loading) return <p className="text-neutral-500">Loading...</p>;

  return (
    <div className="max-w-2xl">
      <h2 className="text-lg font-semibold mb-1">Points System</h2>
      <p className="text-xs text-neutral-500 mb-6">
        Set how many points members earn for each action. Changes apply immediately to future actions —
        past point history is not recalculated.
      </p>

      <div className="card p-5 space-y-5">
        {FIELDS.map((f) => (
          <div key={f.key} className="flex items-start justify-between gap-4 border-b border-neutral-800 pb-4 last:border-0 last:pb-0">
            <div className="flex-1">
              <label className="block text-sm font-medium">{f.label}</label>
              <p className="text-xs text-neutral-500 mt-0.5">{f.help}</p>
            </div>
            <input
              type="number"
              min="0"
              value={values[f.key] ?? 0}
              onChange={(e) => setValues((v) => ({ ...v, [f.key]: parseInt(e.target.value) || 0 }))}
              className="w-20 bg-neutral-900 border border-neutral-800 rounded-md px-3 py-2 text-sm outline-none focus:border-red-600 text-center shrink-0"
            />
          </div>
        ))}
      </div>

      <div className="flex items-center gap-3 mt-4">
        <button onClick={save} disabled={saving} className="btn-primary px-5 py-2 text-sm">
          {saving ? "Saving..." : "Save Point Values"}
        </button>
        {saved && <span className="text-green-500 text-sm">✓ Saved</span>}
      </div>
    </div>
  );
}
