<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Expense\StoreExpenseCategoryRequest;
use App\Http\Resources\ExpenseCategoryResource;
use App\Models\ExpenseCategory;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

class ExpenseCategoryController extends ApiController
{
    public function index(Request $request): AnonymousResourceCollection
    {
        $categories = ExpenseCategory::query()
            ->where('company_id', $this->currentCompanyId($request))
            ->orderBy('name')
            ->get();

        return ExpenseCategoryResource::collection($categories);
    }

    public function store(StoreExpenseCategoryRequest $request): ExpenseCategoryResource
    {
        $category = ExpenseCategory::create([
            ...$request->validated(),
            'company_id' => $this->currentCompanyId($request),
        ]);

        return new ExpenseCategoryResource($category);
    }
}
