<?php

namespace App\Http\Controllers\Admin;

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

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

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

    public function store(Request $request)
    {
        $validated = $request->validate([
            'day_of_week' => 'required|in:Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday',
            'start_time' => 'required',
            'end_time' => 'required|after:start_time',
            'max_orders' => 'required|integer|min:1',
            'additional_fee' => 'nullable|numeric|min:0',
            'is_active' => 'boolean',
        ]);

        $validated['is_active'] = $request->has('is_active');

        DeliverySlot::create($validated);

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

    public function edit(DeliverySlot $deliverySlot)
    {
        $item = $deliverySlot;
        return view('admin.delivery.delivery-slots.edit', compact('item'));
    }

    public function update(Request $request, DeliverySlot $deliverySlot)
    {
        $validated = $request->validate([
            'day_of_week' => 'required|in:Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday',
            'start_time' => 'required',
            'end_time' => 'required|after:start_time',
            'max_orders' => 'required|integer|min:1',
            'additional_fee' => 'nullable|numeric|min:0',
            'is_active' => 'boolean',
        ]);

        $validated['is_active'] = $request->has('is_active');

        $deliverySlot->update($validated);

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

    public function destroy(DeliverySlot $deliverySlot)
    {
        $deliverySlot->delete();

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