package server.logic.ws_protocol.JSON.handlers.tempToTest;

import server.logic.ws_protocol.JSON.ConnectionContext;
import server.logic.ws_protocol.JSON.entyties.Net_Request;
import server.logic.ws_protocol.JSON.entyties.Net_Response;
import server.logic.ws_protocol.JSON.handlers.JsonMessageHandler;
import server.logic.ws_protocol.JSON.handlers.tempToTest.entyties.Net_TestGetFreeAvatarQuota_Request;
import server.logic.ws_protocol.JSON.handlers.tempToTest.entyties.Net_TestGetFreeAvatarQuota_Response;
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
import server.logic.ws_protocol.WireCodes;

public class Net_TestGetFreeAvatarQuota_Handler implements JsonMessageHandler {

    private final TestFreeAvatarArweaveService service = new TestFreeAvatarArweaveService();

    @Override
    public Net_Response handle(Net_Request baseRequest, ConnectionContext ctx) throws Exception {
        Net_TestGetFreeAvatarQuota_Request req = (Net_TestGetFreeAvatarQuota_Request) baseRequest;
        if (ctx == null || !ctx.isAuthenticatedUser()) {
            return NetExceptionResponseFactory.error(req, WireCodes.Status.UNVERIFIED, "NOT_AUTHENTICATED", "Требуется авторизация");
        }

        String login = String.valueOf(ctx.getLogin() == null ? "" : ctx.getLogin()).trim();
        TestFreeAvatarArweaveService.Quota quota = service.getQuota(login);

        Net_TestGetFreeAvatarQuota_Response resp = new Net_TestGetFreeAvatarQuota_Response();
        resp.setOp(req.getOp());
        resp.setRequestId(req.getRequestId());
        resp.setStatus(WireCodes.Status.OK);
        resp.setEnabled(quota.enabled());
        resp.setLimit(quota.limit());
        resp.setUsedCount(quota.usedCount());
        resp.setRemainingCount(quota.remainingCount());
        resp.setMaxBytes(service.getMaxBytes());
        return resp;
    }
}
