<?php

namespace App\Services;

use App\Models\Label;
use App\Models\Song;
use App\Models\SongArrangement;
use App\Models\SongArrangementLabel;
use App\Models\SongSection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class SongService
{
    /**
     * Sicherstellen, dass die Default-Labels und Song-Sektionen existieren.
     *
     * @return Collection<int, SongSection>
     */
    public function createDefaultGroups(Song $song): Collection
    {
        $defaults = [
            ['name' => 'Strophe 1', 'color' => '#3B82F6'],
            ['name' => 'Refrain', 'color' => '#10B981'],
            ['name' => 'Bridge', 'color' => '#F59E0B'],
        ];

        $sections = collect();

        foreach ($defaults as $index => $data) {
            $existing = Label::whereRaw('LOWER(name) = ?', [strtolower($data['name'])])->first();

            if ($existing === null) {
                $existing = Label::create([
                    'name' => $data['name'],
                    'color' => $data['color'],
                ]);
            }

            $section = SongSection::firstOrCreate(
                ['song_id' => $song->id, 'label_id' => $existing->id],
                ['order' => $index + 1],
            );
            $section->update(['order' => $index + 1]);

            $sections->push($section);
        }

        return $sections;
    }

    /**
     * Standard "Normal"-Arrangement mit den Default-Labels erstellen.
     */
    public function createDefaultArrangement(Song $song): SongArrangement
    {
        $arrangement = $song->arrangements()->create([
            'name' => 'Normal',
            'is_default' => true,
        ]);

        $sections = $this->createDefaultGroups($song);

        foreach ($sections->values() as $index => $section) {
            $arrangement->arrangementSections()->create([
                'song_section_id' => $section->id,
                'order' => $index + 1,
            ]);
        }

        return $arrangement->load('arrangementSections.section.label');
    }

    /**
     * Arrangement duplizieren mit neuem Namen.
     */
    public function duplicateArrangement(SongArrangement $arrangement, string $name): SongArrangement
    {
        return DB::transaction(function () use ($arrangement, $name) {
            $clone = $arrangement->replicate(['id', 'created_at', 'updated_at']);
            $clone->name = $name;
            $clone->is_default = false;
            $clone->save();

            foreach ($arrangement->arrangementSections()->orderBy('order')->get() as $arrangementSection) {
                SongArrangementLabel::create([
                    'song_arrangement_id' => $clone->id,
                    'song_section_id' => $arrangementSection->song_section_id,
                    'order' => $arrangementSection->order,
                ]);
            }

            return $clone->load('arrangementSections.section.label');
        });
    }
}
