<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ServiceAndSolution;

class ServiceSolutionController extends Controller
{

    public function index()
    {
        $services = ServiceAndSolution::orderBy('id', 'desc')->get();
        return sendSuccessResponse($services->map(function ($service) {
            return [
                'id' => $service->id,
                'title' => $service->title,
                'icon' => $service->icon,
                'description' => $service->description
            ];
        }));
    }


    public function servicesSolutions()
    {
        $servicesSolutions = ServiceAndSolution::orderBy('id', 'desc')->get();
        return view('Backend.ServiceSolution.servicesSolutions', [
            'servicesSolutions' => $servicesSolutions
        ]);
    }

    public function store(Request $request)
    {
        // Validate the request
        $validated = $request->validate([
            'title' => 'required|string|min:3|max:100',
            'icon' => 'required|string',
            'description' => 'required|string|min:10|max:500',
        ]);

        try {
            // Create new service solution
            ServiceAndSolution::create($validated);

            // Return with success message
            toastr()->success('Service/Solution added successfully!');
            return redirect()
                ->route('admin.services-solutions');
        } catch (\Exception $e) {
            toastr()->error('Failed to add Service/Solution. Please try again.');
            return redirect()
                ->back()
                ->withInput();
        }
    }

    public function destroy($id)
    {
        try {
            ServiceAndSolution::find($id)->delete();
            toastr()->success('Service/Solution deleted successfully!');
            return redirect()->route('admin.services-solutions');
        } catch (\Exception $e) {
            toastr()->error('Failed to delete Service/Solution. Please try again.');
            return redirect()->back();
        }
    }

    public function edit($id)
    {
        $serviceSolution = ServiceAndSolution::findOrFail($id);
        return response()->json($serviceSolution);
    }

    public function update(Request $request, $id)
    {
        // Validate the request
        $validated = $request->validate([
            'title' => 'required|string|min:3|max:100',
            'icon' => 'required|string',
            'description' => 'required|string|min:10|max:500',
        ]);

        try {
            $serviceSolution = ServiceAndSolution::findOrFail($id);
            $serviceSolution->update($validated);

            toastr()->success('Service/Solution updated successfully!');
            return redirect()->route('admin.services-solutions');
        } catch (\Exception $e) {
            toastr()->error('Failed to update Service/Solution. Please try again.');
            return redirect()->back()->withInput();
        }
    }
}
