<?php

require_once __DIR__ . '/../../../autoload/Domain/Tasks/TaskAttachmentRepository.php';

use Domain\Tasks\TaskAttachmentRepository;

function run_task_attachment_repository_tests()
{
  assert_true(
    TaskAttachmentRepository::effectiveTitle( '  Raport  ', 'plik.pdf' ) === 'Raport',
    'Expected effective title to prefer trimmed custom title.'
  );

  assert_true(
    TaskAttachmentRepository::effectiveTitle( '', 'plik.pdf' ) === 'plik.pdf',
    'Expected effective title to fallback to original file name.'
  );

  $sanitized = TaskAttachmentRepository::sanitizeFileName( 'Załącznik #1 (final).pdf' );
  assert_true(
    strpos( $sanitized, '#' ) === false
      && strpos( $sanitized, '(' ) === false
      && strpos( $sanitized, ')' ) === false
      && strpos( $sanitized, ' ' ) === false
      && substr( $sanitized, -4 ) === '.pdf',
    'Expected sanitized file name to remove unsupported characters and keep extension.'
  );

  assert_true(
    TaskAttachmentRepository::detectExtensionFromContent( "%PDF-1.7\nexample" ) === 'pdf',
    'Expected PDF extension to be detected from binary content.'
  );

  assert_true(
    TaskAttachmentRepository::effectiveTitleWithExtension( '', 'zalacznik', 'pdf', '' ) === 'zalacznik.pdf',
    'Expected display title fallback to include file extension.'
  );

  assert_true(
    TaskAttachmentRepository::effectiveTitleWithExtension(
      'zalacznik.pdf',
      'VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf',
      'pdf',
      ''
    ) === 'VIDOK_Instrukcja Montażu I Uruchomienia - Wyroby Elektryczne.pdf',
    'Expected generic title to be ignored when original file name is available.'
  );
}
