<?php

declare(strict_types=1);

namespace Tests\Framework\Filesystem;

use App\Framework\Filesystem\Exceptions\FileNotFoundException;
use App\Framework\Filesystem\FileStorage;
use App\Framework\Filesystem\InMemoryStorage;

test('speichert und lädt Dateien', function () {
    $tempFile = tempnam(sys_get_temp_dir(), 'test_');
    $content = 'Testinhalt ' . uniqid();

    $storage = new FileStorage();
    $storage->put($tempFile, $content);

    expect($storage->exists($tempFile))->toBeTrue();
    expect($storage->get($tempFile))->toBe($content);
    expect($storage->size($tempFile))->toBe(strlen($content));

    // Aufräumen
    $storage->delete($tempFile);
    expect($storage->exists($tempFile))->toBeFalse();
});

test('wirft Exception bei nicht existierender Datei', function () {
    $storage = new FileStorage();
    $nonExistingFile = '/tmp/doesnt_exist_' . uniqid();

    expect(fn () => $storage->get($nonExistingFile))
        ->toThrow(FileNotFoundException::class);
});

test('kopiert Dateien', function () {
    $tempFile = tempnam(sys_get_temp_dir(), 'src_');
    $destFile = tempnam(sys_get_temp_dir(), 'dest_');
    unlink($destFile); // Löschen, damit copy funktioniert

    $content = 'Kopierinhalt ' . uniqid();

    $storage = new FileStorage();
    $storage->put($tempFile, $content);
    $storage->copy($tempFile, $destFile);

    expect($storage->exists($destFile))->toBeTrue();
    expect($storage->get($destFile))->toBe($content);

    // Aufräumen
    $storage->delete($tempFile);
    $storage->delete($destFile);
});

test('InMemoryStorage funktioniert wie FileStorage', function () {
    $storage = new InMemoryStorage();
    $path = '/virtual/test.txt';
    $content = 'Virtueller Inhalt';

    $storage->put($path, $content);
    expect($storage->exists($path))->toBeTrue();
    expect($storage->get($path))->toBe($content);
    expect($storage->size($path))->toBe(strlen($content));

    $copyPath = '/virtual/copy.txt';
    $storage->copy($path, $copyPath);
    expect($storage->get($copyPath))->toBe($content);

    $storage->delete($path);
    expect($storage->exists($path))->toBeFalse();
});
