<?php

namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
use App\Models\SupplierAccount;
use App\Models\SupplierCategory;
use App\Models\Area;
use App\Models\SupAgeing;

class SupplierAccountsController extends Controller
{
    // Show form and list of supplier accounts
    public function create_supplier_account()
    {
        $supplierAccounts = SupplierAccount::with(['category', 'area', 'ageAnalysis'])->get();
        $supplierCategories = SupplierCategory::all();
        $areas = Area::all();
        $ageingRecords = SupAgeing::all();

        return view('maintanance.suppliers.create_supplier_account', compact('supplierAccounts', 'supplierCategories', 'areas', 'ageingRecords'));
    }

    // Store new supplier account
    public function store_supplier_account(Request $request)
    {
        try {
            $request->validate([
                'supplier_code' => 'required|string|max:255|unique:supplier_accounts,supplier_code',
                'supplier_account' => 'required|string|max:255',
                'email' => 'nullable|email|max:255',
                'contact_person' => 'nullable|string|max:255',
                'contact_1' => 'nullable|string|max:255',
                'contact_2' => 'nullable|string|max:255',
                'address' => 'nullable|string|max:1000',
                'category_id' => 'nullable|exists:supplier_categories,id',
                'area_id' => 'nullable|exists:areas,id',
                'age_analysis_id' => 'nullable|exists:supplier_ageing,id',
            ], [
                'supplier_code.unique' => 'The supplier code already exists. Please use a different code.',
            ]);

            $supplier = new SupplierAccount();
            $supplier->supplier_code = $request->supplier_code;
            $supplier->supplier_account = $request->supplier_account;
            $supplier->contact_person = $request->contact_person;
            $supplier->contact_1 = $request->contact_1;
            $supplier->contact_2 = $request->contact_2;
            $supplier->email = $request->email;
            $supplier->address = $request->address;
            $supplier->category_id = $request->category_id;
            $supplier->area_id = $request->area_id;
            $supplier->age_analysis_id = $request->age_analysis_id;
            $supplier->save();

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

    // Edit supplier account
    public function edit_supplier_account(Request $request)
    {
        try {
            $request->validate([
                'id' => 'required|integer|exists:supplier_accounts,id',
                'supplier_code' => 'required|string|max:255',
                'supplier_account' => 'required|string|max:255',
                'email' => 'nullable|email|max:255',
                'contact_person' => 'nullable|string|max:255',
                'contact_1' => 'nullable|string|max:255',
                'contact_2' => 'nullable|string|max:255',
                'address' => 'nullable|string|max:1000',
                'category_id' => 'nullable|exists:supplier_categories,id',
                'area_id' => 'nullable|exists:areas,id',
                'age_analysis_id' => 'nullable|exists:supplier_ageing,id',
            ]);

            $supplier = SupplierAccount::findOrFail($request->id);
            $supplier->supplier_code = $request->supplier_code;
            $supplier->supplier_account = $request->supplier_account;
            $supplier->contact_person = $request->contact_person;
            $supplier->contact_1 = $request->contact_1;
            $supplier->contact_2 = $request->contact_2;
            $supplier->email = $request->email;
            $supplier->address = $request->address;
            $supplier->category_id = $request->category_id;
            $supplier->area_id = $request->area_id;
            $supplier->age_analysis_id = $request->age_analysis_id;
            $supplier->save();

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

    // Delete supplier account
    public function delete_supplier_account($id)
    {
        try {
            $supplier = SupplierAccount::findOrFail($id);
            $supplier->delete();

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