<?php

namespace Lucent\File;

use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Http\UploadedFile;
use Illuminate\Log\Logger;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
use Lucent\Channel\ChannelService;
use Lucent\Database\Database;
use Lucent\LucentException;
use Lucent\Record\FileData as RecordFile;
use Lucent\Record\QueryRecord;
use Lucent\Schema\FilesSchema;
use Lucent\Schema\Schema;
use Spatie\ImageOptimizer\OptimizerChainFactory;

class FileService
{

    public function __construct(
        public ChannelService $channelService,
        public ImageManager   $imageManager,
        public Logger         $logger
    )
    {
    }

    public function getPath(QueryRecord $file): string
    {
        return $this->channelService->channel->filesUrl . "/" . $file->_file->path;
    }

    public function createFromUrl(FilesSchema $schema, string $url): FileUploadResult
    {
        $pathinfo = pathinfo($url);
        $contents = file_get_contents($url);
        if ($contents === false) {
            throw new LucentException("Failed to upload file from url");
        }
        $file = '/tmp/' . $pathinfo['basename'];
        file_put_contents($file, $contents);
        $uploadedFile = new UploadedFile($file, $pathinfo['basename']);
        return $this->upload($schema, $uploadedFile);
    }

    public function upload(FilesSchema $schema, UploadedFile $file): FileUploadResult
    {
        $originalName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
        $extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
        $originalFilename = $file->getClientOriginalName();
        $filename = $this->createFileName($originalName, $extension);
        $mimetype = $file->getMimeType();

        $optimizerChain = OptimizerChainFactory::create();
        $optimizerChain->setTimeout(30)->optimize($file->getPathName());

        $checksum = sha1_file($file);
        $recordId = $this->checkDuplicate($schema->name, $checksum, $file->getSize());

        if (!empty($recordId)) {
            return new FileUploadResult(
                recordFile: null,
                duplicateId: $recordId,
                isDuplicate: true
            );
        }
        $disk = $this->loadDisk($schema);
        $path = $schema->path . "/" . $filename;
        $res = $disk->put(
            $path,
            file_get_contents($file),
//        'public' // now managed by aws policy
        );

        if ($res === false) {
            throw new LucentException("File $filename not uploaded");
        }

        $this->createTemplates($disk, $path, $file);

        list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
        $recordFile = new RecordFile(
            originalName: $originalFilename,
            mime: $mimetype,
            path: $path,
            disk: $schema->disk,
            size: $file->getSize(),
            width: $width,
            height: $height,
            checksum: $checksum
        );

        return new FileUploadResult(
            recordFile: $recordFile,
            duplicateId: "",
            isDuplicate: false
        );
    }

    private function createFileName(string $originalName, string $extension): string
    {
        return Str::slug($originalName, '-') . '-' . uniqid() . '.' . $extension;
    }

    private function isImage(string $mimetype): bool
    {
        $imageMimes = ['image/webp', 'image/gif', 'image/jpeg', 'image/png', 'image/tiff'];
        return in_array($mimetype, $imageMimes);
    }

    public function loadDisk(Schema|string $schema): Filesystem
    {
        return Storage::disk($schema->disk ?? $schema);

    }

    private function checkDuplicate(string $schemaName, string $checksum, int $filesize): string
    {

        $record = Database::make()->table("records")
            ->where("schema", $schemaName)
            ->where("_file->checksum", $checksum)
            ->where("_file->size", $filesize)
            ->first();

        return $record->id ?? "";
    }

    public function createTemplates(Filesystem $disk, string $path): void
    {
        $originalImage = $this->imageManager->make($disk->get($path));
        foreach (config("lucent.imageFilters") as $preset => $filterClass) {
            $image = $originalImage->filter(new $filterClass);
            $templateUri = "/templates/" . $preset . "/" . $path;
            $disk->put($templateUri, $image->encode('webp', 75));
        }

        $thumbDir = "thumbs/" . $path;

        $image = $originalImage->fit(300, 300);
        $disk->put($thumbDir, $image->encode('webp', 75));
    }
}
