/*
 * Ollama4j - Java library for interacting with Ollama server.
 * Copyright (c) 2025 Amith Koujalgi and contributors.
 *
 * Licensed under the MIT License (the "License");
 * you may not use this file except in compliance with the License.
 *
*/
package io.github.ollama4j.unittests.jackson;

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.github.ollama4j.models.generate.OllamaGenerateRequest;
import io.github.ollama4j.utils.OptionsBuilder;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TestGenerateRequestSerialization extends AbstractSerializationTest<OllamaGenerateRequest> {

    private OllamaGenerateRequest builder;

    @BeforeEach
    public void init() {
        builder = OllamaGenerateRequest.builder().withModel("Dummy Model");
    }

    @Test
    public void testRequestOnlyMandatoryFields() {
        OllamaGenerateRequest req =
                builder.withPrompt("Some prompt").withModel("Dummy Model").build();

        String jsonRequest = serialize(req);
        assertEqualsAfterUnmarshalling(deserialize(jsonRequest, OllamaGenerateRequest.class), req);
    }

    @Test
    public void testRequestWithOptions() {
        OptionsBuilder b = new OptionsBuilder();
        OllamaGenerateRequest req =
                builder.withPrompt("Some prompt")
                        .withOptions(b.setMirostat(1).build())
                        .withModel("Dummy Model")
                        .build();

        String jsonRequest = serialize(req);
        OllamaGenerateRequest deserializeRequest =
                deserialize(jsonRequest, OllamaGenerateRequest.class);
        assertEqualsAfterUnmarshalling(deserializeRequest, req);
        assertEquals(1, deserializeRequest.getOptions().get("mirostat"));
    }

    @Test
    public void testWithJsonFormat() {
        OllamaGenerateRequest req =
                builder.withPrompt("Some prompt")
                        .withGetJsonResponse()
                        .withModel("Dummy Model")
                        .build();

        String jsonRequest = serialize(req);
        System.out.printf(jsonRequest);
        // no jackson deserialization as format property is not boolean ==> omit as deserialization
        // of request is never used in real code anyways
        JSONObject jsonObject = new JSONObject(jsonRequest);
        String requestFormatProperty = jsonObject.getString("format");
        assertEquals("json", requestFormatProperty);
    }
}
