<?php

namespace App\Http\Controllers;

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

class PriceListNameController extends Controller
{
    // ===== Price List Name Methods =====

    // Show form and list of price list names
    public function create_price_list_name()
    {
        $priceListNames = PriceListName::all();
        return view('maintanance.inventory.create_price', compact('priceListNames'));
    }

    // Store new price list name
    public function store_price_list_name(Request $request)
    {
        try {
            $request->validate([
                'code'        => 'required|string|max:255|unique:price_list_names,code',
                'name'        => 'required|string|max:255',
                'description' => 'nullable|string|max:255',
            ], [
                'code.unique' => 'The code already exists. Please choose another.',
            ]);

            $priceList = new PriceListName();
            $priceList->code        = $request->code;
            $priceList->name        = $request->name;
            $priceList->description = $request->description;
            $priceList->save();

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

    // Edit price list name
    public function edit_price_list_name(Request $request)
    {
        try {
            $request->validate([
                'id'          => 'required|integer|exists:price_list_names,id',
                'code'        => 'required|string|max:255|unique:price_list_names,code,' . $request->id,
                'name'        => 'required|string|max:255',
                'description' => 'nullable|string|max:255',
            ], [
                'code.unique' => 'The code already exists. Please choose another.',
            ]);

            $priceList = PriceListName::findOrFail($request->id);
            $priceList->code        = $request->code;
            $priceList->name        = $request->name;
            $priceList->description = $request->description;
            $priceList->save();

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

    // Delete price list name
    public function delete_price_list_name($id)
    {
        try {
            $priceList = PriceListName::findOrFail($id);
            $priceList->delete();

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