<?php

namespace App\Http\Controllers;

use App\Models\MacroAssignment;
use App\Models\Service;
use App\Models\ServiceMacroAssignment;
use App\Models\ServiceMacroOverride;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ServiceMacroOverrideController extends Controller
{
    public function store(Request $request, Service $service): JsonResponse
    {
        $validated = $request->validate([
            'part_type' => ['required', 'in:information,moderation,sermon,song,agenda_item'],
        ]);

        ServiceMacroOverride::firstOrCreate([
            'service_id' => $service->id,
            'part_type' => $validated['part_type'],
        ]);

        $globals = MacroAssignment::where('part_type', $validated['part_type'])->orderBy('order')->get();
        foreach ($globals as $global) {
            ServiceMacroAssignment::firstOrCreate([
                'service_id' => $service->id,
                'part_type' => $validated['part_type'],
                'macro_id' => $global->macro_id,
                'position' => $global->position,
                'label_id' => $global->label_id,
                'order' => $global->order,
            ]);
        }

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

    public function destroy(Service $service, Request $request): JsonResponse
    {
        $validated = $request->validate([
            'part_type' => ['required', 'in:information,moderation,sermon,song,agenda_item'],
        ]);

        ServiceMacroOverride::where('service_id', $service->id)
            ->where('part_type', $validated['part_type'])
            ->delete();

        ServiceMacroAssignment::where('service_id', $service->id)
            ->where('part_type', $validated['part_type'])
            ->delete();

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

    public function storeAssignment(Request $request, Service $service): JsonResponse
    {
        $validated = $request->validate([
            'part_type' => ['required', 'in:information,moderation,sermon,song,agenda_item'],
            'macro_id' => ['required', 'integer', 'exists:macros,id'],
            'position' => ['required', 'in:all_slides,first_slide,last_slide,by_label'],
            'label_id' => ['nullable', 'integer', 'exists:labels,id'],
            'order' => ['integer', 'min:0'],
        ]);

        $assignment = ServiceMacroAssignment::create([
            'service_id' => $service->id,
            ...$validated,
        ]);

        return response()->json(['id' => $assignment->id, 'success' => true]);
    }

    public function updateAssignment(Request $request, Service $service, ServiceMacroAssignment $serviceMacroAssignment): JsonResponse
    {
        $validated = $request->validate([
            'position' => ['sometimes', 'in:all_slides,first_slide,last_slide,by_label'],
            'label_id' => ['nullable', 'integer', 'exists:labels,id'],
            'order' => ['sometimes', 'integer', 'min:0'],
        ]);

        $serviceMacroAssignment->update($validated);

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

    public function destroyAssignment(Service $service, ServiceMacroAssignment $serviceMacroAssignment): JsonResponse
    {
        $serviceMacroAssignment->delete();

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