<?php

namespace App\Http\Controllers;

use App\Models\ExportProFile;
use App\Models\Label;
use App\Models\Macro;
use App\Models\MacroAssignment;
use App\Models\MacroCollection;
use App\Models\Setting;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Inertia\Inertia;
use Inertia\Response;

class SettingsController extends Controller
{
    private const AGENDA_KEYS = [
        'agenda_start_title',
        'agenda_end_title',
        'agenda_announcement_position',
        'agenda_sermon_matching',
        'default_translation_language',
        'current_key_visual',
        'current_background',
        'namenseinblender_macro_name',
        'namenseinblender_macro_uuid',
        'namenseinblender_macro_collection_name',
        'namenseinblender_macro_collection_uuid',
        'namenseinblender_macro_id',
        'song_prefix_label_id',
        'song_postfix_label_id',
        'add_countdown_slide',
    ];

    public function index(): Response
    {
        $copyright = Label::firstOrCreate(['name' => 'COPYRIGHT'], ['color' => '#24B34C']);
        $blank = Label::firstOrCreate(['name' => 'BLANK'], ['color' => '#000000']);

        if (Setting::get('song_prefix_label_id') === null) {
            Setting::set('song_prefix_label_id', (string) $copyright->id);
        }

        if (Setting::get('song_postfix_label_id') === null) {
            Setting::set('song_postfix_label_id', (string) $blank->id);
        }

        $settings = [];
        foreach (self::AGENDA_KEYS as $key) {
            $settings[$key] = Setting::get($key);
        }

        return Inertia::render('Settings', [
            'settings' => $settings,
            'assignments' => MacroAssignment::with(['macro', 'label'])->orderBy('part_type')->orderBy('order')->get(),
            'macros' => Macro::with('collections')->orderBy('name')->get(),
            'labels' => Label::orderBy('name')->get(),
            'collections' => MacroCollection::with('macros')->orderBy('name')->get(),
            'last_macros_import' => [
                'at' => Setting::get('macros_last_imported_at'),
                'filename' => Setting::get('macros_last_imported_filename'),
            ],
            'last_labels_import' => [
                'at' => Setting::get('labels_last_imported_at'),
                'filename' => Setting::get('labels_last_imported_filename'),
            ],
            'export_pro_files' => [
                'prefix' => ExportProFile::prefix()->orderBy('order')->get(['id', 'original_name', 'order']),
                'postfix' => ExportProFile::postfix()->orderBy('order')->get(['id', 'original_name', 'order']),
                'keyword' => ExportProFile::keyword()->orderBy('order')->get(['id', 'original_name', 'order', 'keyword']),
            ],
        ]);
    }

    public function update(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'key' => ['required', 'string', Rule::in(self::AGENDA_KEYS)],
            'value' => ['nullable', 'string', 'max:500'],
        ]);

        if ($validated['key'] === 'default_translation_language') {
            validator($validated, [
                'value' => ['nullable', Rule::in(['DE', 'EN', 'FR', 'ES', 'NL', 'IT'])],
            ])->validate();
        }

        Setting::set($validated['key'], $validated['value']);

        return response()->json(['success' => true]);
    }
}
