<?php

declare(strict_types=1);

namespace App\Application\Website;

use App\Domain\Media\ImageProcessor;
use App\Domain\Media\ImageRepository;
use App\Domain\Media\ImageSourceSetGenerator;
use App\Domain\Media\ImageVariantRepository;
use App\Framework\Attributes\Route;
use App\Framework\Attributes\StaticPage;
use App\Framework\Http\Method;
use App\Framework\Meta\Keywords;
use App\Framework\Meta\StaticPageMetaResolver;
use App\Framework\Router\ActionResult;
use App\Framework\Router\Result\Redirect;
use App\Framework\Router\Result\ViewResult;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

final readonly class ShowImpressum
{
    public function __construct(
        private ImageRepository $imageRepository,
        private ImageProcessor $imageProcessor,
        private ImageVariantRepository $imageVariantRepository,
    )
    {
    }


    #[StaticPage]
    #[Route(path: '/impressum', name: 'impressum')]
    public function impressum(): ViewResult
    {
        /*$sg = new ImageSourceSetGenerator();

        $images = $this->imageRepository->findAll();

        foreach($images as $image) {
            echo $sg->generatePictureElement($image);
        }*/


        // Cleanup Media Files that are not saved to Database

        $path = '/var/www/html/storage/uploads/2025';

        $rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));

        /** @var \SplFileInfo $file */
        foreach($rii as $file) {

            $variant = $this->imageVariantRepository->findByFilename($file->getFilename());

            if($variant === null) {
                $image = $this->imageRepository->findByFilename($file->getFilename());
                if($image === null) {
                    $success = unlink($file->getPathname());
                    #var_dump($success);
                }
            }
        }

        // Delete empty Folders.

        $emptyDirs = [];

        $iterator = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST // Ordner erst nach dem Inhalt prüfen
        );

        foreach ($iterator as $fileInfo) {
            if ($fileInfo->isDir()) {
                // Verzeichnis öffnen und prüfen, ob es leer ist
                $files = array_diff(scandir($fileInfo->getPathname()), ['.', '..']);
                if (empty($files)) {
                    $emptyDirs[] = $fileInfo->getPathname();
                    rmdir($fileInfo->getPathname());
                }
            }
        }
        #var_dump($emptyDirs);






        /*$images = $this->imageRepository->findAll();

        foreach($images as $image) {
            $path = ($image->path);
            #chmod($path, 0755);
            #var_dump(is_writable($path));
            $variants = $this->imageProcessor->createAllVariants($image);

            foreach($variants as $variant) {
                try {
                    $this->imageVariantRepository->save($variant);
                } catch (\Throwable $th) {
                    var_dump($variant);
                    var_dump('already saved');
                }
            }
        }*/

        return new ViewResult(
            'impressum',
            new StaticPageMetaResolver(
                'Impressum',
                'Hallo Welt!',
            )(),
            ['text' => 'Hallo Welt!'],
        );
    }

    #[Route(path: '/datenschutz', name: 'datenschutz')]
    public function datenschutz(): ViewResult
    {
        return new ViewResult(
            'impressum',
            new StaticPageMetaResolver(
                'Datenschutz',
                'Hallo Welt!',
            )(),
            ['title' => 'Datenschutz!'],
        );
    }
}
