import {inject, Injectable} from '@angular/core';
import {Meta, Title} from '@angular/platform-browser';
import {SEOConfig} from '../_models/seo';

@Injectable({
  providedIn: 'root'
})
export class SeoService {

  private meta = inject(Meta);
  private title = inject(Title);

  updateSEOMetaTags(config: SEOConfig) {
    // Mise à jour du titre
    this.title.setTitle(config.title);

    // Mise à jour des meta tags standards
    this.meta.updateTag({ name: 'description', content: config.description });
    if (config.keywords) {
      this.meta.updateTag({ name: 'keywords', content: config.keywords });
    }

    // Mise à jour des meta tags Open Graph
    this.meta.updateTag({ property: 'og:title', content: config.ogTitle || config.title });
    this.meta.updateTag({ property: 'og:description', content: config.ogDescription || config.description });
    if (config.ogImage) {
      this.meta.updateTag({ property: 'og:image', content: config.ogImage });
    }

    // Autres meta tags importants
    this.meta.updateTag({ name: 'robots', content: 'index,follow' });
    this.meta.updateTag({ name: 'viewport', content: 'width=device-width, initial-scale=1' });
  }

  createCanonicalLink(url?: string) {
    const canURL = url || document.URL;
    const link: HTMLLinkElement = document.createElement('link');
    link.setAttribute('rel', 'canonical');
    link.setAttribute('href', canURL);
    document.head.appendChild(link);
  }

}
