<?php

namespace App\Http\Controllers\Api\TransactionCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\Loading\LoadingRequest;
use App\Http\Requests\Loading\SaveRequest;
use App\Http\Requests\LoadingDisplayRequest;
use App\Http\Requests\Syncing\SyncTransactionsRequest;
use App\Http\Resources\LoadingResource;
use App\Services\LoadingService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class LoadingController extends Controller
{
    use ApiResponse;

    protected LoadingService $loadingService;

    public function __construct(LoadingService $loadingService)
    {
        $this->loadingService = $loadingService;
    }

    public function index(LoadingDisplayRequest $request)
    {
        $loadings = $this->loadingService->getAll();

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

        $pagination = $request->pagination;

        if (!$pagination) {
            LoadingResource::collection($loadings);
        } else {
            $loadings = LoadingResource::collection($loadings);
        }

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

    public function show($id)
    {
        $loading = $this->loadingService->getById($id);

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

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

    public function store(LoadingRequest $request)
    {
        $loading = $this->loadingService->create($request->all());

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

    public function update(LoadingRequest $request, $id)
    {
        $loading = $this->loadingService->update($request->all(), $id);

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

    public function saveRecords(SaveRequest $request)
    {
        $result = $this->loadingService->saveRecords(
            $request["ids"],
            $request->file('signature')
        );

        $loadings = $result["loadings"];
        $alreadySavedIds = $result["already_saved_ids"];

        if ($loadings->isEmpty()) {
            return $this->responseUnprocessable(
                __("messages.save_unable", ["module" => __("models.loading")]),
                __("messages.unprocessable", ["module" => __("models.loading")])
            );
        }

        if (!empty($alreadySavedIds)) {
            $ids = implode(", ", $alreadySavedIds);

            return $this->responseUnprocessable(
                __("messages.already_saved", [
                    "module" => __("models.loading"),
                    "attribute" => $ids,
                ]),
                __("messages.unprocessable", ["module" => __("models.loading")])
            );
        }

        return $this->responseSuccess(
            __("messages.save", ["module" => __("models.loading")]),
            LoadingResource::collection($loadings)
        );
    }

    public function voidLoading($id)
    {
        $loading = $this->loadingService->voidLoading($id);

        if ($loading === false) {
            return $this->responseUnprocessable(
                __("messages.void_unable", ["module" => __("models.loading")]),
                __("messages.invalid_void", ["module" => __("models.loading")])
            );
        }

        if (!$loading) {
            return $this->responseUnprocessable(
                __("messages.save_unable", ["module" => __("models.loading")]),
                __("messages.unprocessable", ["module" => __("models.loading")])
            );
        }

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

    public function viewAttachment($batchId)
    {
        $attachmentPath = $this->loadingService->viewAttachment($batchId);

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

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

        return Storage::disk("public")->response($attachmentPath);
    }

    public function syncTransactions(SyncTransactionsRequest $request)
    {
        $result = $this->loadingService->syncTransaction($request->all());

        if (!empty($result['failed'])) {
            return $this->responseSuccess('Sync completed with errors', $result);
        }

        return $this->responseSuccess('Sync completed successfully', $result);
    }
}
