import boto3

from moto import mock_wafv2

from .test_helper_functions import CREATE_WEB_ACL_BODY


@mock_wafv2
def test_list_tags_for_resource__none_supplied():
    conn = boto3.client("wafv2", region_name="us-east-1")
    arn = conn.create_web_acl(**CREATE_WEB_ACL_BODY("John", "REGIONAL"))["Summary"][
        "ARN"
    ]

    tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
    assert tag_info["TagList"] == []


@mock_wafv2
def test_list_tags_for_resource():
    conn = boto3.client("wafv2", region_name="us-east-1")
    arn = conn.create_web_acl(
        Name="test",
        Scope="CLOUDFRONT",
        DefaultAction={"Allow": {}},
        Tags=[{"Key": "k1", "Value": "v1"}],
        VisibilityConfig={
            "SampledRequestsEnabled": False,
            "CloudWatchMetricsEnabled": False,
            "MetricName": "idk",
        },
    )["Summary"]["ARN"]

    tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
    assert tag_info["TagList"] == [{"Key": "k1", "Value": "v1"}]


@mock_wafv2
def test_tag_resource():
    conn = boto3.client("wafv2", region_name="us-east-1")
    arn = conn.create_web_acl(
        Name="test",
        Scope="CLOUDFRONT",
        DefaultAction={"Allow": {}},
        Tags=[{"Key": "k1", "Value": "v1"}],
        VisibilityConfig={
            "SampledRequestsEnabled": False,
            "CloudWatchMetricsEnabled": False,
            "MetricName": "idk",
        },
    )["Summary"]["ARN"]

    conn.tag_resource(
        ResourceARN=arn,
        Tags=[
            {"Key": "k2", "Value": "v2"},
        ],
    )

    tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
    assert tag_info["TagList"] == [
        {"Key": "k1", "Value": "v1"},
        {"Key": "k2", "Value": "v2"},
    ]


@mock_wafv2
def test_untag_resource():
    conn = boto3.client("wafv2", region_name="us-east-1")
    arn = conn.create_web_acl(
        Name="test",
        Scope="CLOUDFRONT",
        DefaultAction={"Allow": {}},
        Tags=[{"Key": "k1", "Value": "v1"}, {"Key": "k2", "Value": "v2"}],
        VisibilityConfig={
            "SampledRequestsEnabled": False,
            "CloudWatchMetricsEnabled": False,
            "MetricName": "idk",
        },
    )["Summary"]["ARN"]

    conn.untag_resource(ResourceARN=arn, TagKeys=["k1"])

    tag_info = conn.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"]
    assert tag_info["TagList"] == [{"Key": "k2", "Value": "v2"}]
