<?php

namespace App\Http\Controllers;

use App\Models\Song;
use App\Services\TranslationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class TranslationController extends Controller
{
    public function __construct(
        private readonly TranslationService $translationService,
    ) {}

    public function page(Song $song): Response
    {
        $song->load([
            'arrangements' => fn ($q) => $q->where('is_default', true),
            'arrangements.arrangementLabels' => fn ($q) => $q->orderBy('order'),
            'arrangements.arrangementLabels.label.songSlides',
        ]);

        $defaultArr = $song->arrangements->firstWhere('is_default', true) ?? $song->arrangements->first();

        $groups = collect();
        if ($defaultArr !== null) {
            $groups = $defaultArr->arrangementLabels
                ->sortBy('order')
                ->values()
                ->map(fn ($al) => [
                    'id' => $al->label?->id,
                    'name' => $al->label?->name,
                    'color' => $al->label?->color,
                    'order' => $al->order,
                    'slides' => $al->label
                        ? $al->label->songSlides
                            ->sortBy('order')
                            ->values()
                            ->map(fn ($slide) => [
                                'id' => $slide->id,
                                'order' => $slide->order,
                                'text_content' => $slide->text_content,
                                'text_content_translated' => $slide->text_content_translated,
                            ])->values()
                        : collect(),
                ]);
        }

        return Inertia::render('Songs/Translate', [
            'song' => [
                'id' => $song->id,
                'title' => $song->title,
                'ccli_id' => $song->ccli_id,
                'has_translation' => $song->has_translation,
                'groups' => $groups,
            ],
        ]);
    }

    public function fetchUrl(Request $request): JsonResponse
    {
        $request->validate([
            'url' => ['required', 'url'],
        ]);

        $text = $this->translationService->fetchFromUrl($request->input('url'));

        if ($text === null) {
            return response()->json([
                'message' => 'Konnte Text nicht abrufen',
            ], 422);
        }

        return response()->json([
            'text' => $text,
        ]);
    }

    public function import(int $songId, Request $request): JsonResponse
    {
        $song = Song::find($songId);

        if (! $song) {
            return response()->json([
                'message' => 'Song nicht gefunden',
            ], 404);
        }

        $request->validate([
            'text' => ['required', 'string'],
        ]);

        $this->translationService->importTranslation($song, $request->input('text'));

        return response()->json([
            'message' => 'Übersetzung erfolgreich importiert',
        ]);
    }

    public function destroy(int $songId): JsonResponse
    {
        $song = Song::find($songId);

        if (! $song) {
            return response()->json([
                'message' => 'Song nicht gefunden',
            ], 404);
        }

        $this->translationService->removeTranslation($song);

        return response()->json([
            'message' => 'Übersetzung entfernt',
        ]);
    }
}
