"use client";

import { createContext, type ReactNode, useContext } from "react";

import type { CharacterProfile } from "@/data/constants/character";
import { getCharacterRoutes, type CharacterRoutes } from "@/router/routes";

const CharacterContext = createContext<CharacterProfile | null>(null);

export interface CharacterProviderProps {
  character: CharacterProfile;
  children: ReactNode;
}

export function CharacterProvider({
  character,
  children,
}: CharacterProviderProps) {
  return (
    <CharacterContext.Provider value={character}>
      {children}
    </CharacterContext.Provider>
  );
}

export function useActiveCharacter(): CharacterProfile {
  const character = useContext(CharacterContext);
  if (!character) {
    throw new Error("useActiveCharacter must be used within CharacterProvider.");
  }
  return character;
}

export function useActiveCharacterRoutes(): CharacterRoutes {
  return getCharacterRoutes(useActiveCharacter().slug);
}
