import logger from './logger';
import { shortUrlApiKey, shortUrlDomain } from './env';

interface ShortUrlResponse {
  shortUrl: string;
  longUrl: string;
  shortCode: string;
  dateCreated: string;
}

interface ShortUrlRequest {
  longUrl: string;
  validUntil?: string;
  maxVisits?: number;
  tags?: string[];
  shortCodeLength?: number;
  domain?: string;
  findIfExists?: boolean;
  validateUrl?: boolean;
  forwardQuery?: boolean;
  crawlable?: boolean;
}

export async function formatToShortUrl({
  url,
  validUntil,
  maxVisits,
}: {
  url: string;
  validUntil?: string;
  maxVisits?: number;
}): Promise<string> {
  const apiKey = shortUrlApiKey;

  if (!apiKey) {
    return url;
  }

  try {
    const requestPayload: ShortUrlRequest = {
      longUrl: url,
      validUntil,
      maxVisits,
      tags: [],
      shortCodeLength: 8,
      domain: shortUrlDomain,
      findIfExists: true,
      validateUrl: true,
      forwardQuery: true,
      crawlable: true,
    };

    const response = await fetch(`https://${shortUrlDomain}/rest/v3/short-urls`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': apiKey,
      },
      body: JSON.stringify(requestPayload),
    });

    if (!response.ok) {
      logger.warn('Failed to create short URL, using original URL', {
        status: response.status,
        statusText: response.statusText,
        url,
      });
      return url;
    }

    const data: ShortUrlResponse = await response.json();
    return data.shortUrl;
  } catch (error) {
    logger.error('Error creating short URL, using original URL', { error, url });
    return url;
  }
}
