<?php

namespace App\Http\Controllers\Admin;

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

class DeliveryManController extends Controller
{
    public function index()
    {
        $items = DeliveryMan::latest()->paginate(10);
        return view('admin.delivery.delivery-men.index', compact('items'));
    }

    public function create()
    {
        return view('admin.delivery.delivery-men.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:delivery_men',
            'phone' => 'required|max:20',
            'address' => 'nullable',
            'status' => 'required|in:pending,active,inactive',
            'vehicle_type' => 'required|max:255',
            'identity_type' => 'required|max:255',
            'identity_number' => 'required|max:255',
            'identity_image' => 'nullable|image|max:2048',
            'profile_image' => 'nullable|image|max:2048'
        ]);

        if ($request->hasFile('identity_image')) {
            $validated['identity_image'] = $request->file('identity_image')->store('delivery-men/identities', 'public');
        }

        if ($request->hasFile('profile_image')) {
            $validated['profile_image'] = $request->file('profile_image')->store('delivery-men/profiles', 'public');
        }

        DeliveryMan::create($validated);

        return redirect()->route('admin.delivery.men.index')
            ->with('success', 'Delivery man created successfully');
    }

    public function edit(DeliveryMan $deliveryMan)
    {
        $item = $deliveryMan;
        return view('admin.delivery.delivery-men.edit', compact('item'));
    }

    public function update(Request $request, DeliveryMan $deliveryMan)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:delivery_men,email,' . $deliveryMan->id,
            'phone' => 'required|max:20',
            'address' => 'nullable',
            'status' => 'required|in:pending,active,inactive',
            'vehicle_type' => 'required|max:255',
            'identity_type' => 'required|max:255',
            'identity_number' => 'required|max:255',
            'identity_image' => 'nullable|image|max:2048',
            'profile_image' => 'nullable|image|max:2048'
        ]);

        if ($request->hasFile('identity_image')) {
            $validated['identity_image'] = $request->file('identity_image')->store('delivery-men/identities', 'public');
        }

        if ($request->hasFile('profile_image')) {
            $validated['profile_image'] = $request->file('profile_image')->store('delivery-men/profiles', 'public');
        }

        $deliveryMan->update($validated);

        return redirect()->route('admin.delivery.men.index')
            ->with('success', 'Delivery man updated successfully');
    }

    public function destroy(DeliveryMan $deliveryMan)
    {
        $deliveryMan->delete();

        return redirect()->route('admin.delivery.men.index')
            ->with('success', 'Delivery man deleted successfully');
    }
}
