<?php

namespace App\Http\Controllers;

use App\Models\Branch;
use App\Models\User;
use App\Models\VanLoading;
use App\Models\VanSession;
use App\Models\Warehouse;
use App\Models\WarehouseStock;
use App\Services\VanSalesService;
use Illuminate\Http\Request;

class VanSalesController extends Controller
{
    public function __construct(private VanSalesService $vans) {}
    public function index() { $sessions = VanSession::with(['warehouse', 'salesman', 'sales', 'loadings'])->latest('session_date')->paginate(30); return view('van.index', compact('sessions')); }
    public function create() { $warehouses = Warehouse::where('type', 'van')->where('status', 'active')->get(); $branches = Branch::where('status', 'active')->get(); $salesmen = User::where('status', 'active')->orderBy('name')->get(); return view('van.open', compact('warehouses', 'branches', 'salesmen')); }
    public function store(Request $request) { $data = $request->validate(['warehouse_id' => 'required|exists:warehouses,id', 'branch_id' => 'nullable|exists:branches,id', 'salesman_id' => 'required|exists:users,id', 'driver_name' => 'nullable|string|max:100', 'route' => 'nullable|string|max:100', 'session_date' => 'required|date', 'opening_odometer' => 'nullable|numeric|min:0', 'starting_cash' => 'nullable|numeric|min:0']); try { $session = $this->vans->openSession($data); return redirect()->route('van.show', $session)->with('success', 'Van session opened.'); } catch (\Throwable $e) { report($e); return back()->withInput()->with('error', $e->getMessage()); } }
    public function show(VanSession $session) { $session->load(['warehouse', 'salesman', 'sales.customer', 'loadings.items.product', 'closing']); $stock = WarehouseStock::with('product')->where('warehouse_id', $session->warehouse_id)->where('qty', '!=', 0)->get(); return view('van.show', compact('session', 'stock')); }
    public function createLoading(VanSession $session) { abort_unless($session->status === 'open', 409); $warehouses = Warehouse::where('status', 'active')->where('type', '!=', 'van')->get(); return view('van.loading', compact('session', 'warehouses')); }
    public function storeLoading(Request $request, VanSession $session) { $data = $request->validate(['source_warehouse_id' => 'required|exists:warehouses,id', 'loading_date' => 'required|date', 'notes' => 'nullable|string|max:1000', 'items' => 'required|array|min:1', 'items.*.product_id' => 'required|exists:products,id', 'items.*.unit_id' => 'nullable|exists:units,id', 'items.*.quantity' => 'required|numeric|gt:0']); try { $loading = $this->vans->requestLoading($session, $data, $data['items']); return redirect()->route('van.show', $session)->with('success', 'Loading request created: '.$loading->loading_no); } catch (\Throwable $e) { report($e); return back()->withInput()->with('error', $e->getMessage()); } }
    public function approveLoading(VanLoading $loading) { try { $session = $loading->van_session_id; $this->vans->approveLoading($loading); return redirect()->route('van.show', $session)->with('success', 'Van loading approved and stock transferred.'); } catch (\Throwable $e) { report($e); return back()->with('error', $e->getMessage()); } }
    public function close(Request $request, VanSession $session) { $data = $request->validate(['actual_cash' => 'required|numeric|min:0', 'collections' => 'nullable|numeric|min:0', 'expenses' => 'nullable|numeric|min:0', 'closing_odometer' => 'nullable|numeric|min:0', 'notes' => 'nullable|string|max:1000']); try { $this->vans->closeSession($session, $data); return redirect()->route('van.show', $session)->with('success', 'Van session closed successfully.'); } catch (\Throwable $e) { report($e); return back()->withInput()->with('error', $e->getMessage()); } }
}
