<?php

namespace Modules\Accounting\Http\Controllers;

use Illuminate\Routing\Controller;
use Modules\Accounting\Http\Request\ExpenseCategoryRequest;
use Modules\Accounting\Services\ExpenseCategoryService;

class ExpenseCategoryController extends Controller
{
    public function __construct(private ExpenseCategoryService $expenseCategoryService)
    {
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        if (request()->ajax()) {
            $length = (int) (request()->length ?? 10);
            $start = (int) (request()->start ?? 0);
            $search = request()->search['value'] ?? '';

            $tableData = $this->expenseCategoryService->getDataTable($length, $start, $search);

            return response()->json([
                'draw' => request()->draw,
                'recordsTotal' => $tableData['recordsTotal'],
                'recordsFiltered' => $tableData['recordsFiltered'],
                'data' => $tableData['data'],
            ]);
        }
        return view('accounting::expense-category.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('accounting::expense-category.create');
    }
    
    /**
     * Store a newly created resource in storage.
     */
    public function store(ExpenseCategoryRequest $request)
    {
        try {
            $category = $this->expenseCategoryService->create($request->validated());

            if ($request->ajax()) {
                $categories = $this->expenseCategoryService->listForSelect();

                return response()->json([
                    'status' => 'success',
                    'message' => 'Expense Category Created Successfully',
                    'data' => [
                        'category' => $category,
                        'categories' => $categories,
                    ],
                ], 201);
            }

            return redirect()->route('expense-category.index')
                ->with('success', 'Expense Category Created Successfully');
        } catch (\Exception $e) {
            if ($request->ajax()) {
                return response()->json([
                    'status' => 'error',
                    'message' => $e->getMessage(),
                ], 500);
            }
            return redirect()->back()
                ->with('error', $e->getMessage())
                ->withInput();
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        try {
            $expense_category = $this->expenseCategoryService->getByEncryptedId($id);
            return view('accounting::expense-category.create', compact('expense_category'));
        } catch (\Exception $e) {
            return redirect()->route('expense-category.index')
                ->with('error', 'Expense Category not found');
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(ExpenseCategoryRequest $request, string $id)
    {
        try {
            $this->expenseCategoryService->update($id, $request->validated());
            return redirect()->route('expense-category.index')
                ->with('success', 'Expense Category Updated Successfully');
        } catch (\Exception $e) {
            return redirect()->route('expense-category.index')
                ->with('error', $e->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        try {
            $this->expenseCategoryService->delete($id);
            return redirect()->route('expense-category.index')
                ->with('success', 'Expense Category Deleted Successfully');
        } catch (\Exception $e) {
            return redirect()->route('expense-category.index')
                ->with('error', $e->getMessage());
        }
    }
}

