name: Refactor
description: Improve code quality without changing behaviour
category: development

variables:
  target:
    description: Code to refactor (file, class, module)
    required: true
  goal:
    description: What improvement to achieve
    required: false

guidelines:
  - Ensure tests exist before refactoring
  - Make small, incremental changes
  - Commit frequently
  - Run tests after each change

phases:
  - name: Assessment
    description: Understand current state
    tasks:
      - Review existing code
      - Identify code smells
      - Check test coverage
      - Document current behaviour
      - List specific improvements

  - name: Preparation
    description: Set up for safe refactoring
    tasks:
      - Ensure tests pass
      - Add tests for uncovered behaviour
      - Create feature branch
      - Set up automated checks
      - Document expected behaviour

  - name: Extraction
    description: Extract and simplify
    tasks:
      - Extract methods/functions
      - Extract classes if too large
      - Remove duplication
      - Simplify conditionals
      - Improve naming

  - name: Structure
    description: Improve organisation
    tasks:
      - Reorganise file structure
      - Update namespaces
      - Improve class relationships
      - Apply design patterns
      - Add interfaces where useful

  - name: Polish
    description: Final improvements
    tasks:
      - Add type hints
      - Improve documentation
      - Remove dead code
      - Optimise performance
      - Ensure style consistency

  - name: Verification
    description: Confirm behaviour unchanged
    tasks:
      - Run full test suite
      - Manual testing
      - Performance comparison
      - Code review
      - Merge and monitor
