import Content from "@/components/dashboard/Content";
import { defineAbilitiesFor } from "@/lib/ability";
import { getUser } from "@/lib/auth";
import { getStateInfo } from "@/lib/getStateInfo";
import { removeToken } from "@/utils";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export default async function Dashboard({ searchParams }) {
  const { matchedCity } = getStateInfo();
  const user = await getUser();

  const ability = defineAbilitiesFor(user);
  const cookieStore = cookies();
  const token = cookieStore.get("access_token");

  if (!ability.can("access", "Dashboard")) {
    removeToken();
    return redirect("/login");
  }

  return (
    <Content
      logged={token.value}
      params={searchParams}
      matchedCity={matchedCity}
    />
  );
}
