<?php

declare(strict_types=1);

namespace App\Http\Controllers\Customer;

use App\Domain\Account\Models\Account;
use App\Domain\Files\Services\ImageService;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use RuntimeException;

class ImageController extends Controller
{
    public function __construct(private readonly ImageService $service) {}

    public function index(Request $request): Response
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $path = $request->string('path')->value() ?: '';

        try {
            $images = $this->service->listImages($account, $path);
            $error = null;
        } catch (RuntimeException $e) {
            $images = [];
            $error = $e->getMessage();
        }

        return Inertia::render('Customer/Images/Index', [
            'path' => $path,
            'images' => $images,
            'error' => $error,
        ]);
    }

    public function resize(Request $request): RedirectResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $validated = $request->validate([
            'path' => ['required', 'string'],
            'width' => ['required', 'integer', 'min:10', 'max:10000'],
            'height' => ['nullable', 'integer', 'min:10', 'max:10000'],
            'keep_aspect_ratio' => ['boolean'],
        ]);

        try {
            $this->service->resize($account, $validated['path'], $validated['width'], $validated['height'] ?? null, $validated['keep_aspect_ratio'] ?? true);

            return back()->with('success', 'Görüntü yeniden boyutlandırıldı.');
        } catch (RuntimeException $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    public function thumbnail(Request $request): RedirectResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $validated = $request->validate([
            'path' => ['required', 'string'],
            'max_size' => ['integer', 'min:32', 'max:1024'],
        ]);

        try {
            $outputPath = $this->service->thumbnail($account, $validated['path'], $validated['max_size'] ?? 200);

            return back()->with('success', "Thumbnail oluşturuldu: {$outputPath}");
        } catch (RuntimeException $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    public function convert(Request $request): RedirectResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $validated = $request->validate([
            'path' => ['required', 'string'],
            'target_format' => ['required', 'in:jpg,jpeg,png,webp,gif'],
            'quality' => ['integer', 'min:1', 'max:100'],
        ]);

        try {
            $newPath = $this->service->convert($account, $validated['path'], $validated['target_format'], $validated['quality'] ?? 85);

            return back()->with('success', "Dönüştürüldü: {$newPath}");
        } catch (RuntimeException $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    public function rotate(Request $request): RedirectResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $validated = $request->validate([
            'path' => ['required', 'string'],
            'degrees' => ['required', 'integer', 'in:90,180,270'],
        ]);

        try {
            $this->service->rotate($account, $validated['path'], $validated['degrees']);

            return back()->with('success', "{$validated['degrees']}° döndürüldü.");
        } catch (RuntimeException $e) {
            return back()->with('error', $e->getMessage());
        }
    }

    public function imgTag(Request $request): JsonResponse
    {
        /** @var Account $account */
        $account = $request->attributes->get('customer_account');

        $validated = $request->validate([
            'path' => ['required', 'string'],
            'width' => ['required', 'integer', 'min:1'],
            'height' => ['required', 'integer', 'min:1'],
            'alt' => ['nullable', 'string'],
        ]);

        return response()->json([
            'tag' => $this->service->buildImgTag($account, $validated['path'], $validated['width'], $validated['height'], $validated['alt'] ?? null),
        ]);
    }
}
