<?php

namespace App\Http\Controllers\Api\InventoryCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\Stock\StockTransferOutRequest;
use App\Http\Resources\StockTransferOutResource;
use App\Services\StockTransferOutService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class TransferOutController extends Controller
{
    use ApiResponse;

    protected StockTransferOutService $stockTransferOutService;

    public function __construct(StockTransferOutService $stockTransferOutService)
    {
        $this->stockTransferOutService = $stockTransferOutService;
    }

    public function index(DisplayRequest $request)
    {
        $transfers = $this->stockTransferOutService->getAll();

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

        $pagination = $request->pagination;

        if (!$pagination) {
            StockTransferOutResource::collection($transfers);
        } else {
            $transfers = StockTransferOutResource::collection($transfers);
        }

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

    public function show(int $id)
    {
        $transfer = $this->stockTransferOutService->getById($id);

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

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

    public function store(StockTransferOutRequest $request)
    {
        $transfer = $this->stockTransferOutService->create($request->validated());

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

    public function update(Request $request, int $id)
    {
        $transfer = $this->stockTransferOutService->update($request->all(), $id);

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

    public function cancel(int $id)
    {
        $transfer = $this->stockTransferOutService->cancel($id);

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