<?php

namespace App\Http\Controllers\Api;

use App\Api\Plugins\PluginLoader;
use App\Http\Controllers\Controller;
use App\Models\ApiProvider;
use App\Models\Gallery;
use App\Models\Image;
use App\Models\Style;
use App\Settings\GeneralSettings;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;

class ImageController extends Controller
{
    public function __construct(private GeneralSettings $settings) {}

    public function index(Request $request)
    {
        $gallery = $this->resolveGallery($request);

        if (! $gallery) {
            return response()->json(['error' => 'Gallery not found.'], 404);
        }

        $imagesPath = trim($gallery->images_path, '/');
        $publicUploadsPath = public_path('storage/'.$imagesPath);

        // Ensure the directory exists
        if (! File::exists($publicUploadsPath)) {
            File::makeDirectory($publicUploadsPath, 0755, true);
        }

        // Get files from the gallery-specific directory
        $diskFiles = File::files($publicUploadsPath);
        $diskImagePaths = [];
        foreach ($diskFiles as $file) {
            // Store path relative to public/storage/
            $diskImagePaths[] = $imagesPath.'/'.$file->getFilename();
        }

        $dbImagePaths = Image::where('gallery_id', $gallery->id)->pluck('path')->toArray();

        // Add images from disk that are not in the database
        $imagesToAdd = array_diff($diskImagePaths, $dbImagePaths);
        foreach ($imagesToAdd as $path) {
            Image::create([
                'gallery_id' => $gallery->id,
                'path' => $path,
                'is_public' => true,
            ]);
        }

        // Remove images from database that are not on disk
        $imagesToRemove = array_diff($dbImagePaths, $diskImagePaths);
        Image::where('gallery_id', $gallery->id)
            ->whereIn('path', $imagesToRemove)
            ->delete();

        // Fetch images from the database after synchronization
        $query = Image::where('gallery_id', $gallery->id)->orderBy('updated_at', 'desc');

        // If user is not authenticated, filter by is_public, but also include their temporary images
        if (! auth()->check()) {
            $query->where(function ($q) {
                $q->where('is_public', true)->orWhere('is_temp', true);
            });
        } else {
            // If user is authenticated, show all their images
        }

        $newImageTimespanMinutes = $this->settings->new_image_timespan_minutes;

        $images = $query->get()->map(function ($image) use ($newImageTimespanMinutes) {
            $image->is_new = Carbon::parse($image->created_at)->diffInMinutes(Carbon::now()) <= $newImageTimespanMinutes;

            return $image;
        });

        $formattedImages = [];
        foreach ($images as $image) {
            $formattedImages[] = [
                'image_id' => $image->id,
                'path' => asset('storage/'.$image->path),
                'name' => basename($image->path),
                'is_temp' => (bool) $image->is_temp,
                'is_public' => (bool) $image->is_public,
                'is_new' => (bool) $image->is_new,
            ];
        }

        return response()->json($formattedImages);
    }

    public function upload(Request $request)
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,bmp,gif,webp|max:10240', // Max 10MB
            'gallery' => 'required|string|exists:galleries,slug',
        ]);

        $gallery = Gallery::where('slug', $request->string('gallery'))->firstOrFail();
        $imagesPath = trim($gallery->images_path, '/');

        $file = $request->file('image');
        $fileName = uniqid().'.'.$file->getClientOriginalExtension();
        $destinationPath = public_path('storage/'.$imagesPath);

        // Ensure the directory exists
        if (! File::exists($destinationPath)) {
            File::makeDirectory($destinationPath, 0755, true);
        }

        $file->move($destinationPath, $fileName);
        $relativePath = $imagesPath.'/'.$fileName; // Path relative to public/storage/

        $image = Image::create([
            'gallery_id' => $gallery->id,
            'path' => $relativePath,
            'is_public' => true,
        ]);

        return response()->json([
            'message' => __('api.image_uploaded_successfully'),
            'image_id' => $image->id,
            'path' => asset('storage/'.$relativePath),
        ]);
    }

    public function styleChangeRequest(Request $request)
    {
        // Log the incoming request for debugging
        \Illuminate\Support\Facades\Log::info('styleChangeRequest called', [
            'image_id' => $request->image_id,
            'style_id' => $request->style_id,
            'all_params' => $request->all(),
        ]);

        // Same-origin check
        $appUrl = config('app.url');
        $referer = $request->headers->get('referer');

        if ($referer && parse_url($referer, PHP_URL_HOST) !== parse_url($appUrl, PHP_URL_HOST)) {
            \Illuminate\Support\Facades\Log::warning('Unauthorized styleChangeRequest', [
                'referer' => $referer,
                'app_url' => $appUrl,
            ]);

            return response()->json(['error' => 'Unauthorized: Request must originate from the same domain.'], 403);
        }

        $request->validate([
            'image_id' => 'required|exists:images,id',
            'style_id' => 'nullable|exists:styles,id',
            'gallery' => 'required|string|exists:galleries,slug',
        ]);

        $gallery = $this->resolveGallery($request);

        $image = Image::query()
            ->where('id', $request->integer('image_id'))
            ->where('gallery_id', optional($gallery)->id)
            ->first();

        if (! $image) {
            return response()->json(['error' => __('api.image_not_found')], 404);
        }
        $style = null;

        if ($request->style_id) {
            $style = Style::with(['aiModel' => function ($query) {
                $query->where('enabled', true)->with('primaryApiProvider');
            }])->find($request->style_id);
        } else {
            // Attempt to get default style from settings
            $defaultStyleId = $this->settings->default_style_id;
            if ($defaultStyleId) {
                $style = Style::with(['aiModel' => function ($query) {
                    $query->where('enabled', true)->with('primaryApiProvider');
                }])->find($defaultStyleId);
            }
        }

        if (! $style || ! $style->aiModel || ! $style->aiModel->primaryApiProvider) {
            \Illuminate\Support\Facades\Log::warning('Style or provider not found', [
                'style' => $style ? $style->toArray() : null,
                'ai_model' => $style && $style->aiModel ? $style->aiModel->toArray() : null,
            ]);

            return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
        }

        try {
            // Use the primary API provider for this AI model
            $apiProvider = $style->aiModel->primaryApiProvider;
            if (! $apiProvider) {
                \Illuminate\Support\Facades\Log::error('No API provider found for style', [
                    'style_id' => $style->id,
                    'ai_model_id' => $style->aiModel->id,
                ]);

                return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
            }

            \Illuminate\Support\Facades\Log::info('Selected API provider for style change', [
                'api_provider_id' => $apiProvider->id,
                'api_provider_name' => $apiProvider->name,
                'plugin' => $apiProvider->plugin,
            ]);

            $plugin = PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);

            $result = $plugin->processImageStyleChange($image, $style);

            // Update the image model with the ComfyUI prompt_id and style_id
            $image->comfyui_prompt_id = $result['prompt_id'];
            $image->style_id = $style->id;
            $image->save();

            // Return the prompt_id for WebSocket tracking
            \Illuminate\Support\Facades\Log::info('Style change request completed', [
                'prompt_id' => $result['prompt_id'],
                'image_uuid' => $image->uuid,
            ]);

            return response()->json([
                'message' => 'Style change request sent.',
                'prompt_id' => $result['prompt_id'],
                'image_uuid' => $image->uuid, // Pass image UUID for frontend tracking
                'plugin' => $apiProvider->plugin, // Pass plugin name for frontend handling
            ]);
        } catch (\Exception $e) {
            \Illuminate\Support\Facades\Log::error('Error in styleChangeRequest', [
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
            ]);

            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    public function keepImage(Request $request)
    {
        $request->validate([
            'image_id' => 'required|exists:images,id',
            'gallery' => 'required|string|exists:galleries,slug',
        ]);

        $gallery = $this->resolveGallery($request);
        $image = Image::where('id', $request->integer('image_id'))
            ->where('gallery_id', optional($gallery)->id)
            ->first();

        if (! $image) {
            return response()->json(['error' => __('api.image_not_found')], 404);
        }

        $image->is_temp = false;
        $image->save();

        return response()->json(['message' => __('api.image_kept_successfully')]);
    }

    public function deleteImage(Image $image)
    {
        request()->validate([
            'gallery' => 'required|string|exists:galleries,slug',
        ]);

        $gallery = $this->resolveGallery(request());

        if ($gallery && (int) $image->gallery_id !== (int) $gallery->id) {
            return response()->json(['error' => __('api.image_not_found')], 404);
        }

        try {
            // Delete from the public/storage directory
            File::delete(public_path('storage/'.$image->path));
            $image->delete();

            return response()->json(['message' => __('api.image_deleted_successfully')]);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    public function deleteStyled(Request $request, Image $image)
    {
        $request->validate([
            'gallery' => 'required|string|exists:galleries,slug',
        ]);

        $gallery = $this->resolveGallery($request);

        if ($gallery && (int) $image->gallery_id !== (int) $gallery->id) {
            return response()->json(['error' => __('api.image_not_found')], 404);
        }

        if (! $image->is_temp) {
            return response()->json(['error' => __('api.image_not_found')], 404);
        }

        try {
            File::delete(public_path('storage/'.$image->path));
            $image->delete();

            return response()->json(['message' => __('api.image_deleted_successfully')]);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    public function getStatus(Request $request)
    {
        $request->validate([
            'image_id' => 'required|exists:images,id',
            'api_provider_name' => 'required|string',
            'gallery' => 'sometimes|string|exists:galleries,slug',
        ]);

        $gallery = $this->resolveGallery($request);

        $image = Image::query()
            ->where('id', $request->integer('image_id'))
            ->when($gallery, fn ($q) => $q->where('gallery_id', $gallery->id))
            ->first();
        $apiProvider = ApiProvider::where('name', $request->api_provider_name)->first();

        if (! $image || ! $apiProvider) {
            return response()->json(['error' => __('api.image_or_provider_not_found')], 404);
        }

        try {
            $plugin = PluginLoader::getPlugin($apiProvider->name);
            $status = $plugin->getStatus($image->uuid); // Annahme: Image Model hat eine UUID

            return response()->json($status);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    public function fetchStyledImage(Request $request, string $promptId)
    {
        $request->validate([
            'gallery' => 'required|string|exists:galleries,slug',
        ]);

        $gallery = $this->resolveGallery($request);

        Log::info('fetchStyledImage called.', ['prompt_id' => $promptId]);
        try {
            // Find the image associated with the prompt_id, eagerly loading relationships
            $imageQuery = Image::with(['style.aiModel' => function ($query) {
                $query->with('primaryApiProvider');
            }])->where('comfyui_prompt_id', $promptId);

            if ($gallery) {
                $imageQuery->where('gallery_id', $gallery->id);
            }

            $image = $imageQuery->first();

            if (! $image) {
                Log::warning('fetchStyledImage: Image not found for prompt_id.', ['prompt_id' => $promptId]);

                return response()->json(['error' => __('api.image_not_found')], 404);
            }

            Log::info('fetchStyledImage: Image found.', ['image_id' => $image->id, 'image_uuid' => $image->uuid, 'comfyui_prompt_id' => $image->comfyui_prompt_id]);

            // Get the style and API provider associated with the image
            $style = $image->style;
            if (! $style) {
                Log::warning('fetchStyledImage: Style not found for image.', ['image_id' => $image->id]);

                return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
            }
            Log::info('fetchStyledImage: Style found.', ['style_id' => $style->id, 'style_name' => $style->title]);

            if (! $style->aiModel) {
                Log::warning('fetchStyledImage: AI Model not found for style.', ['style_id' => $style->id]);

                return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
            }
            Log::info('fetchStyledImage: AI Model found.', ['ai_model_id' => $style->aiModel->id, 'ai_model_name' => $style->aiModel->name]);

            // Use the primary API provider for this AI model
            $apiProvider = $style->aiModel->primaryApiProvider;
            if (! $apiProvider) {
                Log::warning('fetchStyledImage: No API Provider found for AI Model.', ['ai_model_id' => $style->aiModel->id]);

                return response()->json(['error' => __('api.style_or_provider_not_found')], 404);
            }
            Log::info('fetchStyledImage: API Provider found.', ['api_provider_id' => $apiProvider->id, 'api_provider_name' => $apiProvider->name]);

            Log::info('Fetching base64 image from plugin.', ['prompt_id' => $promptId, 'api_provider' => $apiProvider->name]);
            // Use the plugin to get the final image data (e.g., from ComfyUI's history/view)
            $plugin = PluginLoader::getPlugin($apiProvider->plugin, $apiProvider);
            $base64Image = $plugin->getStyledImage($promptId); // Use the new method

            if (empty($base64Image)) {
                Log::error('Received empty base64 image from plugin.', ['prompt_id' => $promptId]);

                return response()->json(['error' => 'Received empty image data.'], 500);
            }

            Log::info('Base64 image received. Decoding and saving.');
            $decodedImage = base64_decode(preg_replace('#^data:image/\w+;base64, #i', '', $base64Image));

            $newImageName = 'styled_'.uniqid().'.png';
            $galleryPath = trim($image->gallery?->images_path ?: 'uploads', '/');
            $newImagePathRelative = $galleryPath.'/'.$newImageName;
            $newImageFullPath = public_path('storage/'.$newImagePathRelative);

            if (! File::exists(public_path('storage/'.$galleryPath))) {
                File::makeDirectory(public_path('storage/'.$galleryPath), 0755, true);
                Log::info('Created uploads directory.', ['path' => public_path('storage/'.$galleryPath)]);
            }

            File::put($newImageFullPath, $decodedImage); // Save using File facade
            Log::info('Image saved to disk.', ['path' => $newImageFullPath]);

            $newImage = Image::create([
                'gallery_id' => $image->gallery_id,
                'path' => $newImagePathRelative, // Store relative path
                'original_image_id' => $image->id,
                'style_id' => $style->id,
                'is_temp' => true,
            ]);

            Log::info('New image record created in database.', ['image_id' => $newImage->id, 'path' => $newImage->path]);

            return response()->json([
                'message' => 'Styled image fetched successfully',
                'styled_image' => [
                    'id' => $newImage->id,
                    'path' => asset('storage/'.$newImage->path),
                    'is_temp' => $newImage->is_temp,
                ],
            ]);
        } catch (\Exception $e) {
            Log::error('Error in fetchStyledImage: '.$e->getMessage(), ['exception' => $e]);

            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    public function getComfyUiUrl(Request $request)
    {
        $styleId = $request->query('style_id');
        $imageUuid = $request->query('image_uuid');

        $apiProvider = null;

        // If style_id is provided, get the API provider for that style
        if ($styleId) {
            $style = Style::with(['aiModel' => function ($query) {
                $query->where('enabled', true)->with('primaryApiProvider');
            }])->find($styleId);

            if ($style && $style->aiModel) {
                // Use the primary API provider for this AI model
                $apiProvider = $style->aiModel->primaryApiProvider;
            }
        }
        // If image_uuid is provided, get the API provider for that image's style
        elseif ($imageUuid) {
            $image = Image::with(['style.aiModel' => function ($query) {
                $query->with('primaryApiProvider');
            }])->where('uuid', $imageUuid)->first();

            if ($image && $image->style && $image->style->aiModel) {
                // Use the primary API provider for this AI model
                $apiProvider = $image->style->aiModel->primaryApiProvider;
            }
        }
        // Fallback to the old behavior if no style_id or image_uuid is provided
        else {
            // Try to get a default style if none is provided
            $defaultStyleId = $this->settings->default_style_id;
            if ($defaultStyleId) {
                $style = Style::with(['aiModel' => function ($query) {
                    $query->where('enabled', true)->with('primaryApiProvider');
                }])->find($defaultStyleId);

                if ($style && $style->aiModel) {
                    // Use the primary API provider for this AI model
                    $apiProvider = $style->aiModel->primaryApiProvider;
                }
            }

            // If still no API provider, use the first available ComfyUI provider
            if (! $apiProvider) {
                $apiProvider = ApiProvider::where('plugin', 'ComfyUi')->where('enabled', true)->first();
            }
        }

        if (! $apiProvider) {
            return response()->json(['error' => 'No enabled ComfyUI API provider found.'], 404);
        }

        return response()->json(['comfyui_url' => rtrim($apiProvider->api_url, '/')]);
    }

    private function resolveGallery(Request $request): ?Gallery
    {
        $routeGallery = $request->route('gallery');

        if ($routeGallery instanceof Gallery) {
            return $routeGallery;
        }

        $slug = $routeGallery;

        if (! $slug) {
            $slug = $request->query('gallery');
        }

        if (! $slug) {
            return Gallery::first();
        }

        return Gallery::where('slug', $slug)->first();
    }
}
