name: full-stack-feature
description: End-to-end enterprise feature development: Spec -> Design -> Implementation (DB + API) -> Verification
version: 1

vars:
  project_name: "" # Required
  service_name: "api"
  feature_slug: "todos-crud"

steps:
  # --- Phase 1: Infrastructure ---
  create-project:
    description: Create project with monorepo skeleton
    action: api
    method: POST
    endpoint: /project
    body:
      name: "{{ .vars.project_name }}"
      description: "Full Stack Feature 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

  wait-init-pipeline:
    description: Wait for initial build
    depends_on: [add-service]
    action: wait_pipeline
    project_id: "{{ .outputs.create-project.project_id }}"
    max_attempts: 60

  # --- Phase 2: SDLC Process (Spec & Design) ---
  create-feature:
    description: Register feature
    depends_on: [wait-init-pipeline]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/sdlc/features"
    body:
      slug: "{{ .vars.feature_slug }}"
      title: "Manage Todos"

  generate-spec:
    description: Agent writes the Spec
    depends_on: [create-feature]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/builds"
    body:
      prompt: "/spec-feature {{ .vars.feature_slug }}"
      auto_commit: true
      auto_push: true
      git_clone_url: "https://git.threesix.ai/jordan/{{ .outputs.create-project.project_id }}.git"
    outputs:
      - build_id: .data.task_id

  wait-spec:
     description: Wait for spec generation
     depends_on: [generate-spec]
     action: shell
     command: |
       for i in {1..60}; do
         STATUS=$(curl -s "$RDEV_API_URL/builds/{{ .outputs.generate-spec.build_id }}" -H "X-API-Key: $RDEV_API_KEY" | jq -r '.data.status // .status')
         if [ "$STATUS" == "completed" ]; then exit 0; fi
         if [ "$STATUS" == "failed" ]; then exit 1; fi
         sleep 5
       done
       exit 1

  approve-spec:
    description: Approve the Spec artifact
    depends_on: [wait-spec]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/sdlc/features/{{ .vars.feature_slug }}/artifacts/spec/approve"
    body: {}

  generate-design:
    description: Agent writes the Technical Design
    depends_on: [approve-spec]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/builds"
    body:
      prompt: "/design-feature {{ .vars.feature_slug }}"
      auto_commit: true
      auto_push: true
      git_clone_url: "https://git.threesix.ai/jordan/{{ .outputs.create-project.project_id }}.git"
    outputs:
      - build_id: .data.task_id

  wait-design:
     description: Wait for design generation
     depends_on: [generate-design]
     action: shell
     command: |
       for i in {1..60}; do
         STATUS=$(curl -s "$RDEV_API_URL/builds/{{ .outputs.generate-design.build_id }}" -H "X-API-Key: $RDEV_API_KEY" | jq -r '.data.status // .status')
         if [ "$STATUS" == "completed" ]; then exit 0; fi
         if [ "$STATUS" == "failed" ]; then exit 1; fi
         sleep 5
       done
       exit 1

  approve-design:
    description: Approve the Design artifact
    depends_on: [wait-design]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/sdlc/features/{{ .vars.feature_slug }}/artifacts/design/approve"
    body: {}

  # --- Phase 3: Implementation ---
  # We manually inject tasks to ensure deterministic IDs for the runner, simulating a PM refining the plan
  create-task-backend:
    description: Create backend implementation task
    depends_on: [approve-design]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/sdlc/features/{{ .vars.feature_slug }}/tasks"
    body:
      title: "Implement Todo domain model and in-memory handler"
    outputs:
      - task_id: .data.id

  implement-backend:
    description: Agent writes the Golang code
    depends_on: [create-task-backend]
    action: api
    method: POST
    endpoint: "/projects/{{ .outputs.create-project.project_id }}/builds"
    body:
      prompt: "/implement-task {{ .vars.feature_slug }} {{ .outputs.create-task-backend.task_id }}"
      auto_commit: true
      auto_push: true
      git_clone_url: "https://git.threesix.ai/jordan/{{ .outputs.create-project.project_id }}.git"
    outputs:
      - build_id: .data.task_id

  wait-implementation:
     description: Wait for code generation
     depends_on: [implement-backend]
     action: shell
     command: |
       for i in {1..120}; do
         STATUS=$(curl -s "$RDEV_API_URL/builds/{{ .outputs.implement-backend.build_id }}" -H "X-API-Key: $RDEV_API_KEY" | jq -r '.data.status // .status')
         if [ "$STATUS" == "completed" ]; then exit 0; fi
         if [ "$STATUS" == "failed" ]; then exit 1; fi
         sleep 5
       done
       exit 1

  wait-deploy:
    description: Wait for CI/CD to deploy the new feature
    depends_on: [wait-implementation]
    action: wait_pipeline
    project_id: "{{ .outputs.create-project.project_id }}"
    max_attempts: 60

  # --- Phase 4: Verification ---
  verify-crud:
    description: Functional test of the new endpoint
    depends_on: [wait-deploy]
    action: shell
    command: |
      # Give the pod a moment to settle
      sleep 10
      DOMAIN="{{ .outputs.create-project.domain }}"
      echo "Testing https://$DOMAIN/api/todos..."
      
      # 1. Create
      curl -s -X POST "https://$DOMAIN/api/todos" -d '{"title":"Test Todo"}' -H "Content-Type: application/json"
      
      # 2. List and verify
      RESP=$(curl -s "https://$DOMAIN/api/todos")
      if echo "$RESP" | grep -q "Test Todo"; then
        echo "SUCCESS: Found created todo"
        exit 0
      else
        echo "FAILURE: Todo not found in response: $RESP"
        exit 1
      fi

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