<?php

namespace App\Http\Controllers\Api\TransactionCollection;

use App\Http\Controllers\Controller;
use App\Http\Requests\DisplayRequest;
use App\Http\Requests\Harvest\HarvestRequest;
use App\Http\Requests\Harvest\HarvestUpdateRequest;
use App\Http\Requests\Harvest\SaveRequest;
use App\Http\Requests\LoadingDisplayRequest;
use App\Http\Resources\HarvestingResource;
use App\Models\Harvest;
use App\Services\HarvestService;
use Essa\APIToolKit\Api\ApiResponse;
use Illuminate\Http\Request;

class HarvestController extends Controller
{
    use ApiResponse;

    public function __construct(protected HarvestService $harvestService) {}

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

        return $harvest = $this->harvestService->getAll($status);

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

        if (!$pagination) {
            HarvestingResource::collection($harvest);
        } else {
            $harvest = HarvestingResource::collection($harvest);
        }

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

    public function show($id)
    {
        $harvest = $this->harvestService->getById($id);

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

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

    public function store(HarvestRequest $request)
    {
        $harvest = $this->harvestService->create($request->all());

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

    public function update(HarvestUpdateRequest $request, $id)
    {
        $harvest = $this->harvestService->update($id, $request->all());

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

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

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

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

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

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

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

    public function view_attachment($harvestId)
    {
        $attachment = $this->harvestService->viewAttachment($harvestId);

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

        return $attachment;
    }
}
