#!/usr/bin/env python3
"""
Script to add production workflows with approval gates to parameter-based CircleCI configs
"""

from pathlib import Path

# Parameter-based services that need prod workflows added
PARAMETER_SERVICES = [
    "tbi-acc-cron-service",
    "tbi-acc-gimmonix-automation",
    "tbi-acc-hotel-service-connector",
    "tbi-acc-hotel-service",
    "tbi-acc-mybff",
    "tbi-acc-mybooking-frontend",
    "tbi-acc-package-cron",
    "tbi-acc-package-service",
    "tbi-acc-sabreapi",
    "tbi-acc-travelbrands",
    "tbi-acc-travelbrandsagent",
]

def add_prod_workflow(service_name):
    """Add production workflow with approval gates to a parameter-based config"""
    config_path = Path(f"prod/{service_name}/.circleci/config.yml")
    
    if not config_path.exists():
        print(f"❌ Config not found: {config_path}")
        return False
    
    content = config_path.read_text()
    
    # Check if prod workflow already exists
    if "build-deploy-prod:" in content or "prod-pipeline:" in content:
        print(f"✓ {service_name} already has prod workflow")
        return True
    
    print(f"→ Adding prod workflow to {service_name}...")
    
    # Add prod workflow at the end (before the file ends)
    prod_workflow = '''
  build-deploy-prod:
    jobs:
      - lint:
          context: devops-mgt-prod
          filters:
            branches:
              only:
                - master
      - test:
          context: devops-mgt-prod
          requires:
            - lint
          filters:
            branches:
              only:
                - master
      - hold-build:
          type: approval
          requires:
            - test
          filters:
            branches:
              only:
                - master
      - build-and-push:
          name: build-prod
          environment: prod
          context: devops-mgt-prod
          requires:
            - hold-build
          filters:
            branches:
              only:
                - master
      - hold-deploy:
          type: approval
          requires:
            - build-prod
          filters:
            branches:
              only:
                - master
      - deploy:
          name: deploy-prod
          environment: prod
          cluster_name: tbi-prod-eks
          context: devops-mgt-prod
          requires:
            - hold-deploy
          filters:
            branches:
              only:
                - master
'''
    
    # Append the prod workflow
    content = content.rstrip() + '\n' + prod_workflow
    
    # Write back
    config_path.write_text(content)
    print(f"✓ {service_name} prod workflow added successfully")
    return True

def main():
    print(f"Adding prod workflows to {len(PARAMETER_SERVICES)} parameter-based configs...")
    print()
    
    success_count = 0
    for service in PARAMETER_SERVICES:
        if add_prod_workflow(service):
            success_count += 1
        print()
    
    print(f"✓ Updated {success_count}/{len(PARAMETER_SERVICES)} configs")

if __name__ == "__main__":
    main()
