template:
  id: story-template-v2
  name: Story Document
  version: 2.0
  output:
    format: markdown
    filename: docs/stories/{{epic_num}}.{{story_num}}.{{story_title_short}}.md
    title: "Story {{epic_num}}.{{story_num}}: {{story_title_short}}"

workflow:
  mode: interactive
  elicitation: advanced-elicitation

agent_config:
  editable_sections:
    - Status
    - Story
    - Acceptance Criteria
    - CodeRabbit Integration
    - Tasks / Subtasks
    - Dev Notes
    - Testing
    - Change Log

sections:
  - id: community-origin
    title: Community Origin
    type: optional
    instruction: |
      **For stories originating from community ideas:**
      Complete this section if this story was created from a community idea discussion.
      Leave blank for internally-planned stories.
    template: |
      **Discussion URL**: {{discussion_url}}
      **Author**: @{{author_username}}
      **Approved Date**: {{approved_date}}
      **Approved By**: @{{approver_username}}
    owner: scrum-master
    editors: [scrum-master, po]

  - id: status
    title: Status
    type: choice
    choices: [Draft, Approved, InProgress, Review, Done]
    instruction: Select the current status of the story
    owner: scrum-master
    editors: [scrum-master, dev-agent]

  # Story 11.1: Dynamic Executor Assignment (Projeto Bob)
  - id: executor-assignment
    title: Executor Assignment
    type: metadata
    required: true
    instruction: |
      **REQUIRED FIELDS** - Automatically populated by PM during epic creation.

      Uses dynamic executor assignment based on story type analysis.
      See: PRD AIOS v2.0 "Projeto Bob" - Section 5

      **Work Type → Executor Mapping:**
      | Work Type | Executor | Quality Gate |
      |-----------|----------|--------------|
      | Code/Features/Logic | @dev | @architect |
      | Schema/DB/RLS/Migrations | @data-engineer | @dev |
      | Infra/CI/CD/Deploy | @devops | @architect |
      | Design/UI Components | @ux-design-expert | @dev |
      | Research/Investigation | @analyst | @pm |
      | Architecture Decisions | @architect | @pm |

      **VALIDATION RULES:**
      - executor != quality_gate (ALWAYS)
      - quality_gate_tools must not be empty
    template: |
      executor: "{{executor}}"
      quality_gate: "{{quality_gate}}"
      quality_gate_tools: {{quality_gate_tools}}
    validation:
      - rule: executor != quality_gate
        error: "Executor cannot be the same as Quality Gate"
      - rule: quality_gate_tools.length > 0
        error: "Quality gate tools cannot be empty"
    owner: pm
    editors: [pm, scrum-master]

  - id: story
    title: Story
    type: template-text
    template: |
      **As a** {{role}},
      **I want** {{action}},
      **so that** {{benefit}}
    instruction: Define the user story using the standard format with role, action, and benefit
    elicit: true
    owner: scrum-master
    editors: [scrum-master]
    
  - id: acceptance-criteria
    title: Acceptance Criteria
    type: numbered-list
    instruction: Copy the acceptance criteria numbered list from the epic file
    elicit: true
    owner: scrum-master
    editors: [scrum-master]

  - id: coderabbit-integration
    title: 🤖 CodeRabbit Integration
    # CONDITIONAL SECTION - Story 6.3.3 Self-Healing Integration
    conditional:
      config_path: ".aios-core/core-config.yaml"
      config_key: "coderabbit_integration.enabled"
      behavior:
        when_true: render_full_section
        when_false: render_skip_notice
      skip_notice: |
        > **CodeRabbit Integration**: Disabled
        >
        > CodeRabbit CLI is not enabled in `core-config.yaml`.
        > Quality validation will use manual review process only.
        > To enable, set `coderabbit_integration.enabled: true` in core-config.yaml
    instruction: |
      **CONDITIONAL SECTION** - Check core-config.yaml `coderabbit_integration.enabled`

      **IF enabled=true:**
        Populate this section with story type analysis, quality gates,
        specialized agent assignments, and self-healing configuration.
        This section is auto-generated during story creation.

      **IF enabled=false:**
        SKIP this entire section. Render only the skip_notice above.
        Quality validation falls back to manual review process.

      Based on story type analysis, document the predicted quality gates and
      specialized agent assignments. This section is auto-generated during story
      creation but can be manually adjusted if needed.
    elicit: true
    owner: scrum-master
    editors: [scrum-master, po]
    sections:
      - id: story-type-analysis
        title: Story Type Analysis
        instruction: |
          Categorize story based on technical characteristics:
          - Database: Schema changes, migrations, RLS policies
          - API: REST endpoints, services, business logic
          - Frontend: Components, UX, accessibility
          - Deployment: CI/CD, infrastructure, configuration
          - Security: Authentication, authorization, data protection
          - Architecture: Patterns, scalability, cross-cutting concerns
          - Integration: External systems, APIs, data sync
        template: |
          **Primary Type**: {{story_type}}
          **Secondary Type(s)**: {{story_type_secondary}}
          **Complexity**: {{complexity_level}}
        elicit: true
        owner: scrum-master
        editors: [scrum-master]

      - id: specialized-agents
        title: Specialized Agent Assignment
        instruction: |
          List which specialized agents should be involved based on story type:

          **Always Required**:
          - @dev: Pre-commit reviews (all stories)

          **Conditional Assignment**:
          - @db-sage: Database/SQL changes, schema modifications, RLS policies
          - @github-devops: PR creation, deployment, CI/CD configuration
          - @architect: Architecture decisions, new patterns, scalability
          - @ux-expert: Frontend/UX work, accessibility (WCAG 2.1 AA)
          - @qa: Story validation, test coverage verification
        template: |
          **Primary Agents**:
          - {{primary_agent_1}}
          - {{primary_agent_2}}

          **Supporting Agents**:
          - {{supporting_agent_1}}
          - {{supporting_agent_2}}
        elicit: true
        owner: scrum-master
        editors: [scrum-master, po]

      - id: quality-gates
        title: Quality Gate Tasks
        instruction: |
          Predicted CodeRabbit validation checkpoints based on story type and risk:

          **Pre-Commit** (@dev - always required):
          - Before marking story complete
          - Focus: Code quality, security basics, patterns

          **Pre-PR** (@github-devops - when creating PR):
          - Before creating pull request
          - Focus: Integration safety, backward compatibility

          **Pre-Deployment** (@github-devops - production stories only):
          - Before deploying to production
          - Focus: Security scan, configuration validation, rollback readiness
        template: |
          - [ ] Pre-Commit (@dev): Run before marking story complete
          - [ ] Pre-PR (@github-devops): Run before creating pull request
          - [ ] Pre-Deployment (@github-devops): Run before production deploy (if applicable)
        elicit: true
        owner: scrum-master
        editors: [scrum-master]

      - id: self-healing-config
        title: Self-Healing Configuration
        instruction: |
          **Story 6.3.3 - Self-Healing Integration**

          Document the expected self-healing behavior for this story based on
          agent-specific configurations in core-config.yaml.

          **Severity Handling by Agent**:

          | Severity | @dev (light) | @qa (full) | @github-devops (check) |
          |----------|--------------|------------|------------------------|
          | CRITICAL | auto_fix (2 iter, 15min) | auto_fix (3 iter, 30min) | report_only |
          | HIGH     | document_only | auto_fix | report_only |
          | MEDIUM   | ignore | document_as_debt | report_only |
          | LOW      | ignore | ignore | ignore |

          **Workflow Triggers**:
          - @dev: story_completion (before marking "Ready for Review")
          - @qa: review_start (at beginning of QA review)
          - @github-devops: pr_creation (before creating PR)
        template: |
          **Expected Self-Healing**:
          - Primary Agent: @{{primary_agent}} ({{self_healing_type}} mode)
          - Max Iterations: {{max_iterations}}
          - Timeout: {{timeout_minutes}} minutes
          - Severity Filter: {{severity_filter}}

          **Predicted Behavior**:
          - CRITICAL issues: {{critical_behavior}}
          - HIGH issues: {{high_behavior}}
        elicit: true
        owner: scrum-master
        editors: [scrum-master]

      - id: focus-areas
        title: CodeRabbit Focus Areas
        instruction: |
          Specific patterns for CodeRabbit to validate based on story type:

          **Database Stories**:
          - Service filters: .eq('service', 'ttcx') on all queries
          - Schema compliance: Foreign keys, indexes, constraints
          - RLS policies: Row-level security properly configured

          **API Stories**:
          - Error handling: Try-catch blocks, proper error responses
          - Security: Input validation, authentication, authorization
          - Validation: Request/response schema validation

          **Frontend Stories**:
          - Accessibility: WCAG 2.1 AA compliance (semantic HTML, ARIA, keyboard nav)
          - Performance: Component optimization, lazy loading
          - Responsive design: Mobile-first, breakpoints

          **Deployment Stories**:
          - CI/CD: Pipeline configuration, test coverage
          - Secrets management: No hardcoded credentials
          - Environment config: Proper variable usage

          **Security Stories**:
          - OWASP Top 10: Injection, XSS, authentication
          - Timing attacks: Constant-time comparisons
          - Data protection: Encryption, sanitization
        template: |
          **Primary Focus**:
          - {{focus_area_1}}
          - {{focus_area_2}}

          **Secondary Focus**:
          - {{focus_area_3}}
          - {{focus_area_4}}
        elicit: true
        owner: scrum-master
        editors: [scrum-master]

  - id: tasks-subtasks
    title: Tasks / Subtasks
    type: bullet-list
    instruction: |
      Break down the story into specific tasks and subtasks needed for implementation.
      Reference applicable acceptance criteria numbers where relevant.
    template: |
      - [ ] Task 1 (AC: # if applicable)
        - [ ] Subtask1.1...
      - [ ] Task 2 (AC: # if applicable)
        - [ ] Subtask 2.1...
      - [ ] Task 3 (AC: # if applicable)
        - [ ] Subtask 3.1...
    elicit: true
    owner: scrum-master
    editors: [scrum-master, dev-agent]
    
  - id: dev-notes
    title: Dev Notes
    instruction: |
      Populate relevant information, only what was pulled from actual artifacts from docs folder, relevant to this story:
      - Do not invent information
      - If known add Relevant Source Tree info that relates to this story
      - If there were important notes from previous story that are relevant to this one, include them here
      - Put enough information in this section so that the dev agent should NEVER need to read the architecture documents, these notes along with the tasks and subtasks must give the Dev Agent the complete context it needs to comprehend with the least amount of overhead the information to complete the story, meeting all AC and completing all tasks+subtasks
    elicit: true
    owner: scrum-master
    editors: [scrum-master]
    sections:
      - id: testing-standards
        title: Testing
        instruction: |
          List Relevant Testing Standards from Architecture the Developer needs to conform to:
          - Test file location
          - Test standards
          - Testing frameworks and patterns to use
          - Any specific testing requirements for this story
        elicit: true
        owner: scrum-master
        editors: [scrum-master]
        
  - id: change-log
    title: Change Log
    type: table
    columns: [Date, Version, Description, Author]
    instruction: Track changes made to this story document
    owner: scrum-master
    editors: [scrum-master, dev-agent, qa-agent]
    
  - id: dev-agent-record
    title: Dev Agent Record
    instruction: This section is populated by the development agent during implementation
    owner: dev-agent
    editors: [dev-agent]
    sections:
      - id: agent-model
        title: Agent Model Used
        template: "{{agent_model_name_version}}"
        instruction: Record the specific AI agent model and version used for development
        owner: dev-agent
        editors: [dev-agent]
        
      - id: debug-log-references
        title: Debug Log References
        instruction: Reference any debug logs or traces generated during development
        owner: dev-agent
        editors: [dev-agent]
        
      - id: completion-notes
        title: Completion Notes List
        instruction: Notes about the completion of tasks and any issues encountered
        owner: dev-agent
        editors: [dev-agent]
        
      - id: file-list
        title: File List
        instruction: List all files created, modified, or affected during story implementation
        owner: dev-agent
        editors: [dev-agent]
        
  - id: qa-results
    title: QA Results
    instruction: Results from QA Agent QA review of the completed story implementation
    owner: qa-agent
    editors: [qa-agent] 