<?php

namespace App\Http\Controllers\Api\SupplierCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\Supplier\SupplierRequest;
use App\Http\Resources\SupplierResource;
use App\Services\SupplierService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class SupplierController extends Controller
{
    use ApiResponse;

    protected SupplierService $supplierService;

    public function __construct(SupplierService $supplierService)
    {
        $this->supplierService = $supplierService;
    }

    public function index(DisplayRequest $request)
    {
        $suppliers = $this->supplierService->getAll();

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

        $pagination = $request->pagination;

        if (!$pagination) {
            SupplierResource::collection($suppliers);
        } else {
            $suppliers = SupplierResource::collection($suppliers);
        }

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

    public function store(SupplierRequest $request)
    {
        $supplier = $this->supplierService->create($request->validated());

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

    public function show($id)
    {
        $supplier = $this->supplierService->getById($id);

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

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

    public function update(SupplierRequest $request, $id)
    {
        $supplier = $this->supplierService->update($request->all(), $id);

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

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

        return $this->responseSuccess($message, new SupplierResource($supplier));
    }

    public function toggleArchive($id)
    {
        $supplier = $this->supplierService->toggleArchive($id);

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

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

        return $this->responseSuccess($message, new SupplierResource($supplier));
    }
}
