<?php

namespace App\Service;

use App\Entity\Mod;
use App\Entity\Modpack;
use Doctrine\Common\Collections\Collection;

class ModpackZipService
{
    public function __construct(private readonly string $tmpDir,
                                private readonly UploaderService $uploaderService,) {}

    /**
     * @param Mod[] $mods
     */
    public function createZip(Modpack $modpack, array $mods): string
    {
        if (!is_dir($this->tmpDir)) {
            mkdir($this->tmpDir, 0777, true);
        }

        $zipPath = $this->tmpDir . '/modpack-' . $modpack->getId() . '.zip';

        $zip = new \ZipArchive();
        $zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);

        foreach ($mods as $mod) {
            $modPath = $this->uploaderService->getUploadDir(UploaderService::DIR_MOD) . '/' . $mod->getUri() ; // chemin réel vers le fichier
            $zip->addFile($modPath, $mod->getNom() . '-' . $mod->getVersion() . '.jar');
        }

        $zip->close();

        return $zipPath;
    }
}
