import type { TenantEvent } from '../api';

function isTranslatableName(value: unknown): value is Record<string, string> {
  return Boolean(value && typeof value === 'object');
}

export function resolveEventDisplayName(event?: TenantEvent | null): string {
  if (!event) {
    return 'Event';
  }

  const { name, slug } = event;

  if (typeof name === 'string' && name.trim().length > 0) {
    return name;
  }

  if (isTranslatableName(name)) {
    const match = Object.values(name).find(
      (entry) => typeof entry === 'string' && entry.trim().length > 0,
    );
    if (match) {
      return match;
    }
  }

  return slug ?? 'Event';
}

export function formatEventDate(value?: string | null, locale = 'de-DE'): string | null {
  if (!value) {
    return null;
  }

  const date = new Date(value);
  if (Number.isNaN(date.getTime())) {
    return null;
  }

  try {
    return new Intl.DateTimeFormat(locale, {
      day: '2-digit',
      month: 'short',
      year: 'numeric',
    }).format(date);
  } catch {
    return date.toISOString().slice(0, 10);
  }
}

export function formatEventDateTime(value?: string | null, locale = 'de-DE'): string | null {
  if (!value) {
    return null;
  }

  const date = new Date(value);
  if (Number.isNaN(date.getTime())) {
    return null;
  }

  try {
    return new Intl.DateTimeFormat(locale, {
      day: '2-digit',
      month: 'short',
      year: 'numeric',
      hour: '2-digit',
      minute: '2-digit',
    }).format(date);
  } catch {
    return date.toISOString().slice(0, 16).replace('T', ' ');
  }
}

export function resolveEngagementMode(event?: TenantEvent | null): 'tasks' | 'photo_only' | null {
  if (!event) {
    return null;
  }

  if (event.engagement_mode) {
    return event.engagement_mode;
  }

  if (event.settings && typeof event.settings === 'object') {
    const mode = event.settings.engagement_mode;
    if (mode === 'tasks' || mode === 'photo_only') {
      return mode;
    }
  }

  return null;
}

export function isBrandingAllowed(event?: TenantEvent | null): boolean {
  if (!event) return true;
  const settings = (event.settings ?? {}) as Record<string, unknown>;
  const packageAllowed = (event.package as any)?.branding_allowed;
  if (packageAllowed === false) {
    return false;
  }
  if (typeof settings.branding_allowed === 'boolean') {
    return settings.branding_allowed;
  }
  return true;
}

export function isWatermarkAllowed(event?: TenantEvent | null): boolean {
  if (!event) return true;
  const settings = (event.settings ?? {}) as Record<string, unknown>;
  const packageAllowed = (event.package as any)?.watermark_allowed;
  if (packageAllowed === false) {
    return false;
  }
  if (typeof settings.watermark_allowed === 'boolean') {
    return settings.watermark_allowed;
  }
  return true;
}

export function isWatermarkRemovalAllowed(event?: TenantEvent | null): boolean {
  if (!event) return false;
  const settings = (event.settings ?? {}) as Record<string, unknown>;
  if (typeof settings.watermark_removal_allowed === 'boolean') {
    return settings.watermark_removal_allowed;
  }
  return false;
}

export function formatEventStatusLabel(
  status: TenantEvent['status'] | null,
  t: (key: string, options?: Record<string, unknown>) => string,
): string {
  const map: Record<string, { key: string; fallback: string }> = {
    published: { key: 'events.status.published', fallback: 'Veröffentlicht' },
    draft: { key: 'events.status.draft', fallback: 'Entwurf' },
    archived: { key: 'events.status.archived', fallback: 'Archiviert' },
  };

  const target = map[status ?? 'draft'] ?? map.draft;
  return t(target.key, { defaultValue: target.fallback });
}
