import { Metadata } from 'next';
import { getWebsitePublicEnv } from '@/lib/config/env';

interface MetadataOptions {
  title: string;
  description: string;
  path: string;
  image?: string;
  type?: 'website' | 'article' | 'profile';
}

export class MetadataHelper {
  private static readonly DEFAULT_IMAGE = '/og-image.png';
  private static readonly SITE_NAME = 'GridPilot';

  static generate({
    title,
    description,
    path,
    image = this.DEFAULT_IMAGE,
    type = 'website',
  }: MetadataOptions): Metadata {
    const env = getWebsitePublicEnv();
    const baseUrl = env.NEXT_PUBLIC_SITE_URL || 'https://gridpilot.com';
    const url = `${baseUrl}${path}`;
    const fullTitle = `${title} | ${this.SITE_NAME}`;

    return {
      title: fullTitle,
      description,
      alternates: {
        canonical: url,
      },
      openGraph: {
        title: fullTitle,
        description,
        url,
        siteName: this.SITE_NAME,
        images: [
          {
            url: image.startsWith('http') ? image : `${baseUrl}${image}`,
            width: 1200,
            height: 630,
            alt: title,
          },
        ],
        locale: 'en_US',
        type,
      },
      twitter: {
        card: 'summary_large_image',
        title: fullTitle,
        description,
        images: [image.startsWith('http') ? image : `${baseUrl}${image}`],
      },
    };
  }
}
