"use client";

import { useState, useEffect, createContext, useContext } from "react";
import { ReactNode } from "react";
import { useRouter, usePathname } from "next/navigation";
import type { Child } from "@/types";

interface FamilyContextType {
  familyId: string | null;
  familyName: string | null;
  childId: string | null;
  child: Child | null;
  children: Child[];
  loading: boolean;
  tier: "free" | "pro";
  memberCount: number;
  updateChildImage: (childId: string, imageUrl: string | null) => void;
}

const FamilyContext = createContext<FamilyContextType>({
  familyId: null,
  familyName: null,
  childId: null,
  child: null,
  children: [],
  loading: true,
  tier: "free",
  memberCount: 2,
  updateChildImage: () => {},
});

export function useFamily() {
  return useContext(FamilyContext);
}

export function FamilyProvider({ children: providerChildren }: { children: ReactNode }) {
  const router = useRouter();
  const pathname = usePathname();
  const [familyId, setFamilyId] = useState<string | null>(null);
  const [familyName, setFamilyName] = useState<string | null>(null);
  const [childId, setChildId] = useState<string | null>(null);
  const [child, setChild] = useState<Child | null>(null);
  const [children, setChildren] = useState<Child[]>([]);
  const [loading, setLoading] = useState(true);
  const [tier, setTier] = useState<"free" | "pro">("free");
  const [memberCount, setMemberCount] = useState(2);

  useEffect(() => {
    if (pathname?.startsWith("/admin") || pathname === "/admin-login") {
      setLoading(false);
      return;
    }

    async function fetchFamilyData() {
      try {
        // Get current session from database
        const sessionRes = await fetch("/api/auth/signin");
        const sessionData = await sessionRes.json();

        // Not authenticated - stay on current page, don't redirect
        if (!sessionData.authenticated) {
          setLoading(false);
          return;
        }

        // Authenticated but no family - go to onboarding
        if (!sessionData.familyId) {
          router.push("/onboarding");
          setLoading(false);
          return;
        }

        // Authenticated with family - check for children
        const familyId = sessionData.familyId;
        const res = await fetch(`/api/children?familyId=${familyId}`);
        const data = await res.json();

        // Handle API error
        if (!res.ok || data.error) {
          console.error("Failed to fetch children:", data.error);
          setLoading(false);
          return;
        }

        if (data.children?.length > 0) {
          const childList: Child[] = data.children.map((c: Child) => ({
            id: c.id,
            name: c.name,
            birthDate: c.birthDate,
            sex: c.sex,
            imageUrl: c.imageUrl ?? null,
          }));

          setChildren(childList);
          setChild(childList[0]);
          setChildId(childList[0].id);
        } else {
          // No children - go to onboarding
          router.push("/onboarding");
          setLoading(false);
          return;
        }

        setFamilyId(familyId);
        setFamilyName(sessionData.familyName || "My Family");
        setTier(sessionData.tier || "free");

        // Fetch actual member count
        try {
          const membersRes = await fetch(`/api/family/members?familyId=${familyId}`);
          const membersData = await membersRes.json();
          setMemberCount(membersData.members?.length || 2);
        } catch {
          setMemberCount(2);
        }
      } catch (err) {
        console.error("Failed to fetch family:", err);
      } finally {
        setLoading(false);
      }
    }

    fetchFamilyData();
  }, [router]);

  const updateChildImage = (cId: string, imageUrl: string | null) => {
    setChildren(prev => prev.map(c => c.id === cId ? { ...c, imageUrl } : c));
    setChild(prev => prev?.id === cId ? { ...prev, imageUrl } : prev);
  };

  return (
    <FamilyContext.Provider
      value={{
        familyId,
        familyName,
        childId,
        child,
        children,
        loading,
        tier,
        memberCount,
        updateChildImage,
      }}
    >
      {providerChildren}
    </FamilyContext.Provider>
  );
}