#
#  Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
#  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
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  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.
#
from concurrent.futures import ThreadPoolExecutor, as_completed

import pytest
from common import batch_create_chat_assistants, delete_chat_assistants, list_chat_assistants
from configs import INVALID_API_TOKEN
from libs.auth import RAGFlowHttpApiAuth


@pytest.mark.p1
class TestAuthorization:
    @pytest.mark.parametrize(
        "invalid_auth, expected_code, expected_message",
        [
            (None, 401, "<Unauthorized '401: Unauthorized'>"),
            (RAGFlowHttpApiAuth(INVALID_API_TOKEN), 401, "<Unauthorized '401: Unauthorized'>"),
        ],
    )
    def test_invalid_auth(self, invalid_auth, expected_code, expected_message):
        res = delete_chat_assistants(invalid_auth)
        assert res["code"] == expected_code
        assert res["message"] == expected_message


class TestChatAssistantsDelete:
    @pytest.mark.parametrize(
        "payload, expected_code, expected_message, remaining",
        [
            pytest.param(None, 0, "", 5, marks=pytest.mark.p3),
            pytest.param({"ids": []}, 0, "", 5, marks=pytest.mark.p3),
            pytest.param({"ids": ["invalid_id"]}, 102, "Chat(invalid_id) not found.", 5, marks=pytest.mark.p3),
            pytest.param({"ids": ["\n!?。；！？\"'"]}, 102, """Chat(\n!?。；！？"\') not found.""", 5, marks=pytest.mark.p3),
            pytest.param("not json", 100, "AttributeError(\"'str' object has no attribute 'get'\")", 5, marks=pytest.mark.p3),
            pytest.param(lambda r: {"ids": r[:1]}, 0, "", 4, marks=pytest.mark.p3),
            pytest.param(lambda r: {"ids": r}, 0, "", 0, marks=pytest.mark.p1),
        ],
    )
    def test_basic_scenarios(self, HttpApiAuth, add_chat_assistants_func, payload, expected_code, expected_message, remaining):
        _, _, chat_assistant_ids = add_chat_assistants_func
        if callable(payload):
            payload = payload(chat_assistant_ids)
        res = delete_chat_assistants(HttpApiAuth, payload)
        assert res["code"] == expected_code
        if res["code"] != 0:
            assert res["message"] == expected_message

        res = list_chat_assistants(HttpApiAuth)
        assert len(res["data"]["chats"]) == remaining

    @pytest.mark.parametrize(
        "payload",
        [
            pytest.param(lambda r: {"ids": ["invalid_id"] + r}, marks=pytest.mark.p3),
            pytest.param(lambda r: {"ids": r[:1] + ["invalid_id"] + r[1:5]}, marks=pytest.mark.p1),
            pytest.param(lambda r: {"ids": r + ["invalid_id"]}, marks=pytest.mark.p3),
        ],
    )
    def test_delete_partial_invalid_id(self, HttpApiAuth, add_chat_assistants_func, payload):
        _, _, chat_assistant_ids = add_chat_assistants_func
        if callable(payload):
            payload = payload(chat_assistant_ids)
        res = delete_chat_assistants(HttpApiAuth, payload)
        assert res["code"] == 0
        assert res["data"]["errors"][0] == "Chat(invalid_id) not found."
        assert res["data"]["success_count"] == 5

        res = list_chat_assistants(HttpApiAuth)
        assert len(res["data"]["chats"]) == 0

    @pytest.mark.p3
    def test_repeated_deletion(self, HttpApiAuth, add_chat_assistants_func):
        _, _, chat_assistant_ids = add_chat_assistants_func
        res = delete_chat_assistants(HttpApiAuth, {"ids": chat_assistant_ids})
        assert res["code"] == 0

        res = delete_chat_assistants(HttpApiAuth, {"ids": chat_assistant_ids})
        assert res["code"] == 102
        assert "not found" in res["message"]

    @pytest.mark.p3
    def test_duplicate_deletion(self, HttpApiAuth, add_chat_assistants_func):
        _, _, chat_assistant_ids = add_chat_assistants_func
        res = delete_chat_assistants(HttpApiAuth, {"ids": chat_assistant_ids + chat_assistant_ids})
        assert res["code"] == 0
        assert "Duplicate chat ids" in res["data"]["errors"][0]
        assert res["data"]["success_count"] == 5

        res = list_chat_assistants(HttpApiAuth)
        assert res["code"] == 0

    @pytest.mark.p3
    def test_concurrent_deletion(self, HttpApiAuth):
        count = 100
        ids = batch_create_chat_assistants(HttpApiAuth, count)

        with ThreadPoolExecutor(max_workers=5) as executor:
            futures = [executor.submit(delete_chat_assistants, HttpApiAuth, {"ids": ids[i : i + 1]}) for i in range(count)]
        responses = list(as_completed(futures))
        assert len(responses) == count, responses
        assert all(future.result()["code"] == 0 for future in futures)

    @pytest.mark.p3
    def test_delete_10k(self, HttpApiAuth):
        ids = batch_create_chat_assistants(HttpApiAuth, 1_000)
        res = delete_chat_assistants(HttpApiAuth, {"ids": ids})
        assert res["code"] == 0

        res = list_chat_assistants(HttpApiAuth)
        assert len(res["data"]["chats"]) == 0

    @pytest.mark.p2
    def test_delete_all_errors_no_success_p2(self, HttpApiAuth, add_chat_assistants_func):
        delete_payload = {"ids": ["missing-1", "missing-2"]}
        res = delete_chat_assistants(HttpApiAuth, delete_payload)
        assert res["code"] == 102
        assert "Chat(missing-1) not found." in res["message"]
        assert "Chat(missing-2) not found." in res["message"]

    @pytest.mark.p2
    def test_delete_duplicate_partial_success_p2(self, HttpApiAuth, add_chat_assistants_func):
        _, _, chat_assistant_ids = add_chat_assistants_func
        payload = {"ids": [chat_assistant_ids[0], chat_assistant_ids[0]]}
        res = delete_chat_assistants(HttpApiAuth, payload)
        assert res["code"] == 0
        assert res["data"]["success_count"] == 1
        assert "Duplicate chat ids" in res["data"]["errors"][0]
