<?php

namespace App\Http\Controllers\Admin;

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

class PassengerExtraServiceController extends Controller
{
    public function index(): View
    {
        $services = PassengerExtraService::query()->orderBy('sort_order')->orderByDesc('id')->paginate(30);

        return view('admin.passenger_extra_services.index', compact('services'));
    }

    public function create(): View
    {
        return view('admin.passenger_extra_services.create');
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'title' => 'required|string|max:255',
            'subtitle' => 'nullable|string|max:500',
            'description' => 'nullable|string|max:10000',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:3072',
            'price_estimate' => 'nullable|numeric|min:0',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'nullable',
        ]);
        $data['is_active'] = $request->boolean('is_active');
        $data['sort_order'] = (int) ($data['sort_order'] ?? 0);
        unset($data['image']);

        if ($request->hasFile('image')) {
            $data['image_path'] = $request->file('image')->store('passenger_extra_services', 'public');
        }

        PassengerExtraService::create($data);

        return redirect()->route('admin.passenger-extra-services.index')
            ->with('success', 'تمت إضافة الخدمة');
    }

    public function edit(int $id): View
    {
        $service = PassengerExtraService::findOrFail($id);

        return view('admin.passenger_extra_services.edit', compact('service'));
    }

    public function update(Request $request, int $id): RedirectResponse
    {
        $service = PassengerExtraService::findOrFail($id);
        $data = $request->validate([
            'title' => 'required|string|max:255',
            'subtitle' => 'nullable|string|max:500',
            'description' => 'nullable|string|max:10000',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:3072',
            'price_estimate' => 'nullable|numeric|min:0',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'nullable',
        ]);
        $data['is_active'] = $request->boolean('is_active');
        $data['sort_order'] = (int) ($data['sort_order'] ?? 0);
        unset($data['image']);

        if ($request->hasFile('image')) {
            if (! empty($service->image_path)) {
                Storage::disk('public')->delete($service->image_path);
            }
            $data['image_path'] = $request->file('image')->store('passenger_extra_services', 'public');
        }

        $service->update($data);

        return redirect()->route('admin.passenger-extra-services.index')
            ->with('success', 'تم تحديث الخدمة');
    }

    public function destroy(int $id): RedirectResponse
    {
        $service = PassengerExtraService::findOrFail($id);
        if (! empty($service->image_path)) {
            Storage::disk('public')->delete($service->image_path);
        }
        $service->delete();

        return redirect()->route('admin.passenger-extra-services.index')
            ->with('success', 'تم حذف الخدمة');
    }
}
