<?php
namespace App\Services\Generators;
use Illuminate\Support\Facades\Storage;
use App\Models\Quote;

class ZipGenerator {
	public static function generate(Quote $quote, array $attachments = [], bool $force = false): array {
		$path = "quotes/{$quote->id}/zip.zip";
		if(!$force && Storage::exists($path)) return [1, Storage::get($path)];

		$zipfoldername = preg_replace('/[^\w\s]/', '', preg_replace('/[\s+]/', ' ', trim($quote->title)));

		$zip = new \ZipArchive();
		if($zip->open(Storage::path($path), \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === true) {
			foreach($attachments as $attachment) $zip->addFromString($zipfoldername . '/' . $attachment[1], $attachment[0]);
			$zip->close();
			return [1, Storage::get($path)];
			}
		return false;
		}
	}
