import { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { seoApi } from '@/db/api';
import type { PageSEO, SEOSettings } from '@/types/seo';

export function useSEO(pagePath?: string) {
  const location = useLocation();
  const currentPath = pagePath || location.pathname;
  
  const [globalSettings, setGlobalSettings] = useState<SEOSettings | null>(null);
  const [pageSEO, setPageSEO] = useState<PageSEO | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const loadSEO = async () => {
      try {
        setLoading(true);
        
        // Global ayarları yükle
        const settings = await seoApi.getSettings();
        setGlobalSettings(settings);

        // Sayfa SEO bilgilerini yükle
        const pageData = await seoApi.getPageSEO(currentPath);
        setPageSEO(pageData);
      } catch (error) {
        console.error('SEO yükleme hatası:', error);
      } finally {
        setLoading(false);
      }
    };

    loadSEO();
  }, [currentPath]);

  return {
    globalSettings,
    pageSEO,
    loading,
  };
}
