import { useEffect } from 'react';

export interface SEOProps {
  title: string;
  description: string;
  keywords?: string;
  ogTitle?: string;
  ogDescription?: string;
  ogImage?: string;
  twitterTitle?: string;
  twitterDescription?: string;
  twitterImage?: string;
  canonical?: string;
  type?: 'website' | 'article' | 'product' | 'profile';
  author?: string;
  publishedTime?: string;
  modifiedTime?: string;
}

/**
 * Custom hook for managing SEO meta tags dynamically
 * Centralizes SEO logic for all pages in the application
 */
export const useSEO = ({
  title,
  description,
  keywords,
  ogTitle,
  ogDescription,
  ogImage,
  twitterTitle,
  twitterDescription,
  twitterImage,
  canonical,
  type = 'website',
  author,
  publishedTime,
  modifiedTime,
}: SEOProps) => {
  useEffect(() => {
    // Update page title
    document.title = title;

    // Helper function to update or create meta tags
    const updateMetaTag = (selector: string, attribute: string, content: string) => {
      let element = document.querySelector(selector);
      if (element) {
        element.setAttribute(attribute, content);
      } else {
        element = document.createElement('meta');
        if (selector.startsWith('meta[name=')) {
          element.setAttribute('name', selector.match(/name="([^"]+)"/)?.[1] || '');
        } else if (selector.startsWith('meta[property=')) {
          element.setAttribute('property', selector.match(/property="([^"]+)"/)?.[1] || '');
        }
        element.setAttribute(attribute, content);
        document.head.appendChild(element);
      }
    };

    // Update description
    updateMetaTag('meta[name="description"]', 'content', description);

    // Update keywords if provided
    if (keywords) {
      updateMetaTag('meta[name="keywords"]', 'content', keywords);
    }

    // Update author if provided
    if (author) {
      updateMetaTag('meta[name="author"]', 'content', author);
    }

    // Update Open Graph tags
    updateMetaTag('meta[property="og:title"]', 'content', ogTitle || title);
    updateMetaTag('meta[property="og:description"]', 'content', ogDescription || description);
    updateMetaTag('meta[property="og:type"]', 'content', type);

    if (ogImage) {
      updateMetaTag('meta[property="og:image"]', 'content', ogImage);
    }

    // Update canonical URL
    if (canonical) {
      let linkElement = document.querySelector('link[rel="canonical"]');
      if (linkElement) {
        linkElement.setAttribute('href', canonical);
      } else {
        linkElement = document.createElement('link');
        linkElement.setAttribute('rel', 'canonical');
        linkElement.setAttribute('href', canonical);
        document.head.appendChild(linkElement);
      }
    }

    // Update Twitter Card tags
    updateMetaTag('meta[name="twitter:title"]', 'content', twitterTitle || ogTitle || title);
    updateMetaTag('meta[name="twitter:description"]', 'content', twitterDescription || ogDescription || description);

    if (twitterImage || ogImage) {
      updateMetaTag('meta[name="twitter:image"]', 'content', twitterImage || ogImage || '');
    }

    // Article-specific tags
    if (type === 'article') {
      if (publishedTime) {
        updateMetaTag('meta[property="article:published_time"]', 'content', publishedTime);
      }
      if (modifiedTime) {
        updateMetaTag('meta[property="article:modified_time"]', 'content', modifiedTime);
      }
      if (author) {
        updateMetaTag('meta[property="article:author"]', 'content', author);
      }
    }
  }, [
    title,
    description,
    keywords,
    ogTitle,
    ogDescription,
    ogImage,
    twitterTitle,
    twitterDescription,
    twitterImage,
    canonical,
    type,
    author,
    publishedTime,
    modifiedTime,
  ]);
};
