<?php

namespace App\Services;

use App\Models\Setting;
use App\Models\Song;
use ProPresenter\Parser\ProFileGenerator;

class ProExportService
{
    public function generateProFile(Song $song): string
    {
        $tempPath = sys_get_temp_dir().'/'.uniqid('pro-export-').'.pro';

        ProFileGenerator::generateAndWrite(
            $tempPath,
            $song->title,
            $this->buildGroups($song),
            $this->buildArrangements($song),
            $this->buildCcliMetadata($song),
        );

        return $tempPath;
    }

    public function generateParserSong(Song $song): \ProPresenter\Parser\Song
    {
        $song->loadMissing(['groups.slides', 'arrangements.arrangementGroups.group']);

        return ProFileGenerator::generate(
            $song->title,
            $this->buildGroups($song),
            $this->buildArrangements($song),
            $this->buildCcliMetadata($song),
        );
    }

    private function buildGroups(Song $song): array
    {
        $groups = [];
        $macroData = $this->buildMacroData();

        foreach ($song->groups->sortBy('order') as $group) {
            $slides = [];
            $isCopyrightGroup = strcasecmp($group->name, 'COPYRIGHT') === 0;

            foreach ($group->slides->sortBy('order') as $slide) {
                $slideData = ['text' => $slide->text_content ?? ''];

                if ($slide->text_content_translated) {
                    $slideData['translation'] = $slide->text_content_translated;
                }

                if ($isCopyrightGroup && $macroData) {
                    $slideData['macro'] = $macroData;
                }

                $slides[] = $slideData;
            }

            $groups[] = [
                'name' => $group->name,
                'color' => ProImportService::hexToRgba($group->color),
                'slides' => $slides,
            ];
        }

        return $groups;
    }

    private function buildMacroData(): ?array
    {
        $name = Setting::get('macro_name');
        $uuid = Setting::get('macro_uuid');

        if (! $name || ! $uuid) {
            return null;
        }

        return [
            'name' => $name,
            'uuid' => $uuid,
            'collectionName' => Setting::get('macro_collection_name', '--MAIN--'),
            'collectionUuid' => Setting::get('macro_collection_uuid', '8D02FC57-83F8-4042-9B90-81C229728426'),
        ];
    }

    private function buildArrangements(Song $song): array
    {
        $arrangements = [];
        $groupIdToName = $song->groups->pluck('name', 'id')->toArray();

        foreach ($song->arrangements as $arrangement) {
            $groupNames = $arrangement->arrangementGroups
                ->sortBy('order')
                ->map(fn ($ag) => $groupIdToName[$ag->song_group_id] ?? null)
                ->filter()
                ->values()
                ->toArray();

            $arrangements[] = [
                'name' => $arrangement->name,
                'groupNames' => $groupNames,
            ];
        }

        return $arrangements;
    }

    private function buildCcliMetadata(Song $song): array
    {
        return array_filter([
            'author' => $song->author,
            'song_title' => $song->title,
            'copyright_year' => $song->copyright_year,
            'publisher' => $song->publisher,
            'song_number' => $song->ccli_id ? (int) $song->ccli_id : null,
        ]);
    }
}
