"""
DRM 카탈로그 동기화 라우트
backend/routes/drm_sync.py
"""

from flask import Blueprint, request, jsonify
from backend.services.drm_catalog_sync import sync_from_drm, check_drm_repository

drm_sync_bp = Blueprint('drm_sync', __name__, url_prefix='/drm')


@drm_sync_bp.route('/check', methods=['POST'])
def check_repository():
    """DRM 리포지토리 상태 확인"""
    try:
        data = request.get_json() or {}
        repository_path = data.get('repository_path')
        
        if not repository_path:
            return jsonify({
                'success': False,
                'message': 'repository_path가 필요합니다'
            }), 400
        
        info = check_drm_repository(repository_path)
        
        return jsonify({
            'success': info.get('exists', False),
            'info': info
        })
        
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'오류: {str(e)}'
        }), 500


@drm_sync_bp.route('/sync', methods=['POST'])
def sync_repository():
    """DRM 리포지토리에서 펌웨어 동기화"""
    try:
        data = request.get_json() or {}
        repository_path = data.get('repository_path')
        model = data.get('model', 'PowerEdge R750')
        
        if not repository_path:
            return jsonify({
                'success': False,
                'message': 'repository_path가 필요합니다'
            }), 400
        
        result = sync_from_drm(repository_path, model)
        
        return jsonify(result)
        
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'오류: {str(e)}'
        }), 500
