<?php

namespace App\Http\Controllers\Api;

use App\Services\OneService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Charging\ChangePasswordRequest;
use App\Http\Requests\Charging\OneRdfDisplay;
use App\Http\Requests\Charging\OneUserStoreRequest;
use App\Services\OneChargingService;
use Essa\APIToolKit\Api\ApiResponse;

class OneChargingController extends Controller
{
    use ApiResponse;

    protected OneChargingService $oneChargingService;

    public function __construct(OneChargingService $oneChargingService)
    {
        $this->oneChargingService = $oneChargingService;
    }

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

        $data = $this->oneChargingService->getAll($status);

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

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

    public function show($id)
    {
        $one_charging = $this->oneChargingService->getById($id);

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

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

    public function sync()
    {
        $result = $this->oneChargingService->sync();

        if (!$result["success"]) {
            return $this->responseServerError($result["message"]);
        }

        return $this->responseSuccess($result["message"], $result["data"]);
    }

    public function oneRdfUserIndex(OneRdfDisplay $request)
    {
        $result = $this->oneChargingService->oneRdfUserIndex($request->all());

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

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

    public function oneRdfUserShow($id)
    {
        $result = $this->oneChargingService->oneRdfUserShow($id);

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

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

    public function oneRdfUserSync(OneUserStoreRequest $request)
    {
        $result = $this->oneChargingService->userSync($request->all());

        if (isset($result["created"])) {
            return $this->responseCreated(__("messages.created", ["module" => __("models.user")]), $result);
        }

        if (isset($result["updated"]) && $result["updated"] === true) {
            return $this->responseSuccess(__("messages.updated", ["module" => __("models.user")]), $result);
        }

        return $this->responseSuccess("No changes detected.", $result);
    }

    public function changePassword(ChangePasswordRequest $request, $id)
    {
        $result = $this->oneChargingService->changePassword($request->all(), $id);

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

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

    public function resetPassword(Request $request, $id)
    {
        $result = $this->oneChargingService->resetPassword($id);

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

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