<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Service;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;

class ServiceController extends Controller
{
    public function index()
    {
        $services = Service::orderBy('order_index')->get();
        return view('admin.services.index', compact('services'));
    }

    public function create()
    {
        return view('admin.services.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'slug' => 'nullable|string|unique:services,slug',
            'description' => 'required|string',
            'overview' => 'nullable|string',
            'what_to_expect' => 'nullable|string',
            'recovery' => 'nullable|string',
            'image_upload' => 'nullable|image|max:5120',
            'image' => 'nullable|string',
            'order_index' => 'integer',
            'faqs' => 'nullable|array',
            'faqs.*.question' => 'required|string',
            'faqs.*.answer' => 'required|string',
        ]);

        if ($request->hasFile('image_upload')) {
            $path = $request->file('image_upload')->store('services', 'public');
            $validated['image'] = '/storage/' . $path;
        }
        unset($validated['image_upload']);

        if (empty($validated['slug'])) {
            $validated['slug'] = Str::slug($validated['title']);
        }

        Service::create($validated);
        return redirect()->route('admin.services.index')->with('success', 'Service created successfully.');
    }

    public function edit(Service $service)
    {
        return view('admin.services.edit', compact('service'));
    }

    public function update(Request $request, Service $service)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'slug' => 'required|string|unique:services,slug,' . $service->id,
            'description' => 'required|string',
            'overview' => 'nullable|string',
            'what_to_expect' => 'nullable|string',
            'recovery' => 'nullable|string',
            'image_upload' => 'nullable|image|max:5120',
            'image' => 'nullable|string',
            'order_index' => 'integer',
            'faqs' => 'nullable|array',
            'faqs.*.question' => 'required|string',
            'faqs.*.answer' => 'required|string',
        ]);

        if ($request->hasFile('image_upload')) {
            if ($service->image && str_starts_with($service->image, '/storage/')) {
                $oldPath = str_replace('/storage/', '', $service->image);
                Storage::disk('public')->delete($oldPath);
            }
            $path = $request->file('image_upload')->store('services', 'public');
            $validated['image'] = '/storage/' . $path;
        }
        unset($validated['image_upload']);

        $service->update($validated);
        return redirect()->route('admin.services.index')->with('success', 'Service updated successfully.');
    }

    public function destroy(Service $service)
    {
        if ($service->image && str_starts_with($service->image, '/storage/')) {
            $oldPath = str_replace('/storage/', '', $service->image);
            Storage::disk('public')->delete($oldPath);
        }
        $service->delete();
        return redirect()->route('admin.services.index')->with('success', 'Service deleted successfully.');
    }
}
