<?php

namespace App\Http\Controllers\Api\TransactionCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\FeedConsumption\FeedRequest;
use App\Http\Resources\FeedConsumptionResource;
use App\Services\FeedConsumptionService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class FeedConsumptionController extends Controller
{
    use ApiResponse;

    protected FeedConsumptionService $service;

    public function __construct(FeedConsumptionService $service)
    {
        $this->service = $service;
    }

    public function index(DisplayRequest $request)
    {
        $status = $request->status;

        $feedConsumptions = $this->service->getAll($status);

        $pagination = $request->pagination;

        if (!$pagination) {
            FeedConsumptionResource::collection($feedConsumptions);
        } else {
            $feedConsumptions = FeedConsumptionResource::collection($feedConsumptions);
        }

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

    public function activeFarmBuilding()
    {
        $batches = $this->service->buildingBatches();

        if ($batches->isEmpty()) {
            return $this->responseSuccess(__("messages.display", ["module" => __("models.active_building_batches")]), collect());
        }

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

    public function availableItems($id)
    {
        $items = $this->service->getAvailableItems($id);

        if ($items->isEmpty()) {
            return $this->responseNotFound(__("messages.no_data", ["module" => __("models.available_feed_items")]));
        }

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

    public function store(FeedRequest $request)
    {
        $feed_consumption = $this->service->create($request->all());

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

    public function show($id)
    {
        $feed_consumption = $this->service->getById($id);

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

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

    public function update(Request $request, $id)
    {
        //
    }

    public function destroy($id)
    {
        //
    }
}
