/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  <p>
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  <p>
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.easyagents.rerank;

import com.easyagents.core.document.Document;
import com.easyagents.core.model.client.HttpClient;
import com.easyagents.core.model.rerank.RerankException;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class DefaultRerankModelBehaviorTest {

    @Test
    public void testRerankShouldThrowDetailedErrorWhenResultsEmpty() {
        DefaultRerankModel model = newModel();
        model.setHttpClient(new MockHttpClient("{\"error\":{\"message\":\"invalid documents\"}}"));

        try {
            model.rerank("query", Arrays.asList(Document.of("doc-1")));
            Assert.fail("Expected RerankException");
        } catch (RerankException e) {
            Assert.assertTrue(e.getMessage().contains("empty results"));
            Assert.assertTrue(e.getMessage().contains("invalid documents"));
        }
    }

    @Test
    public void testRerankShouldSkipNullContentAndMapOriginalIndex() {
        DefaultRerankModel model = newModel();
        model.setHttpClient(new MockHttpClient("{\"results\":[{\"index\":0,\"relevance_score\":0.8},{\"index\":1,\"relevance_score\":0.3}]}"));

        Document empty = new Document();
        Document d1 = Document.of("doc-1");
        Document d2 = Document.of("doc-2");
        List<Document> rerankResult = model.rerank("query", Arrays.asList(empty, d1, d2));

        Assert.assertNull(empty.getScore());
        Assert.assertEquals(0.8d, d1.getScore(), 0.0001d);
        Assert.assertEquals(0.3d, d2.getScore(), 0.0001d);
        Assert.assertEquals("doc-1", rerankResult.get(0).getContent());
    }

    private DefaultRerankModel newModel() {
        DefaultRerankModelConfig config = new DefaultRerankModelConfig();
        config.setEndpoint("https://example.com");
        config.setRequestPath("/v1/rerank");
        config.setModel("test-rerank-model");
        config.setApiKey("test-key");
        return new DefaultRerankModel(config);
    }

    private static class MockHttpClient extends HttpClient {
        private final String response;

        private MockHttpClient(String response) {
            this.response = response;
        }

        @Override
        public String post(String url, Map<String, String> headers, String payload) {
            return response;
        }
    }
}
