# LocalStack Resource Provider Scaffolding {{ scaffolding_version }}
import os

import pytest
# from botocore.exceptions import ClientError


class TestBasicCRD:

    def test_black_box(self, deploy_cfn_template, aws_client, snapshot):
        """
        Simple test that
        - deploys a stack containing the resource
        - verifies that the resource has been created correctly by querying the service directly
        - deletes the stack ensuring that the delete operation has been implemented correctly
        - verifies that the resource no longer exists by querying the service directly
        """
        stack = deploy_cfn_template(
            template_path=os.path.join(
                os.path.dirname(__file__),
                "{{ black_box_template_path }}",
            ),
        )
        snapshot.match("stack-outputs", stack.outputs)

        # TODO: fetch the resource and perform any required validations here
        # e.g.
        # parameter_name = stack.outputs["MyRef"]
        # snapshot.add_transformer(snapshot.transform.regex(parameter_name, "<parameter>"))

        # res = aws_client.ssm.get_parameter(Name=stack.outputs["MyRef"])
        # - this snapshot also asserts that the value set in the template is correct
        # snapshot.match("describe-resource", res)

        # verify that the delete operation works
        stack.destroy()

        # TODO: fetch the resource again and assert that it no longer exists
        # e.g.
        # with pytest.raises(ClientError):
        #     aws_client.ssm.get_parameter(Name=stack.outputs["MyRef"])

    def test_autogenerated_values(self, aws_client, deploy_cfn_template, snapshot):
        stack = deploy_cfn_template(
            template_path=os.path.join(
                os.path.dirname(__file__),
                "{{ autogenerated_template_path }}",
            ),
        )
        snapshot.match("stack_outputs", stack.outputs)

        # user_name = stack.outputs["MyRef"]

        # verify resource has been correctly deployed with the autogenerated field
        # e.g. aws_client.iam.get_user(UserName=user_name)

        # check the auto-generated pattern
        # TODO: add a sample of the auto-generated value here for reference, e.g. "TestStack-CustomUser-13AA838"


class TestUpdates:
    @pytest.mark.skip(reason="TODO")
    def test_update_without_replacement(self, deploy_cfn_template, aws_client, snapshot):
        """
        Test an UPDATE of a simple property that does not require replacing the entire resource.
        Check out the official resource documentation at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html to see if a property needs replacement
        """
        stack = deploy_cfn_template(
            template_path=os.path.join(
                os.path.dirname(__file__),
                "{{ update_template_path }}",
            ),
            parameters={"AttributeValue": "first"},
        )

        # TODO: implement fetching the resource and performing any required validations here
        res = aws_client.ssm.get_parameter(Name=stack.outputs["MyRef"])
        snapshot.match("describe-resource-before-update", res)

        # TODO: update the stack
        deploy_cfn_template(
            stack_name=stack.stack_name,
            template_path=os.path.join(
                os.path.dirname(__file__),
                "{{ update_template_path }}",
            ),
            parameters={"AttributeValue": "second"},
            is_update=True,
        )

        # TODO: check the value has changed
        res = aws_client.ssm.get_parameter(Name=stack.outputs["MyRef"])
        snapshot.match("describe-resource-after-update", res)
