import type { UseCaseOutputPort } from '@core/shared/application';
import type { GetMediaResult } from '@core/media/application/use-cases/GetMediaUseCase';
import type { GetMediaOutputDTO } from '../dtos/GetMediaOutputDTO';

export type GetMediaResponseModel = GetMediaOutputDTO | null;

export class GetMediaPresenter implements UseCaseOutputPort<GetMediaResult> {
  private model: GetMediaResponseModel | null = null;

  reset(): void {
    this.model = null;
  }

  present(result: GetMediaResult): void {
    const media = result.media;

    const model: GetMediaResponseModel = {
      id: media.id,
      url: media.url,
      type: media.type,
      uploadedAt: media.uploadedAt,
      size: media.size,
    };

    // Best-effort mapping from arbitrary metadata
    const category = (media.metadata as { category?: string } | undefined)?.category;
    if (category !== undefined) {
      model.category = category;
    }

    this.model = model;
  }

  getResponseModel(): GetMediaResponseModel | null {
    return this.model;
  }

  get responseModel(): GetMediaResponseModel {
    return this.model ?? null;
  }
}