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

export default function AdminDeletedPostsPage() {
  const [posts, setPosts] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch("/api/admin/deleted-posts")
      .then((r) => r.json())
      .then((d) => {
        setPosts(d);
        setLoading(false);
      });
  }, []);

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

  return (
    <div className="max-w-3xl">
      <h2 className="text-lg font-semibold mb-1">Deleted Posts Archive</h2>
      <p className="text-xs text-neutral-500 mb-6">
        Posts users have deleted themselves — kept here for moderation visibility, not restorable
        automatically (contact the user or manually re-create if needed).
      </p>

      {posts.length === 0 ? (
        <div className="card p-6 text-center text-neutral-500 text-sm">No deleted posts yet.</div>
      ) : (
        <div className="space-y-3">
          {posts.map((p) => (
            <div key={p.id} className="card p-4">
              <div className="flex items-center justify-between mb-2">
                <span className="font-semibold text-sm">{p.authorName}</span>
                <span className="text-xs text-neutral-500">
                  Deleted {new Date(p.deletedAt).toLocaleString()}
                </span>
              </div>
              <p className="text-sm text-neutral-300 whitespace-pre-wrap break-words mb-2">{p.content}</p>
              {p.attachmentUrl && (
                <p className="text-xs text-neutral-600">Attachment ({p.attachmentType}): {p.attachmentUrl}</p>
              )}
              <p className="text-xs text-neutral-600 mt-2">
                Originally posted: {p.originalCreatedAt ? new Date(p.originalCreatedAt).toLocaleString() : "—"}
              </p>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}
