"use client";

import { useEffect } from "react";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { Shield, Users, BarChart3 } from "lucide-react";
import { Button } from "@/components/ui/button";
import Link from "next/link";
import { ThemeToggle } from "@/components/layout/theme-toggle";
import { AppLoader } from "@/components/ui/app-loader";
import { BrandLockup } from "@/components/brand/brand-logo";

export default function Home() {
  const { data: session, status } = useSession();
  const router = useRouter();

  useEffect(() => {
    if (status === "authenticated" && session) {
      const roleDashboards = {
        SUPER_ADMIN: "/dashboard/admin",
        UPPER_MANAGEMENT: "/dashboard/management",
        MANAGER: "/dashboard/manager",
        UNIT_LEAD: "/dashboard/lead",
        MEMBER: "/dashboard/member",
      };

      const dashboardUrl =
        roleDashboards[session.user.role as keyof typeof roleDashboards] ||
        "/dashboard";
      router.push(dashboardUrl);
    }
  }, [session, status, router]);

  if (status === "loading" || status === "authenticated") {
    return (
      <AppLoader
        variant="full"
        label={
          status === "authenticated"
            ? "Opening your workspace"
            : "Loading Miaro"
        }
      />
    );
  }

  return (
    <div className="min-h-screen bg-background">
      <div className="mx-auto max-w-6xl px-4 sm:px-6 py-4">
        <header className="rounded-2xl border border-border bg-card px-5 py-4 flex items-center justify-between">
          <BrandLockup height={36} priority />
          <div className="flex items-center gap-2">
            <ThemeToggle />
            <Link href="/auth/login">
              <Button size="sm">Sign in</Button>
            </Link>
          </div>
        </header>
      </div>

      <main className="mx-auto max-w-6xl px-4 sm:px-6 py-16 animate-page-enter">
        <div className="rounded-2xl border border-border bg-card px-8 py-16 md:px-16 md:py-20">
          <div className="max-w-2xl">
            <p className="text-sm text-muted-foreground mb-3 font-secondary">
              Assessment workspace for modern organisations
            </p>
            <h1 className="text-4xl md:text-5xl font-semibold tracking-tight leading-[1.1] mb-5 font-secondary">
              People, performance, and evaluations — clearly measured.
            </h1>
            <p className="text-base text-muted-foreground mb-8 max-w-xl">
              Role-based operations with KPI tracking, attendance insight, and
              organisational clarity in one place.
            </p>
            <Link href="/auth/login">
              <Button size="lg" className="h-11 px-6">
                Enter Miaro
              </Button>
            </Link>
          </div>

          <div className="mt-16 grid grid-cols-1 md:grid-cols-3 gap-px bg-border rounded-xl overflow-hidden border border-border">
            {[
              {
                icon: Shield,
                title: "Role-based access",
                body: "Five-tier permissions with secure session routing.",
              },
              {
                icon: Users,
                title: "People operations",
                body: "Lifecycle management, attendance, and report cards.",
              },
              {
                icon: BarChart3,
                title: "Evaluation insight",
                body: "KPIs, assessments, and performance health in one view.",
              },
            ].map(({ icon: Icon, title, body }) => (
              <div key={title} className="bg-card p-6">
                <Icon
                  className="h-5 w-5 mb-3 text-foreground"
                  strokeWidth={1.75}
                />
                <h3 className="text-sm font-semibold mb-1.5 font-secondary">
                  {title}
                </h3>
                <p className="text-sm text-muted-foreground leading-relaxed">
                  {body}
                </p>
              </div>
            ))}
          </div>
        </div>
      </main>
    </div>
  );
}
