<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use App\Models\Uom;
use App\Models\UomCategory;

class UomController extends Controller
{
    /**
     * Display the form to create a new UOM and list existing UOMs.
     */
    public function index()
    {
        // Fetch all categories for the dropdown
        $uom_categories = UomCategory::all();

        $uoms = Uom::with('category')->get();

        // Return a view similar to your Blade: e.g. 'maintanance.inventory.create_uom'
        return view('maintanance.inventory.create_uom', compact('uoms', 'uom_categories'));
    }

    /**
     * Store a newly created UOM.
     */
    public function store(Request $request)
    {
        try {
            // Validate input
            $request->validate([
                'code' => 'required|string|max:255|unique:uoms,code',
                'description' => 'required|string|max:255',
                'uom_category_id' => 'required|integer|exists:uom_categories,id',
            ]);

            $uom = new Uom();
            $uom->code = $request->code;
            $uom->description = $request->description;
            $uom->uom_category_id = $request->uom_category_id;
            $uom->save();

            return redirect()->route('uoms.index')->with('success', 'UOM created successfully.');
        } catch (Exception $e) {
            return redirect()->back()->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
        }
    }

    /**
     * Update an existing UOM.
     * Expects 'id' in the request to identify which UOM to update.
     */
    public function edit(Request $request)
    {
        try {
            // Validate input; for unique code, ignore the current record's ID
            $request->validate([
                'id' => 'required|integer|exists:uoms,id',
                'code' => 'required|string|max:255|unique:uoms,code,' . $request->id,
                'description' => 'required|string|max:255',
                'uom_category_id' => 'required|integer|exists:uom_categories,id',
            ]);

            $uom = Uom::findOrFail($request->id);
            $uom->code = $request->code;
            $uom->description = $request->description;
            $uom->uom_category_id = $request->uom_category_id;
            $uom->save();

            return redirect()->route('uoms.index')->with('success', 'UOM updated successfully.');
        } catch (Exception $e) {
            return redirect()->back()->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
        }
    }

    /**
     * Delete a UOM by ID.
     */
    public function delete($id)
    {
        try {
            $uom = Uom::findOrFail($id);
            $uom->delete();

            return redirect()->route('uoms.index')->with('success', 'UOM deleted successfully.');
        } catch (Exception $e) {
            return redirect()->route('uoms.index')->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
        }
    }
}
