package push

import (
	"code.cloudfoundry.org/cli/integration/helpers"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gbytes"
	. "github.com/onsi/gomega/gexec"
)

var _ = Describe("push with --no-start", func() {
	var (
		appName string
	)

	BeforeEach(func() {
		appName = helpers.NewAppName()
	})

	When("the app is new", func() {
		It("pushes the app without starting it", func() {
			helpers.WithHelloWorldApp(func(dir string) {
				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
				Eventually(session).Should(Say(`Uploading files\.\.\.`))
				Eventually(session).Should(Say("100.00%"))
				Eventually(session).Should(Say(`Waiting for API to complete processing files\.\.\.`))
				Eventually(session).Should(Say(`\s+name:\s+%s`, appName))
				Eventually(session).Should(Say(`requested state:\s+stopped`))
				Eventually(session).Should(Say(`routes:\s+%s.%s`, appName, helpers.DefaultSharedDomain()))
				Eventually(session).Should(Exit(0))
			})

			session := helpers.CF("app", appName)
			Eventually(session).Should(Say(`name:\s+%s`, appName))
			Eventually(session).Should(Say(`requested state:\s+stopped`))
			Eventually(session).Should(Exit(0))
		})
	})

	When("the app exists", func() {
		When("the app is running", func() {
			BeforeEach(func() {
				helpers.WithHelloWorldApp(func(dir string) {
					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName)
					Eventually(session).Should(Say(`\s+name:\s+%s`, appName))
					Eventually(session).Should(Say(`requested state:\s+started`))
					Eventually(session).Should(Exit(0))
				})
			})

			It("stops the app", func() {
				helpers.WithHelloWorldApp(func(dir string) {
					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
					Eventually(session).Should(Say(`Uploading files\.\.\.`))
					Eventually(session).Should(Say("100.00%"))
					Eventually(session).Should(Say(`Waiting for API to complete processing files\.\.\.`))
					Eventually(session).Should(Say(`\s+name:\s+%s`, appName))
					Eventually(session).Should(Say(`requested state:\s+stopped`))
					Eventually(session).Should(Exit(0))
				})

				session := helpers.CF("app", appName)
				Eventually(session).Should(Say(`name:\s+%s`, appName))
				Eventually(session).Should(Say(`requested state:\s+stopped`))
				Eventually(session).Should(Exit(0))
			})
		})

		When("the app is stopped", func() {
			BeforeEach(func() {
				helpers.WithHelloWorldApp(func(dir string) {
					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
					Eventually(session).Should(Say(`\s+name:\s+%s`, appName))
					Eventually(session).Should(Say(`requested state:\s+stopped`))
					Eventually(session).Should(Exit(0))
				})
			})

			It("the app remains stopped", func() {
				helpers.WithHelloWorldApp(func(dir string) {
					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start")
					Eventually(session).Should(Say(`Uploading files\.\.\.`))
					Eventually(session).Should(Say("100.00%"))
					Eventually(session).Should(Say(`Waiting for API to complete processing files\.\.\.`))
					Eventually(session).Should(Say(`\s+name:\s+%s`, appName))
					Eventually(session).Should(Say(`requested state:\s+stopped`))
					Eventually(session).Should(Exit(0))
				})

				session := helpers.CF("app", appName)
				Eventually(session).Should(Say(`name:\s+%s`, appName))
				Eventually(session).Should(Say(`requested state:\s+stopped`))
				Eventually(session).Should(Exit(0))
			})
		})
	})
})
