"""Test S3 construct against cdk-nag."""

import aws_cdk.aws_kms as kms
import aws_cdk.aws_s3 as s3

from aws_cdk import Aspects, Stack
from cdk_nag import AwsSolutionsChecks, NagPackSuppression, NagSuppressions
from constructs import Construct

from cdk_opinionated_constructs.s3 import S3Bucket


class TestS3Stack(Stack):
    """Test generated s3 bucket against AWS solutions checks."""

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)
        shared_kms_key = kms.Key(self, "SharedKmsKey", enable_key_rotation=True)

        s3_bucket_construct = S3Bucket(self, id="bucket")
        access_logs_bucket = s3_bucket_construct.create_bucket(
            bucket_name="access-logs-bucket", encryption=s3.BucketEncryption.S3_MANAGED
        )

        NagSuppressions.add_resource_suppressions(
            access_logs_bucket,
            [
                NagPackSuppression(
                    id="AwsSolutions-S1",
                    reason="This is the access logs bucket, "
                    "it doesn't require another resource for storing access logs from it",
                )
            ],
        )

        s3_bucket_construct.create_bucket(
            bucket_name="test-s3-bucket",
            kms_key=shared_kms_key,
            encryption=s3.BucketEncryption.KMS,
            server_access_logs_bucket=access_logs_bucket,
            server_access_logs_prefix="test-s3-bucket",
        )

        # Validate stack against AWS Solutions checklist
        Aspects.of(self).add(AwsSolutionsChecks(log_ignores=True))
