<?php

namespace App\Http\Controllers\Api\InventoryCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\MiscReceipt\MiscReceiptRequest;
use App\Http\Resources\MiscReceiptResource;
use App\Services\MiscReceiptService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class MiscellaneuosReceiptController extends Controller
{
    use ApiResponse;

    protected MiscReceiptService $miscReceiptService;

    public function __construct(MiscReceiptService $miscReceiptService)
    {
        $this->miscReceiptService = $miscReceiptService;
    }

    public function index(DisplayRequest $request)
    {
        $misc_receipts = $this->miscReceiptService->getAll();

        if ($misc_receipts->isEmpty()) {
            return $this->responseNotFound(
                __(
                    "messages.not_found",
                    ["module" => __("models.misc_receipt_plural")]
                )
            );
        }

        $pagination = $request->pagination;

        if (!$pagination) {
            MiscReceiptResource::collection($misc_receipts);
        } else {
            $misc_receipts = MiscReceiptResource::collection($misc_receipts);
        }

        return $this->responseSuccess(
            __(
                "messages.display",
                ["module" => __("models.misc_receipt_plural")]
            ),
            $misc_receipts
        );
    }

    public function show($id)
    {
        $misc_receipts = $this->miscReceiptService->getById($id);

        if (!$misc_receipts) {
            return $this->responseNotFound(
                __(
                    "messages.not_found",
                    ["module" => __("models.misc_receipt")]
                )
            );
        }

        return $this->responseSuccess(
            __(
                "messages.display",
                ["module" => __("models.misc_receipt")]
            ),
            new MiscReceiptResource($misc_receipts)
        );
    }

    public function store(MiscReceiptRequest $request)
    {
        $misc_receipts = $this->miscReceiptService->create($request->all());

        return $this->responseCreated(__("messages.created", ["module" => __("models.misc_receipt")]), $misc_receipts);
    }
}
