<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LandingFeature;
use App\Models\GeneralSetting;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class LandingFeatureController extends Controller
{
    public function index()
    {
        $features       = LandingFeature::orderBy('sort_order')->orderBy('id', 'ASC')->get();
        $generalsetting = GeneralSetting::where('status', 1)->first();

        return view('backEnd.landing.features.index', compact('features', 'generalsetting'));
    }

    public function create()
    {
        return view('backEnd.landing.features.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title'       => 'required|string|max:255',
            'description' => 'nullable|string|max:500',
            'icon'        => 'nullable|string|max:100',
            'sort_order'  => 'nullable|integer|min:0',
            'status'      => 'nullable|boolean',
        ]);

        LandingFeature::create([
            'title'       => $request->title,
            'description' => $request->description,
            'icon'        => $request->icon,
            'sort_order'  => $request->sort_order ?? 0,
            'status'      => $request->boolean('status', true),
        ]);

        Cache::forget('landing_features');
        Toastr::success('Success', 'Feature created successfully');

        return redirect()->route('admin.landing-features.index');
    }

    public function edit($id)
    {
        $feature = LandingFeature::findOrFail($id);

        return view('backEnd.landing.features.edit', compact('feature'));
    }

    public function update(Request $request, $id)
    {
        $feature = LandingFeature::findOrFail($id);

        $request->validate([
            'title'       => 'required|string|max:255',
            'description' => 'nullable|string|max:500',
            'icon'        => 'nullable|string|max:100',
            'sort_order'  => 'nullable|integer|min:0',
            'status'      => 'nullable|boolean',
        ]);

        $feature->title       = $request->title;
        $feature->description = $request->description;
        $feature->icon        = $request->icon;
        $feature->sort_order  = $request->sort_order ?? 0;
        $feature->status      = $request->boolean('status', true);
        $feature->save();

        Cache::forget('landing_features');
        Toastr::success('Success', 'Feature updated successfully');

        return redirect()->route('admin.landing-features.index');
    }

    public function destroy(Request $request)
    {
        LandingFeature::findOrFail($request->hidden_id)->delete();

        Cache::forget('landing_features');
        Toastr::success('Success', 'Feature deleted successfully');

        return redirect()->back();
    }

    public function updateSectionTitle(Request $request)
    {
        $request->validate([
            'features_section_title' => 'required|string|max:255',
        ]);

        GeneralSetting::where('status', 1)->update([
            'features_section_title' => $request->features_section_title,
        ]);

        Cache::forget('generalsetting_active');
        Cache::forget('general_setting');
        \Artisan::call('cache:clear');
        Toastr::success('Success', 'Section title updated');

        return redirect()->back();
    }
}
