<?php

namespace App\Http\Controllers\Api\ItemCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\Item\ImportRequest;
use App\Http\Requests\Item\ItemRequest;
use App\Http\Resources\ItemResource;
use App\Services\ItemService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    use ApiResponse;

    protected ItemService $itemService;

    public function __construct(ItemService $itemService)
    {
        $this->itemService = $itemService;
    }

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

        $item = $this->itemService->getAll($status);

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

        if (!$pagination) {
            ItemResource::collection($item);
        } else {
            $item = ItemResource::collection($item);
        }

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

    public function show($id)
    {
        $item = $this->itemService->getById($id);

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

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

    public function store(ItemRequest $request)
    {
        $item = $this->itemService->create($request->all());

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

    public function update(ItemRequest $request, $id)
    {
        $item = $this->itemService->update($id, $request->all());

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

        $message = $item->was_changed
            ? __("messages.updated", ["module" => __("models.item")])
            : __("messages.no_changes", ["module" => __("models.item")]);

        return $this->responseSuccess($message, new ItemResource($item));
    }

    public function toggleArchive($id)
    {
        $item = $this->itemService->toggleArchive($id);

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

        $message =
            $item["status"] === "restored"
            ? __("messages.restored", ["module" => __("models.item")])
            : __("messages.archived", ["module" => __("models.item")]);

        return $this->responseSuccess(
            $message,
            new ItemResource($item["item"])
        );
    }

    public function import(ImportRequest $request)
    {
        $result = $this->itemService->import($request->file("file"));

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