<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\PassengerExtraService;
use App\Models\PassengerExtraServiceItem;
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::with(['items' => function ($q) {
            $q->orderBy('sort_order')->orderBy('id');
        }])->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', 'تم حذف الخدمة');
    }

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

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

        PassengerExtraServiceItem::create($data);

        return redirect()->route('admin.passenger-extra-services.edit', $service->id)
            ->with('success', 'تمت إضافة عنصر جديد للخدمة');
    }

    public function updateItem(Request $request, int $id, int $itemId): RedirectResponse
    {
        $service = PassengerExtraService::findOrFail($id);
        $item = PassengerExtraServiceItem::query()
            ->where('passenger_extra_service_id', $service->id)
            ->findOrFail($itemId);

        $data = $request->validate([
            'title' => 'required|string|max:255',
            'subtitle' => 'nullable|string|max:500',
            'price' => 'required|numeric|min:0',
            'discount_percent' => 'nullable|numeric|min:0|max:100',
            'sort_order' => 'nullable|integer|min:0',
            'is_active' => 'nullable',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:3072',
        ]);
        $data['is_active'] = $request->boolean('is_active');
        $data['sort_order'] = (int) ($data['sort_order'] ?? 0);
        $data['discount_percent'] = (float) ($data['discount_percent'] ?? 0);
        unset($data['image']);

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

        $item->update($data);

        return redirect()->route('admin.passenger-extra-services.edit', $service->id)
            ->with('success', 'تم تحديث عنصر الخدمة');
    }

    public function destroyItem(int $id, int $itemId): RedirectResponse
    {
        $service = PassengerExtraService::findOrFail($id);
        $item = PassengerExtraServiceItem::query()
            ->where('passenger_extra_service_id', $service->id)
            ->findOrFail($itemId);

        if (! empty($item->image_path)) {
            Storage::disk('public')->delete($item->image_path);
        }
        $item->delete();

        return redirect()->route('admin.passenger-extra-services.edit', $service->id)
            ->with('success', 'تم حذف عنصر الخدمة');
    }
}
