# ========= Copyright 2026 @ Strukto.AI 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.
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========

# yapf: disable
from mirage.core.google.client import (TokenManager, calendar_base, docs_base,
                                       drive_base, drive_upload_base,
                                       forms_base, gmail_base,
                                       google_error_message, sheets_base,
                                       slides_base, token_url)
# yapf: enable
from mirage.core.google.config import GoogleConfig
from mirage.core.google.constants import (CALENDAR_API_BASE, DOCS_API_BASE,
                                          DRIVE_API_BASE, DRIVE_UPLOAD_BASE,
                                          FORMS_API_BASE, GMAIL_API_BASE,
                                          SHEETS_API_BASE, SLIDES_API_BASE,
                                          TOKEN_URL)

API_ERROR = ('{"error": {"code": 400, "message": "Unsupported request: '
             'insertDimension", "status": "INVALID_ARGUMENT"}}')
TOKEN_ERROR = ('{"error": "invalid_grant", "error_description": '
               '"Token has been expired or revoked."}')


def _manager(api_base: str | None = None) -> TokenManager:
    return TokenManager(
        GoogleConfig(client_id="cid", refresh_token="rt", api_base=api_base))


def test_bases_default_to_real_google_hosts():
    tm = _manager()
    assert drive_base(tm) == DRIVE_API_BASE
    assert drive_upload_base(tm) == DRIVE_UPLOAD_BASE
    assert docs_base(tm) == DOCS_API_BASE
    assert slides_base(tm) == SLIDES_API_BASE
    assert sheets_base(tm) == SHEETS_API_BASE
    assert gmail_base(tm) == GMAIL_API_BASE
    assert calendar_base(tm) == CALENDAR_API_BASE
    assert forms_base(tm) == FORMS_API_BASE
    assert token_url(tm.config) == TOKEN_URL


def test_api_base_override_rewrites_every_service():
    tm = _manager("http://127.0.0.1:19999")
    assert drive_base(tm) == "http://127.0.0.1:19999/drive/v3"
    assert drive_upload_base(tm) == "http://127.0.0.1:19999/upload/drive/v3"
    assert docs_base(tm) == "http://127.0.0.1:19999/v1"
    assert slides_base(tm) == "http://127.0.0.1:19999/v1"
    assert sheets_base(tm) == "http://127.0.0.1:19999/v4"
    assert gmail_base(tm) == "http://127.0.0.1:19999/gmail/v1"
    assert calendar_base(tm) == "http://127.0.0.1:19999/calendar/v3"
    assert forms_base(tm) == "http://127.0.0.1:19999/v1"
    assert token_url(tm.config) == "http://127.0.0.1:19999/token"


def test_api_error_message_comes_from_the_body():
    assert google_error_message(
        API_ERROR, 400,
        "Bad Request") == "Unsupported request: insertDimension"


def test_token_endpoint_error_shape_is_flat():
    assert google_error_message(
        TOKEN_ERROR, 400,
        "Bad Request") == "invalid_grant: Token has been expired or revoked."


def test_non_json_body_is_reported_verbatim():
    assert google_error_message("<html>gateway</html>", 502,
                                "Bad Gateway") == "<html>gateway</html>"


def test_empty_body_falls_back_to_the_reason_then_the_status():
    assert google_error_message("", 404, "Not Found") == "Not Found"
    assert google_error_message("   ", 404, None) == "HTTP 404"
