<?php

namespace App\Http\Controllers\Api\UnitCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\Unit\UnitRequest;
use App\Http\Resources\UnitResource;
use App\Services\UnitService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class UnitController extends Controller
{
    use ApiResponse;

    protected UnitService $service;

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

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

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

        $pagination = $request->pagination;

        if (!$pagination) {
            UnitResource::collection($units);
        } else {
            $units = UnitResource::collection($units);
        }

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

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

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

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

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

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

    public function update(UnitRequest $request, $id)
    {
        $unit = $this->service->update($request->all(), $id);

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

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

    public function toggleArchive($id)
    {
        $unit = $this->service->toggleArchive($id);

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

        $message = $unit->trashed()
            ? __(
                "messages.archived",
                ["module" => __("models.uom")]
            )
            : __(
                "messages.restored",
                ["module" => __("models.uom")]
            );

        return $this->responseSuccess($message, $unit);
    }

    // public function import(ImportRequest $request)
    // {
    //     $import = $request->all();

    //     foreach ($import as $index) {
    //         $company = Uom::create([
    //             "name" => $index["name"],
    //             "code" => $index["code"],
    //             "is_integer" => $index["is_integer"],
    //         ]);
    //     }

    //     
    // }
}
