<?php

namespace App\Http\Controllers\Admin\Settings\FrontendSection;

use App\Http\Controllers\Controller;
use App\Models\FrontendSection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class FrontendSectionController extends Controller
{
    public function index()
    {
        adminUserHasPermission(permission: 'edit');

        $buttons = [
            [
                'label' => __('Add Section'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'link',
                'link' => route('admin.settings.frontend-sections.create'),
            ],
        ];

        $search = request()->query('search', '');

        $sections = FrontendSection::when($search, function ($query) use ($search) {
            $query->where('title', 'like', '%'.$search.'%');
        })->get();

        return view('admin.pages.settings.manage-pages-sections.index', compact('sections', 'buttons'));
    }

    public function create()
    {
        adminUserHasPermission(permission: 'create');

        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'ph ph-arrow-left',
                'type' => 'link',
                'link' => route('admin.settings.frontend-sections.index'),
            ],
        ];

        return view('admin.pages.settings.manage-pages-sections.create', compact('buttons'));
    }

    public function store(Request $request)
    {
        adminUserHasPermission(permission: 'create');

        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'icon' => 'required|string|max:255',
            'content' => 'required',
        ]);

        FrontendSection::create($validated);

        return response()->json([
            'message' => __('Frontend section created.'),
        ]);
    }

    public function edit(FrontendSection $frontendSection)
    {
        $buttons = [
            [
                'label' => __('Back'),
                'icon' => 'ph ph-arrow-left',
                'type' => 'link',
                'link' => route('admin.settings.frontend-sections.index'),
            ],
        ];

        $has_table_slugs = ['teams', 'testimonials', 'faqs'];

        if (in_array($frontendSection->slug, $has_table_slugs)) {
            $buttons[] = [
                'label' => __(ucfirst($frontendSection->slug)),
                'icon' => 'ph ph-list',
                'type' => 'link',
                'link' => route("admin.settings.{$frontendSection->slug}.index"),
            ];
        }

        return view('admin.pages.settings.manage-pages-sections.sections.regular', compact('frontendSection', 'buttons'));
    }

    public function update(Request $request, FrontendSection $frontendSection)
    {
        $request->validate([
            'content' => 'required',
        ]);

        $frontendSection->update([
            'content' => $request->content,
        ]);

        Cache::forget('pages_list');

        return response()->json([
            'message' => __('Frontend section updated.'),
        ]);
    }
}
