from aws_cdk import (
    core as cdk,
    aws_codecommit as cc,
    aws_codebuild as cb,
    aws_codepipeline as cp,
    aws_codepipeline_actions as cpa,
)


class PipelineStack(cdk.Stack):

    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        repo = cc.Repository(
            self, 'CicdPipelineCodeRepo',
            repository_name="Cicd-Pipeline-Code-Repo"
        )

        cb_source = cb.Source.code_commit(repository=repo)

        source_artifact = cp.Artifact()

        source_action=cpa.CodeCommitSourceAction(
            action_name='CodeCommit',
            output=source_artifact,
            repository=repo
        )


        cb_project=cb.Project(self, "Cicd-Pipeline-Build-Project",
            environment={
                "build_image": cb.LinuxBuildImage.from_code_build_image_id("aws/codebuild/standard:5.0"),
                "privileged": True,
            },
            source=cb_source,
        )


        pipeline = cp.Pipeline(
            self, 'cicd-pipeline',
            pipeline_name='Cicd-Pipeline',
            stages=[
                cp.StageProps(stage_name="Source", actions=[source_action]),
                cp.StageProps(
                    stage_name="Build",
                    actions=[
                        cpa.CodeBuildAction(
                            action_name="Build",
                            project=cb_project,
                            input=source_artifact,
                        )
                    ],
                ),
            ],
        )
        
        cdk.CfnOutput(
            self, "REPOURI",
            description="REPO URI",
            value=repo.repository_clone_url_grc,
        )