<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\HomepageFeature;
use App\Models\HomepageSection;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class HomepageContentController extends Controller
{
    public function index(): View
    {
        HomepageSection::seedDefaults();
        HomepageFeature::seedDefaults();

        return view('admin.homepage-content.index', [
            'sections' => HomepageSection::orderBy('sort_order')->get(),
            'features' => HomepageFeature::orderBy('sort_order')->orderBy('title')->get(),
        ]);
    }

    public function updateSections(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'sections' => ['required', 'array'],
            'sections.*.id' => ['required', 'exists:homepage_sections,id'],
            'sections.*.title' => ['nullable', 'string', 'max:255'],
            'sections.*.subtitle' => ['nullable', 'string', 'max:5000'],
            'sections.*.slogan' => ['nullable', 'string', 'max:255'],
            'sections.*.cta_text' => ['nullable', 'string', 'max:255'],
            'sections.*.cta_link' => ['nullable', 'string', 'max:255'],
            'sections.*.content' => ['nullable', 'string', 'max:10000'],
            'sections.*.sort_order' => ['required', 'integer', 'min:0', 'max:9999'],
            'sections.*.is_active' => ['nullable', 'boolean'],
        ]);

        foreach ($data['sections'] as $sectionData) {
            HomepageSection::whereKey($sectionData['id'])->update([
                'title' => $sectionData['title'] ?? null,
                'subtitle' => $sectionData['subtitle'] ?? null,
                'slogan' => $sectionData['slogan'] ?? null,
                'cta_text' => $sectionData['cta_text'] ?? null,
                'cta_link' => $sectionData['cta_link'] ?? null,
                'content' => $sectionData['content'] ?? null,
                'sort_order' => $sectionData['sort_order'],
                'is_active' => (bool) ($sectionData['is_active'] ?? false),
            ]);
        }

        return back()->with('success', 'Homepage sections updated.');
    }

    public function storeFeature(Request $request): RedirectResponse
    {
        HomepageFeature::create($this->featureData($request));

        return back()->with('success', 'Homepage feature created.');
    }

    public function updateFeature(Request $request, HomepageFeature $homepageFeature): RedirectResponse
    {
        $homepageFeature->update($this->featureData($request));

        return back()->with('success', 'Homepage feature updated.');
    }

    public function destroyFeature(HomepageFeature $homepageFeature): RedirectResponse
    {
        $homepageFeature->delete();

        return back()->with('success', 'Homepage feature deleted.');
    }

    private function featureData(Request $request): array
    {
        $data = $request->validate([
            'title' => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string', 'max:5000'],
            'icon' => ['nullable', 'string', 'max:100'],
            'sort_order' => ['required', 'integer', 'min:0', 'max:9999'],
            'is_active' => ['nullable', 'boolean'],
        ]);

        $data['is_active'] = $request->boolean('is_active');

        return $data;
    }
}
