/**
 * UploadedMediaResolverAdapter
 * 
 * Resolves uploaded media references to image serving URLs.
 * Part of the adapters layer, implementing the MediaResolverPort interface.
 */

import { MediaReference } from '@core/domain/media/MediaReference';
import { MediaResolverPort } from '@core/ports/media/MediaResolverPort';

/**
 * Configuration for the UploadedMediaResolverAdapter
 */
export interface UploadedMediaResolverConfig {
  /**
   * Base path for uploaded assets (defaults to '/media/uploaded')
   */
  basePath?: string | undefined;
}

/**
 * UploadedMediaResolverAdapter
 * 
 * Resolves uploaded media references to image serving URLs.
 * 
 * URL format: /media/uploaded/{mediaId}
 * Examples:
 * - /media/uploaded/media-123
 * - /media/uploaded/media-456
 * 
 * Note: This is a stub implementation. In production, this would:
 * - Check if the media exists in storage
 * - Handle different file types (images, videos, documents)
 * - Handle access control and permissions
 * - Generate signed URLs for private media
 */
export class UploadedMediaResolverAdapter implements MediaResolverPort {
  private readonly basePath: string;

  constructor(config: UploadedMediaResolverConfig = {}) {
    this.basePath = config.basePath || '/media/uploaded';
  }

  /**
   * Resolve an uploaded media reference to a path-only URL
   * Returns paths like /media/uploaded/{mediaId} (no baseUrl)
   */
  async resolve(ref: MediaReference): Promise<string | null> {
    // Only handle uploaded references
    if (ref.type !== 'uploaded') {
      return null;
    }

    // Validate mediaId exists
    if (!ref.mediaId) {
      return null;
    }

    // Return path-only URL
    return `${this.basePath}/${ref.mediaId}`;
  }
}

/**
 * Factory function for creating UploadedMediaResolverAdapter instances
 */
export function createUploadedMediaResolver(
  config: UploadedMediaResolverConfig = {}
): UploadedMediaResolverAdapter {
  return new UploadedMediaResolverAdapter(config);
}