from __future__ import annotations

import sys
from functools import partial

import pytest

from django_upgrade.data import Settings
from tests.fixers import tools

settings = Settings(target_version=(3, 0))
check_noop = partial(tools.check_noop, settings=settings)
check_transformed = partial(tools.check_transformed, settings=settings)


def test_no_deprecated_alias():
    check_noop(
        """\
        from django.utils.http import something

        something
        """,
    )


def test_one_local_name():
    check_transformed(
        """\
        from django.utils.http import is_safe_url

        x = is_safe_url(y)
        """,
        """\
        from django.utils.http import url_has_allowed_host_and_scheme

        x = url_has_allowed_host_and_scheme(y)
        """,
    )


def test_one_urllib_name():
    check_transformed(
        """\
        from django.utils.http import urlquote

        x = urlquote(y)
        """,
        """\
        from urllib.parse import quote

        x = quote(y)
        """,
    )


@pytest.mark.xfail(sys.version_info < (3, 12), reason="3.12+ feature")
def test_one_f_string():
    check_transformed(
        """\
        from django.utils.http import urlquote

        f"{urlquote(y)}"
        """,
        """\
        from urllib.parse import quote

        f"{quote(y)}"
        """,
    )


def test_one_urllib_name_indented():
    check_transformed(
        """\
        if True:
            from django.utils.http import urlquote
        """,
        """\
        if True:
            from urllib.parse import quote
        """,
    )


def test_all_names():
    check_transformed(
        """\
        from django.utils.http import (
            urlquote, urlquote_plus, urlunquote, urlunquote_plus
        )

        urlquote(urlquote_plus(urlunquote(urlunquote_plus(21))))
        """,
        """\
        from urllib.parse import quote, quote_plus, unquote, unquote_plus

        quote(quote_plus(unquote(unquote_plus(21))))
        """,
    )


def test_all_names_different_format():
    check_transformed(
        """\
        from django.utils.http import (
            urlquote, # hi
                urlquote_plus, # yo
                    urlunquote, # huh
                        urlunquote_plus #??
        )

        urlquote(urlquote_plus(urlunquote(urlunquote_plus(21))))
        """,
        """\
        from urllib.parse import quote, quote_plus, unquote, unquote_plus

        quote(quote_plus(unquote(unquote_plus(21))))
        """,
    )


def test_single_alias():
    check_transformed(
        """\
        from django.utils.http import urlquote as q

        v = q("x")
        """,
        """\
        from urllib.parse import quote as q

        v = q("x")
        """,
    )


def test_mixed_aliases():
    check_transformed(
        """\
        from django.utils.http import (
            urlunquote, urlquote_plus as qp, urlquote as q, urlunquote_plus
        )

        v = q("x")
        """,
        """\
        from urllib.parse import unquote, quote_plus as qp, quote as q, unquote_plus

        v = q("x")
        """,
    )
