# -*- coding: utf-8 -*-
"""
部署验证工具

提供检测题目是否可部署的功能
"""
import os
from typing import Optional


def check_challenge_deployable(output_dir: Optional[str]) -> Optional[bool]:
    """
    检查题目是否可部署
    
    通过检查输出目录中是否存在 Docker 配置文件来判断：
    - docker-compose.yml 或 docker-compose.yaml
    - Dockerfile
    
    Args:
        output_dir: 题目输出目录路径
        
    Returns:
        Optional[bool]: 
            - True: 可部署（找到 Docker 配置文件）
            - False: 不可部署（目录存在但无 Docker 配置文件）
            - None: 无法判断（目录不存在或其他错误）
    """
    if not output_dir:
        return None
    
    if not os.path.exists(output_dir):
        return None
    
    # 递归查找 Docker 配置文件
    docker_files = _find_docker_files(output_dir)
    
    # 如果找到 docker-compose.yml 或 Dockerfile，则认为可部署
    if docker_files.get('docker_compose') or docker_files.get('dockerfile'):
        return True
    
    # 目录存在但没有 Docker 配置文件，不可部署
    return False


def _find_docker_files(directory: str) -> dict:
    """
    递归查找 Docker 配置文件
    
    Args:
        directory: 要搜索的目录路径
        
    Returns:
        dict: 包含找到的文件路径的字典
            - 'docker_compose': docker-compose.yml 或 docker-compose.yaml 的路径
            - 'dockerfile': Dockerfile 的路径
    """
    result = {}
    
    for root, _, files in os.walk(directory):
        for file in files:
            if file == 'docker-compose.yml' or file == 'docker-compose.yaml':
                result['docker_compose'] = os.path.join(root, file)
            elif file == 'Dockerfile':
                if 'dockerfile' not in result:
                    result['dockerfile'] = os.path.join(root, file)
    
    return result
















