"use client";

import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "./auth-context";
import { layoutStyles } from "./styles/shared";
import constants from "../../shared/constants.json";

const { ADMIN, REGULAR } = constants.roles;

export default function Home() {
  const { user, isLoading, hasRole } = useAuth();
  const router = useRouter();

  useEffect(() => {
    if (isLoading) return;

    if (!user) {
      router.replace("/login");
      return;
    }

    // Redirect based on role
    if (hasRole(ADMIN)) {
      router.replace("/admin/appointments");
    } else if (hasRole(REGULAR)) {
      router.replace("/booking");
    } else {
      // User with no roles - redirect to login
      router.replace("/login");
    }
  }, [user, isLoading, hasRole, router]);

  return (
    <main style={layoutStyles.main}>
      <div style={layoutStyles.loader}>Redirecting...</div>
    </main>
  );
}
