import type { Metadata } from "next";
import { db, users, supportTickets, eq, desc } from "@epicure/db";
import { AdminSupportManager } from "@/components/admin/admin-support-manager";

export const metadata: Metadata = {};

export default async function AdminSupportPage() {
  const rows = await db
    .select({
      id: supportTickets.id,
      userEmail: users.email,
      username: users.username,
      type: supportTickets.type,
      title: supportTickets.title,
      description: supportTickets.description,
      status: supportTickets.status,
      giteaIssueUrl: supportTickets.giteaIssueUrl,
      giteaError: supportTickets.giteaError,
      createdAt: supportTickets.createdAt,
    })
    .from(supportTickets)
    .innerJoin(users, eq(supportTickets.userId, users.id))
    .orderBy(desc(supportTickets.createdAt));

  return (
    <div className="space-y-6">
      <div>
        <h1 className="text-2xl font-bold tracking-tight">Support Tickets</h1>
        <p className="text-muted-foreground text-sm mt-1">
          Bug reports, suggestions, and questions submitted through the app.
        </p>
      </div>
      <AdminSupportManager
        initialTickets={rows.map((r) => ({ ...r, createdAt: r.createdAt.toISOString() }))}
      />
    </div>
  );
}
