import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

// This check can be removed, it is just for tutorial purposes
export const hasEnvVars =
  (process.env.SUPABASE_URL || process.env.NEXT_PUBLIC_SUPABASE_URL) &&
  (process.env.SUPABASE_ANON_KEY ||
    process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY ||
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY);

export function resolveSupabaseUrl(url: string | undefined): string | undefined {
  if (!url) return url;
  
  // Bestimme die externe Basis-URL aus Umgebungsvariablen (falls vorhanden und extern)
  let extBase = process.env.NEXT_PUBLIC_SUPABASE_URL_EXTERNAL || process.env.NEXT_PUBLIC_SUPABASE_URL || '';
  if (extBase.includes('kong') || extBase.includes('supabase-kong')) {
    extBase = ''; // ignorieren, da intern
  }

  if (typeof window !== 'undefined') {
    try {
      const parsed = new URL(url);
      if (parsed.hostname === 'supabase-kong' || parsed.hostname === 'kong') {
        if (extBase) {
          const extUrl = new URL(extBase);
          parsed.hostname = extUrl.hostname;
          parsed.port = extUrl.port;
          parsed.protocol = extUrl.protocol;
        } else {
          // Fallback auf die aktuelle Browser-Domain
          parsed.hostname = window.location.hostname;
          parsed.protocol = window.location.protocol;
          if (window.location.hostname === 'localhost' && parsed.port === '8000') {
            parsed.port = '8000';
          } else {
            parsed.port = ''; // Port entfernen für Produktion hinter Reverse Proxy
          }
        }
        return parsed.toString().replace(/\/$/, '');
      }
      
      // Mitigate Mixed Content: if the site is HTTPS, force Supabase to HTTPS
      if (window.location.protocol === 'https:' && parsed.protocol === 'http:') {
        parsed.protocol = 'https:';
        return parsed.toString().replace(/\/$/, '');
      }
    } catch {
      let resolved = url;
      if (extBase) {
        const extUrl = new URL(extBase);
        resolved = resolved
          .replace('//supabase-kong:8000', `//${extUrl.host}`)
          .replace('//kong:8000', `//${extUrl.host}`)
          .replace('//supabase-kong', `//${extUrl.host}`)
          .replace('//kong', `//${extUrl.host}`);
      } else {
        resolved = resolved
          .replace('//supabase-kong:8000', `//${window.location.hostname}`)
          .replace('//kong:8000', `//${window.location.hostname}`)
          .replace('//supabase-kong', `//${window.location.hostname}`)
          .replace('//kong', `//${window.location.hostname}`);
      }
      if (window.location.protocol === 'https:') {
        resolved = resolved.replace('http://', 'https://');
      }
      return resolved;
    }
  } else {
    // Server-Side: Umschreiben auf externe URL oder internen Host
    const defaultFallback = process.env.IS_DOCKER === 'true' ? 'http://kong:8000' : 'http://localhost:8000';
    try {
      const parsed = new URL(url);
      if (parsed.hostname === 'supabase-kong' || parsed.hostname === 'kong') {
        const ext = extBase || defaultFallback;
        const extUrl = new URL(ext);
        parsed.hostname = extUrl.hostname;
        parsed.port = extUrl.port;
        parsed.protocol = extUrl.protocol;
        return parsed.toString().replace(/\/$/, '');
      }
    } catch {
      const target = extBase || defaultFallback;
      const targetUrl = new URL(target);
      return url
        .replace('//supabase-kong:8000', `//${targetUrl.host}`)
        .replace('//kong:8000', `//${targetUrl.host}`)
        .replace('//supabase-kong', `//${targetUrl.host}`)
        .replace('//kong', `//${targetUrl.host}`);
    }
  }
  return url;
}
