<?php

namespace App\Services;

use App\Models\Song;
use App\Models\SongSlide;
use Illuminate\Support\Facades\Http;

class TranslationService
{
    public function fetchFromUrl(string $url): ?string
    {
        try {
            $response = Http::timeout(10)->get($url);

            if ($response->successful()) {
                $html = $response->body();
                $text = strip_tags($html);
                $text = trim($text);

                return $text !== '' ? $text : null;
            }
        } catch (\Exception) {
            // Best-effort: Fehler stillschweigend behandeln
        }

        return null;
    }

    public function importTranslation(Song $song, string $text): void
    {
        $translatedLines = explode("\n", $text);
        $offset = 0;

        $defaultArr = $song->arrangements()
            ->where('is_default', true)
            ->with(['arrangementLabels' => fn ($q) => $q->orderBy('order'), 'arrangementLabels.label.songSlides'])
            ->first();

        if ($defaultArr === null) {
            $this->markAsTranslated($song);

            return;
        }

        foreach ($defaultArr->arrangementLabels->sortBy('order') as $arrangementLabel) {
            $label = $arrangementLabel->label;

            if ($label === null) {
                continue;
            }

            foreach ($label->songSlides->sortBy('order') as $slide) {
                $originalLineCount = count(explode("\n", $slide->text_content ?? ''));
                $chunk = array_slice($translatedLines, $offset, $originalLineCount);
                $offset += $originalLineCount;

                $slide->update([
                    'text_content_translated' => implode("\n", $chunk),
                ]);
            }
        }

        $this->markAsTranslated($song);
    }

    public function markAsTranslated(Song $song): void
    {
        $song->update(['has_translation' => true]);
    }

    public function removeTranslation(Song $song): void
    {
        $labelIds = $song->arrangements()
            ->with('arrangementLabels')
            ->get()
            ->flatMap(fn ($arr) => $arr->arrangementLabels->pluck('label_id'))
            ->unique()
            ->values();

        if ($labelIds->isNotEmpty()) {
            SongSlide::whereIn('label_id', $labelIds)->update([
                'text_content_translated' => null,
            ]);
        }

        $song->update(['has_translation' => false]);
    }
}
