name: composable-app
description: Deploy a composable app with backend service and frontend
version: 1

vars:
  project_name: ""  # Required
  service_name: "api"
  frontend_name: "web"

steps:
  create-project:
    description: Create project with monorepo skeleton
    action: api
    method: POST
    endpoint: /project
    body:
      name: "{{ .vars.project_name }}"
      description: "Composable app E2E test"
    outputs:
      - project_id: .data.name
      - domain: .data.domain

  add-service:
    description: Add backend service component
    depends_on: [create-project]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/components"
    body:
      type: service
      name: "{{ .vars.service_name }}"
      template: service
    outputs:
      - service_path: .data.path
      - service_port: .data.port

  add-frontend:
    description: Add frontend app component (app-react)
    depends_on: [create-project]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/components"
    body:
      type: app-react
      name: "{{ .vars.frontend_name }}"
      template: app-react
    outputs:
      - frontend_path: .data.path
      - frontend_port: .data.port

  verify-components:
    description: Verify all components were added
    depends_on: [add-service, add-frontend]
    action: api
    method: GET
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/components"
    outputs:
      - component_count: ".data.components | length"

  wait-pipeline:
    description: Wait for CI pipeline to complete
    depends_on: [verify-components]
    action: wait_pipeline
    project_id: "{{ .outputs.create-project.project_id }}"
    max_attempts: 60
    poll_interval: 5
    on_error: continue

  verify-site:
    description: Verify site is accessible
    depends_on: [wait-pipeline]
    action: wait_site
    domain: "{{ .outputs.create-project.domain }}"
    project_id: "{{ .outputs.create-project.project_id }}"
    max_attempts: 30
    poll_interval: 5

  test-api-health:
    description: Test API health endpoint
    depends_on: [verify-site]
    action: shell
    command: "curl -s 'https://{{ .outputs.create-project.domain }}/api/{{ .vars.service_name }}/health' 2>/dev/null || echo '{\"error\":\"connection failed\"}'"
    on_error: continue

teardown:
  - description: Delete project
    action: api
    method: DELETE
    endpoint: "/project/{{ .outputs.create-project.project_id }}"
