<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use App\Models\BinLocation;

class BinLocationController extends Controller
{
    // ===== Bin Location Methods =====

    // Show form and list of bin locations
    public function create_bin_location()
    {
        $binLocations = BinLocation::all();
        // Adjust view path if needed; e.g. 'maintanance.bin_locations.create_bin_location'
        return view('maintanance.inventory.create_bin', compact('binLocations'));
    }

    // Store new bin location (or update if id present)
    public function store_bin_location(Request $request)
    {
        try {
            $request->validate([
                'code'        => 'required|string|max:255|unique:bin_locations,code,' . $request->id,
                'description' => 'required|string|max:255',
            ], [
                'code.unique' => 'The code you entered already exists. Please use a different code.',
            ]);

            if ($request->id) {
                // update existing
                $bin = BinLocation::find($request->id);
                if (!$bin) {
                    return redirect()->back()
                                     ->withErrors(['error' => 'Bin Location not found.'])
                                     ->withInput();
                }
                $bin->code = $request->code;
                $bin->description = $request->description;
                $bin->save();

                return redirect()->route('supplier.bin_locations.create')
                                 ->with('success', 'Bin Location updated successfully.');
            } else {
                // create new
                $bin = new BinLocation();
                $bin->code = $request->code;
                $bin->description = $request->description;
                $bin->save();

                return redirect()->route('supplier.bin_locations.create')
                                 ->with('success', 'Bin Location created successfully.');
            }
        } catch (Exception $e) {
            return redirect()->back()
                             ->withErrors(['error' => 'An error occurred: ' . $e->getMessage()])
                             ->withInput();
        }
    }

    // Edit bin location (reuses store logic)
    public function edit_bin_location(Request $request)
    {
        // The form posts here when editing
        return $this->store_bin_location($request);
    }

    // Delete bin location
    public function delete_bin_location($id)
    {
        try {
            $bin = BinLocation::findOrFail($id);
            $bin->delete();

            return redirect()->route('supplier.bin_locations.create')
                             ->with('success', 'Bin Location deleted successfully.');
        } catch (Exception $e) {
            return redirect()->route('supplier.bin_locations.create')
                             ->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
        }
    }
}
