<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use App\Models\InventoryCategory;
use Illuminate\Support\Facades\Validator;

class InventoryCategoryController extends Controller
{
    // ===== Inventory Category Methods =====

    // Show form and list of inventory categories
    public function create_inventory_category()
    {
        $categories = InventoryCategory::all();
        // Adjust the view path if yours differs; e.g. 'maintanance.inventory.create_inventory_category'
        return view('maintanance.inventory.create_category', compact('categories'));
    }

    // Store new inventory category
    public function store_inventory_category(Request $request)
    {
        try {
            $request->validate([
                'group_code'  => 'required|string|max:255|unique:inventory_categories,group_code',
                'name'        => 'required|string|max:255',
                'description' => 'required|string|max:255',
            ], [
                'group_code.unique' => 'The group code you entered already exists. Please use a different code.',
            ]);

            $category = new InventoryCategory();
            $category->group_code  = $request->group_code;
            $category->name        = $request->name;
            $category->description = $request->description;
            $category->save();

            // Adjust route name to match your routes file
            return redirect()->route('supplier.inventory_categories.create')
                             ->with('success', 'Inventory category created successfully.');
        } catch (Exception $e) {
            return redirect()->back()
                             ->withErrors(['error' => 'An error occurred: ' . $e->getMessage()])
                             ->withInput();
        }
    }

    // Edit inventory category
    public function edit_inventory_category(Request $request)
    {
        try {
            $request->validate([
                'id'          => 'required|integer|exists:inventory_categories,id',
                'group_code'  => 'required|string|max:255|unique:inventory_categories,group_code,' . $request->id,
                'name'        => 'required|string|max:255',
                'description' => 'required|string|max:255',
            ], [
                'group_code.unique' => 'The group code you entered already exists. Please use a different code.',
            ]);

            $category = InventoryCategory::findOrFail($request->id);
            $category->group_code  = $request->group_code;
            $category->name        = $request->name;
            $category->description = $request->description;
            $category->save();

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

    // Delete inventory category
    public function delete_inventory_category($id)
    {
        try {
            $category = InventoryCategory::findOrFail($id);
            $category->delete();

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