import { redirect } from "next/navigation";

import { getActiveContext } from "@/lib/appwrite/active-context";
import { CustomerInsights } from "../dashboard-2/components/customer-insights";
import { MetricsOverview } from "../dashboard-2/components/metrics-overview";
import { QuickActions } from "../dashboard-2/components/quick-actions";
import { RecentTransactions } from "../dashboard-2/components/recent-transactions";
import { RevenueBreakdown } from "../dashboard-2/components/revenue-breakdown";
import { SalesChart } from "../dashboard-2/components/sales-chart";
import { TopProducts } from "../dashboard-2/components/top-products";

export default async function DashboardPage() {
  const ctx = await getActiveContext();
  if (!ctx) redirect("/onboarding");

  const firstName = ctx.user.name?.split(" ")[0] ?? "";
  const companyName = ctx.settings?.companyName ?? "Çalışma alanı";

  return (
    <div className="flex-1 space-y-6 px-6 pt-0">
      <div className="flex flex-col justify-between gap-4 md:flex-row md:items-center md:gap-6">
        <div className="flex flex-col gap-1">
          <p className="text-muted-foreground text-sm">{companyName}</p>
          <h1 className="text-2xl font-bold tracking-tight">
            {firstName ? `Hoş geldiniz, ${firstName}` : "Genel bakış"}
          </h1>
          <p className="text-muted-foreground text-sm">
            İşletmenizin temel metriklerini ve son hareketleri buradan takip edin.
          </p>
        </div>
        <QuickActions />
      </div>

      <div className="@container/main space-y-6">
        <MetricsOverview />

        <div className="grid grid-cols-1 gap-6 @5xl:grid-cols-2">
          <SalesChart />
          <RevenueBreakdown />
        </div>

        <div className="grid grid-cols-1 gap-6 @5xl:grid-cols-2">
          <RecentTransactions />
          <TopProducts />
        </div>

        <CustomerInsights />
      </div>
    </div>
  );
}
