<?php

namespace App\Services;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Encoders\AutoEncoder;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\Laravel\Facades\Image;

class ImageService
{
    /**
     * Store a private document — used for CNIC, licence, business proof, selfie etc.
     * Returns the relative path within the given disk.
     */
    public function storePrivate(UploadedFile $file, string $disk, string $folder): string
    {
        $filename = $this->generateFilename($file);
        $path     = trim($folder, '/') . '/' . $filename;

        Storage::disk($disk)->put($path, file_get_contents($file->getRealPath()));

        return $path;
    }

    /**
     * Store a public product image with thumb + webp variants.
     * Returns array with paths.
     */
    public function storeProductImage(UploadedFile $file): array
    {
        $filename = $this->generateFilename($file);
        $base     = pathinfo($filename, PATHINFO_FILENAME);
        $source   = $file->getRealPath();

        // Original (max 800x800, original format preserved via AutoEncoder)
        Storage::disk('public')->put(
            "products/{$filename}",
            (string) Image::decode($source)->scaleDown(800, 800)->encode(new AutoEncoder())
        );

        // Thumbnail (200x200)
        Storage::disk('public')->put(
            "products/thumbs/{$filename}",
            (string) Image::decode($source)->coverDown(200, 200)->encode(new AutoEncoder())
        );

        // WebP variant for web/PWA
        Storage::disk('public')->put(
            "products/webp/{$base}.webp",
            (string) Image::decode($source)->scaleDown(800, 800)->encode(new WebpEncoder(quality: 80))
        );

        return [
            'path'       => "products/{$filename}",
            'path_thumb' => "products/thumbs/{$filename}",
            'path_webp'  => "products/webp/{$base}.webp",
        ];
    }

    /**
     * Store a public avatar / logo / banner.
     */
    public function storePublic(UploadedFile $file, string $folder, int $maxSize = 600): string
    {
        $filename = $this->generateFilename($file);
        $path     = trim($folder, '/') . '/' . $filename;

        Storage::disk('public')->put(
            $path,
            (string) Image::decode($file->getRealPath())->scaleDown($maxSize, $maxSize)->encode(new AutoEncoder())
        );

        return $path;
    }

    private function generateFilename(UploadedFile $file): string
    {
        $ext = $file->getClientOriginalExtension() ?: 'jpg';
        return Str::random(32) . '.' . strtolower($ext);
    }

    public function deletePrivate(string $disk, ?string $path): void
    {
        if (! $path) {
            return;
        }
        Storage::disk($disk)->delete($path);
    }

    public function deletePublic(?string $path): void
    {
        if (! $path) {
            return;
        }
        Storage::disk('public')->delete($path);
    }
}
