<?php

namespace App\Http\Controllers\Admin;

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

class ServiceController extends Controller
{
    public function index(): View
    {
        return view('admin.services.index', [
            'services' => Service::query()->orderBy('sort_order')->get(),
        ]);
    }

    public function create(): View
    {
        return view('admin.services.form', ['service' => new Service()]);
    }

    public function store(Request $request): RedirectResponse
    {
        Service::create($this->validated($request));

        return redirect()->route('admin.services.index')->with('status', __('Service created successfully.'));
    }

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

    public function update(Request $request, Service $service): RedirectResponse
    {
        $service->update($this->validated($request, $service));

        return redirect()->route('admin.services.index')->with('status', __('Service updated successfully.'));
    }

    public function destroy(Service $service): RedirectResponse
    {
        if ($service->image_path) {
            Storage::disk('public')->delete($service->image_path);
        }

        $service->delete();

        return back()->with('status', __('Service deleted successfully.'));
    }

    protected function validated(Request $request, ?Service $service = null): array
    {
        $validated = $request->validate([
            'title_en' => ['required', 'string', 'max:255'],
            'title_ar' => ['required', 'string', 'max:255'],
            'excerpt_en' => ['required', 'string', 'max:500'],
            'excerpt_ar' => ['required', 'string', 'max:500'],
            'bullets_en' => ['required', 'string', 'max:1000'],
            'bullets_ar' => ['required', 'string', 'max:1000'],
            'icon' => ['nullable', 'string', 'max:100'],
            'image' => ['nullable', 'image', 'max:4096'],
            'remove_image' => ['nullable', 'boolean'],
            'sort_order' => ['nullable', 'integer'],
            'is_active' => ['nullable', 'boolean'],
        ]);

        $imagePath = $service?->image_path;

        if ($request->hasFile('image')) {
            if ($imagePath) {
                Storage::disk('public')->delete($imagePath);
            }

            $imagePath = $request->file('image')->store('services', 'public');
        } elseif ($request->boolean('remove_image') && $imagePath) {
            Storage::disk('public')->delete($imagePath);
            $imagePath = null;
        }

        return [
            'title' => ['en' => $validated['title_en'], 'ar' => $validated['title_ar']],
            'excerpt' => ['en' => $validated['excerpt_en'], 'ar' => $validated['excerpt_ar']],
            'bullets' => [
                'en' => preg_split('/\r\n|\r|\n/', $validated['bullets_en']) ?: [],
                'ar' => preg_split('/\r\n|\r|\n/', $validated['bullets_ar']) ?: [],
            ],
            'icon' => $validated['icon'] ?? $service?->icon ?? 'sparkles',
            'image_path' => $imagePath,
            'sort_order' => $validated['sort_order'] ?? 0,
            'is_active' => $request->has('is_active')
                ? $request->boolean('is_active')
                : ($service?->is_active ?? true),
        ];
    }
}