package StepDefinitions;


import Resources.TestData;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.restassured.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.testng.Assert;

import java.io.IOException;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.notNullValue;

@Slf4j
public class PromptCall {
    private Response response;
    TestData data = new TestData();
    String requestBody;

    @Given("LLM model body with {}")
    public void llmModelBodyWith(String prompt) {
        log.info("Creating request body with prompt: {}", prompt);
        requestBody = data.prompt(prompt);
        System.out.println(requestBody);
    }

    @When("user calls prompt API using POST method")
    public void userCallsPromptAPIUsingPOSTMethod()  {

           log.info("Serialized request body: {}", requestBody);
           System.out.println("----------> "+requestBody);
           response = given().log().all().contentType("application/json")
                   .body(requestBody).when()
                   .post("http://localhost:11434/api/generate");
           System.out.println(response.getBody().asString());

    }

     @Then("user verifies status code in response")
    public void thenUserVerifiesStatusCode() {
        response.then().statusCode(200);
    }

     @And("user verifies response attribute from response")
    public void andUserVerifiesResponseAttribute() {
        response.then().body("response", notNullValue());
        String responseBody = response.getBody().asString();
        Assert.assertNotNull(responseBody);
        System.out.println(responseBody);
        log.info("Response Body: {}", responseBody);

    }

    @Given("LLM model body from a file")
    public void llm_model_body_from_a_file() throws IOException {
        String error ="error is java.lang.AssertionError: Page title is not correct expected [Tpoint Tech - Free Online Tutorials conveyed] but found [Tpoint Tech - Free Online Tutorials] and the method where the error is public void the_homepage_opens_verify_it() {\n" +
                "        String expectedTitle = \"Tpoint Tech - Free Online Tutorials conveyed\";\n" +
                "        String actualTitle = driver.getTitle();\n" +
                "        Assert.assertEquals(actualTitle, expectedTitle, \"Page title is not correct\");\n" +
                "    } how to resolve the above issue give one perfect solution rather than multiple solutions";
        String fileName = "ExceptionPrompt.txt";
        requestBody = data.errorPrompt(error, fileName);
        System.out.println(requestBody);
    }

    @Then("print the LLM response attribute in a formatted way")
    public void print_the_llm_response_attribute_in_a_formatted_way() {
        response.then().body("response", notNullValue());
        response.getBody().asString();
        String llmResponse = TestData.getJsonPath(response, "response");
        String formattedResponse = TestData.convertStringToBulletPoints(llmResponse);
        System.out.println(formattedResponse);

    }






}