<?php

namespace App\Http\Controllers\Api\InventoryCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\MoveOrder\MoveRequest;
use App\Http\Requests\MoveOrder\MoveUpdateRequest;
use App\Http\Resources\MoveOrderResource;
use App\Services\MoveOrderService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class MoveOrderController extends Controller
{
    use ApiResponse;

    protected MoveOrderService $service;

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

    public function index(DisplayRequest $request)
    {
        $move_order = $this->service->getAll();

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

        $pagination = $request->pagination;

        if (!$pagination) {
            MoveOrderResource::collection($move_order);
        } else {
            $move_order = MoveOrderResource::collection($move_order);
        }

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

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

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

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

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

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

    public function update(MoveUpdateRequest $request, int $id)
    {
        $move_order = $this->service->update($request->all(), $id);

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