<?php

namespace App\Http\Controllers;

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

class CustomerCategoryController extends Controller
{
    // Show form and list of categories
    public function create_customer_category()
    {
        $categories = CustomerCategory::all();
        return view('maintanance.customers.create_customer_category', compact('categories'));
    }

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

        $category = new CustomerCategory();
        $category->code = $request->code;
        $category->name = $request->name;
        $category->description = $request->description;
        $category->save();

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

    // Edit category
    public function edit_customer_category(Request $request)
    {
        try {
            $request->validate([
                'id' => 'required|integer|exists:customer_categories,id',
                'code' => 'required|string|max:255',
                'name' => 'required|string|max:255',
                'description' => 'required|string|max:255',
            ]);

            $result = CustomerCategory::editCategory(
                $request->id,
                $request->code,
                $request->name,
                $request->description
            );

            if ($result) {
                return redirect()->route('customer.categories.create')->with('success', 'Category updated successfully.');
            }

            return redirect()->route('customer.categories.create')->withErrors(['error' => 'Update failed.']);
        } catch (Exception $e) {
            return redirect()->back()->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
        }
    }

    // Delete category
    public function delete_customer_category($id)
    {
        try {
            $result = CustomerCategory::deleteCategory($id);

            if ($result) {
                return redirect()->route('customer.categories.create')->with('success', 'Category deleted successfully.');
            }

            return redirect()->route('customer.categories.create')->withErrors(['error' => 'Delete failed.']);
        } catch (Exception $e) {
            return redirect()->route('customer.categories.create')->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
        }
    }
}
