<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LocalDeliveryZone;
use App\Models\LocalDeliveryPoint;
use App\Models\GeneralSetting;
use Illuminate\Http\Request;
use Brian2694\Toastr\Facades\Toastr;

class LocalDeliveryController extends Controller
{
    public function index()
    {
        $zones = LocalDeliveryZone::withCount('points')->latest()->get();
        $points = LocalDeliveryPoint::with('zone')->latest()->get();
        $setting = GeneralSetting::first();
        
        return view('backEnd.local_delivery.index', compact('zones', 'points', 'setting'));
    }

    public function toggleSetting(Request $request)
    {
        $setting = GeneralSetting::first();
        if (!$setting) {
            $setting = GeneralSetting::create([]);
        }
        
        $setting->local_delivery_enabled = $request->enabled ? 1 : 0;
        $setting->save();

        return response()->json([
            'success' => true,
            'message' => 'Local delivery setting updated successfully!',
            'enabled' => $setting->local_delivery_enabled
        ]);
    }

    public function storeZone(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'lat' => 'required|numeric',
            'lng' => 'required|numeric',
            'radius' => 'required|numeric|min:0.1',
            'delivery_charge' => 'required|numeric|min:0',
        ]);

        LocalDeliveryZone::create([
            'name' => $request->name,
            'lat' => $request->lat,
            'lng' => $request->lng,
            'radius' => $request->radius,
            'delivery_charge' => $request->delivery_charge,
            'is_enabled' => true,
        ]);

        Toastr::success('Local delivery zone created successfully!', 'Success');
        return redirect()->back();
    }

    public function updateZone(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'lat' => 'required|numeric',
            'lng' => 'required|numeric',
            'radius' => 'required|numeric|min:0.1',
            'delivery_charge' => 'required|numeric|min:0',
        ]);

        $zone = LocalDeliveryZone::findOrFail($id);
        $zone->update([
            'name' => $request->name,
            'lat' => $request->lat,
            'lng' => $request->lng,
            'radius' => $request->radius,
            'delivery_charge' => $request->delivery_charge,
            'is_enabled' => $request->has('is_enabled'),
        ]);

        Toastr::success('Local delivery zone updated successfully!', 'Success');
        return redirect()->back();
    }

    public function destroyZone($id)
    {
        $zone = LocalDeliveryZone::findOrFail($id);
        $zone->delete();

        Toastr::success('Local delivery zone deleted.', 'Deleted');
        return redirect()->back();
    }

    public function storePoint(Request $request)
    {
        $request->validate([
            'zone_id' => 'required|exists:local_delivery_zones,id',
            'name' => 'required|string|max:255',
            'lat' => 'nullable|numeric',
            'lng' => 'nullable|numeric',
            'extra_charge' => 'required|numeric|min:0',
        ]);

        LocalDeliveryPoint::create([
            'zone_id' => $request->zone_id,
            'name' => $request->name,
            'lat' => $request->lat,
            'lng' => $request->lng,
            'extra_charge' => $request->extra_charge,
        ]);

        Toastr::success('Sub-location point added successfully!', 'Success');
        return redirect()->back();
    }

    public function destroyPoint($id)
    {
        $point = LocalDeliveryPoint::findOrFail($id);
        $point->delete();

        Toastr::success('Sub-location point removed.', 'Deleted');
        return redirect()->back();
    }

    public function updateCommissionDefault(Request $request)
    {
        $request->validate(['default_rider_commission_percent' => 'required|numeric|min:0|max:100']);
        $setting = GeneralSetting::first() ?? GeneralSetting::create([]);
        $setting->default_rider_commission_percent = $request->default_rider_commission_percent;
        $setting->save();

        return response()->json(['success' => true, 'message' => 'Default commission updated']);
    }

    public function updateCancelMinutes(Request $request)
    {
        $request->validate(['local_delivery_unassigned_cancel_minutes' => 'required|integer|min:5|max:1440']);
        $setting = GeneralSetting::first() ?? GeneralSetting::create([]);
        $setting->local_delivery_unassigned_cancel_minutes = (int) $request->local_delivery_unassigned_cancel_minutes;
        $setting->save();

        return response()->json(['success' => true, 'message' => 'Auto-cancel time updated']);
    }
}
