# ═══════════════════════════════════════════════════════════════════════════════
# tbdflow — Branch Creation & Naming
# ═══════════════════════════════════════════════════════════════════════════════
# User Story: As a developer, I want to create short-lived branches with
# consistent naming so that my team can understand work-in-progress at a glance.
# ═══════════════════════════════════════════════════════════════════════════════

feature "tbdflow Branch Creation"

actors: Terminal

settings {
    timeout_seconds = 30
    stop_on_failure = true
    shell_path = "/bin/bash"
}

var REPO_DIR = "/tmp/tbdflow_branch_test"
var BARE_REPO = "/tmp/tbdflow_branch_bare.git"

# ═══════════════════════════════════════════════════════════════════════════════
# IMPLEMENTATION LAYER — Tasks hide the "how"
# ═══════════════════════════════════════════════════════════════════════════════

task init_repo(repo, bare) {
    Terminal run "rm -rf ${repo} ${bare} && mkdir -p ${repo}"
    Terminal run "git init --bare ${bare}"
    Terminal run "git -C ${repo} init && git -C ${repo} commit --allow-empty -m 'init'"
    Terminal run "git -C ${repo} remote add origin ${bare}"
    Terminal run "git -C ${repo} push -u origin main"
    Terminal set_cwd "${repo}"
}

task cleanup_repo(repo, bare) {
    Terminal run "rm -rf ${repo} ${bare}"
}

task create_branch(type, name) {
    Terminal run "tbdflow branch -t ${type} -n ${name}"
}

task create_branch_with_issue(type, name, issue) {
    Terminal run "tbdflow branch -t ${type} -n ${name} --issue ${issue}"
}

task return_to_main() {
    Terminal run "git checkout main"
}

task verify_branch_created(expected_name) {
    Terminal last_command succeeded
    Terminal output_contains "${expected_name}"
    Terminal output_not_contains "Error"
}

# ═══════════════════════════════════════════════════════════════════════════════
# BUSINESS SPECIFICATION LAYER — Scenarios describe the "what"
# ═══════════════════════════════════════════════════════════════════════════════

# AC1: Branches must follow the <type>/<name> convention.
# AC2: Issue keys must be embedded in the branch name when provided.
# AC3: Multiple branch types (feat, fix, chore) must be supported.

scenario "Creating short-lived branches" {

    test Setup "Initialise test repository" {
        given:
            Test can_start
        when:
            init_repo("${REPO_DIR}", "${BARE_REPO}")
        then:
            Terminal last_command succeeded
    }

    test FeatBranch "Create a feature branch" {
        given:
            Test has_succeeded Setup
        when:
            create_branch("feat", "add-user-profile")
        then:
            verify_branch_created("feat/add-user-profile")
    }

    test BranchWithIssue "Create a branch with an issue key" {
        given:
            Test has_succeeded FeatBranch
            return_to_main()
        when:
            create_branch_with_issue("fix", "resolve-timeout", "API-456")
        then:
            verify_branch_created("fix/API-456-resolve-timeout")
    }

    test ChoreBranch "Create a chore branch" {
        given:
            Test has_succeeded BranchWithIssue
            return_to_main()
        when:
            create_branch("chore", "update-deps")
        then:
            verify_branch_created("chore/update-deps")
    }

    after {
        cleanup_repo("${REPO_DIR}", "${BARE_REPO}")
    }
}
