"use client";

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

interface SEOMetadata {
  title: string;
  metaDescription: string;
  keywords?: string;
  ogTitle?: string;
  ogDescription?: string;
  ogImage?: string;
  twitterCard?: string;
  canonicalUrl?: string;
  structuredData?: any;
  robotsMeta?: string;
}

interface SEOContextType {
  metadata: SEOMetadata;
  updateMetadata: (metadata: Partial<SEOMetadata>) => void;
}

const defaultMetadata: SEOMetadata = {
  title: "Nitescape",
  metaDescription: "Welcome to Nitescape - Your Digital Solutions Partner",
  robotsMeta: "index,follow",
};

const SEOContext = createContext<SEOContextType>({
  metadata: defaultMetadata,
  updateMetadata: () => {},
});

export const useSEO = () => useContext(SEOContext);

interface SEOProviderProps {
  children: ReactNode;
  initialMetadata?: Partial<SEOMetadata>;
}

export const SEOProvider = ({ children, initialMetadata }: SEOProviderProps) => {
  const [metadata, setMetadata] = useState<SEOMetadata>({
    ...defaultMetadata,
    ...initialMetadata,
  });

  const updateMetadata = (newMetadata: Partial<SEOMetadata>) => {
    setMetadata((prev: SEOMetadata) => ({
      ...prev,
      ...newMetadata,
    }));
  };

  return <SEOContext.Provider value={{ metadata, updateMetadata }}>{children}</SEOContext.Provider>;
};
