package com.sonymusic

class ECRImage {
    String accountId
    String region
    String registry
    String repository
    String tag

    static ECRImage fromString(String ecrImage) {
        def parts = ecrImage.split('/', 2)

        if (parts.length < 2) {
            throw new IllegalArgumentException("Invalid ECR image format: ${ecrImage}")
        }

        def registry = parts[0]
        def registryParts = registry.split('\\.')

        if (registryParts.length != 6) {
            throw new IllegalArgumentException("Invalid ECR registry format: ${registry}")
        }

        def repositoryAndTag = parts[1].split(':')

        if (repositoryAndTag.length != 2) {
            throw new IllegalArgumentException("Invalid ECR image format: ${ecrImage}")
        }

        return new ECRImage(
            accountId: registryParts[0],
            region: registryParts[3],
            registry: registry,
            repository: repositoryAndTag[0],
            tag: repositoryAndTag[1]
        )
    }
}
