import DeleteSubscriberButton from "@/components/admin/DeleteSubscriberButton"
import { getAllSubscribers } from "@/lib/admin/queries"

function formatDate(date: Date | null) {
  if (!date) return "Not available"

  return date.toLocaleDateString("en-GB", {
    day: "numeric",
    month: "short",
    year: "numeric",
  })
}

export default async function AdminSubscribersPage() {
  const subscribers = await getAllSubscribers()
  const emails = subscribers.map((subscriber) => subscriber.email).join("\n")

  return (
    <div>
      <header className="mb-8">
        <div className="flex items-center gap-3">
          <h1 className="font-serif text-[2rem] font-normal text-[var(--color-text-primary)]">
            Newsletter Subscribers
          </h1>
          <span className="rounded border border-[var(--color-border)] bg-[var(--color-surface)] px-2 py-1 text-[12px] text-[var(--color-text-secondary)]">
            {subscribers.length}
          </span>
        </div>
        <p className="mt-1 text-[14px] text-[var(--color-text-secondary)]">
          Manage newsletter signups and copy emails for export.
        </p>
      </header>

      <p className="mb-4 text-[12px] text-[var(--color-text-secondary)]">
        To export: copy the emails below and paste into your email provider
        (Resend, Mailchimp, etc.).
      </p>

      <div className="overflow-x-auto rounded-lg border border-[var(--color-border)]">
        <table className="w-full border-collapse text-left">
          <thead className="bg-[var(--color-surface)] text-[11px] uppercase tracking-[0.14em] text-[var(--color-accent)]">
            <tr>
              <th className="px-4 py-3 font-normal">Email</th>
              <th className="px-4 py-3 font-normal">Confirmed</th>
              <th className="px-4 py-3 font-normal">Subscribed At</th>
              <th className="px-4 py-3 font-normal">Actions</th>
            </tr>
          </thead>
          <tbody>
            {subscribers.length > 0 ? (
              subscribers.map((subscriber) => (
                <tr
                  key={subscriber.id}
                  className="border-t border-[var(--color-border)] text-[13px]"
                >
                  <td className="px-4 py-3 text-[var(--color-text-primary)]">
                    {subscriber.email}
                  </td>
                  <td className="px-4 py-3">
                    {subscriber.confirmed ? (
                      <span className="text-[#22c55e]">Yes</span>
                    ) : (
                      <span className="text-[var(--color-text-secondary)]">
                        Pending
                      </span>
                    )}
                  </td>
                  <td className="px-4 py-3 text-[var(--color-text-secondary)]">
                    {formatDate(subscriber.subscribedAt)}
                  </td>
                  <td className="px-4 py-3">
                    <DeleteSubscriberButton id={subscriber.id} />
                  </td>
                </tr>
              ))
            ) : (
              <tr>
                <td
                  colSpan={4}
                  className="px-4 py-8 text-center text-[13px] text-[var(--color-text-secondary)]"
                >
                  No subscribers yet.
                </td>
              </tr>
            )}
          </tbody>
        </table>
      </div>

      <div className="mt-8">
        <label className="mb-1 block text-[12px] uppercase tracking-[0.16em] text-[var(--color-accent)]">
          All emails (copy to export)
        </label>
        <textarea
          readOnly
          rows={6}
          value={emails}
          className="w-full resize-y rounded border border-[var(--color-border)] bg-[var(--color-surface)] px-3 py-2 font-mono text-[13px] text-[var(--color-text-primary)] outline-none focus:border-[var(--color-accent)]"
        />
      </div>
    </div>
  )
}
