<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ServicePageContent;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ServicePageController extends Controller
{
    public function edit()
    {
        $content = ServicePageContent::getContent();

        return view('admin.services.edit', compact('content'));
    }

    public function update(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'seo_title' => 'required|string|max:160',
            'seo_description' => 'required|string|max:320',

            'hero_title' => 'required|string|max:500',
            'hero_highlight' => 'required|string|max:255',
            'hero_text' => 'required|string|max:1000',
            'hero_button' => 'required|string|max:120',
            'hero_image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:5120',

            'offer_title' => 'required|string|max:255',
            'offer_highlight' => 'required|string|max:255',
            'offer_text' => 'required|string|max:1000',

            'final_cta_title' => 'required|string|max:255',
            'final_cta_text' => 'required|string|max:1000',
            'final_cta_button' => 'required|string|max:120',
            'final_cta_image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:5120',
        ]);

        for ($i = 1; $i <= 6; $i++) {
            $request->validate([
                "service_{$i}_title" => 'required|string|max:255',
                "service_{$i}_text" => 'required|string|max:1200',
                "service_{$i}_image" => 'nullable|image|mimes:jpg,jpeg,png,webp|max:5120',
            ]);
        }

        $existing = ServicePageContent::getContent();

        $heroImage = $this->storeImageIfUploaded($request, 'hero_image', $existing['hero']['image'] ?? null);
        $finalImage = $this->storeImageIfUploaded($request, 'final_cta_image', $existing['final_cta']['image'] ?? null);

        $serviceItems = [];
        for ($i = 1; $i <= 6; $i++) {
            $old = $existing['services'][$i - 1] ?? [];
            $serviceItems[] = [
                'title' => (string) $request->input("service_{$i}_title"),
                'text' => (string) $request->input("service_{$i}_text"),
                'image' => $this->storeImageIfUploaded($request, "service_{$i}_image", $old['image'] ?? null),
            ];
        }

        $content = [
            'seo' => [
                'title' => $validated['seo_title'],
                'description' => $validated['seo_description'],
            ],
            'hero' => [
                'title' => $validated['hero_title'],
                'highlight' => $validated['hero_highlight'],
                'text' => $validated['hero_text'],
                'button' => $validated['hero_button'],
                'image' => $heroImage,
            ],
            'offer' => [
                'title' => $validated['offer_title'],
                'highlight' => $validated['offer_highlight'],
                'text' => $validated['offer_text'],
            ],
            'services' => $serviceItems,
            'final_cta' => [
                'title' => $validated['final_cta_title'],
                'text' => $validated['final_cta_text'],
                'button' => $validated['final_cta_button'],
                'image' => $finalImage,
            ],
        ];

        ServicePageContent::saveContent($content);

        return back()->with('success', 'Services page content updated successfully.');
    }

    private function storeImageIfUploaded(Request $request, string $field, ?string $fallback = null): ?string
    {
        if ($request->hasFile($field)) {
            return $request->file($field)->store('services', 'public');
        }

        return $fallback;
    }
}