import logging
from django.apps import AppConfig

logger = logging.getLogger(__name__)


class PrintingConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'printing'
    verbose_name = '印染管理'

    def ready(self):
        """注册信号处理函数"""
        from stateflow.signals import process_completed, state_advanced
        from .models import PrintingJob, PrintingOrder
        from . import handlers
        from .signals import printing_job_production_completed, printing_order_created
        
        # 监听 PrintingJob 流程完成信号
        process_completed.connect(
            handlers.on_printing_job_process_completed,
            sender=PrintingJob
        )

        # 监听 PrintingJob 状态推进信号（每次推进都会触发）
        state_advanced.connect(
            handlers.on_printing_job_state_advanced,
            sender=PrintingJob
        )

        # 监听 PrintingOrder 创建事件（领域 signal）
        printing_order_created.connect(
            handlers.on_printing_order_created,
            sender=PrintingOrder,
            dispatch_uid="printing.on_printing_order_created",
        )
        printing_job_production_completed.connect(
            handlers.on_printing_job_production_completed,
            sender=PrintingJob,
            dispatch_uid="printing.on_printing_job_production_completed",
        )
        
        logger.info(
            f'[printing.apps] 已注册 process_completed 信号处理器, '
            f'sender={PrintingJob}, handler={handlers.on_printing_job_process_completed}'
        )
        logger.info(
            f'[printing.apps] 已注册 state_advanced 信号处理器, '
            f'sender={PrintingJob}, handler={handlers.on_printing_job_state_advanced}'
        )
        logger.info(
            f'[printing.apps] 已注册 printing_order_created 信号处理器, '
            f'sender={PrintingOrder}, handler={handlers.on_printing_order_created}'
        )
        logger.info(
            f'[printing.apps] 已注册 printing_job_production_completed 信号处理器, '
            f'sender={PrintingJob}, handler={handlers.on_printing_job_production_completed}'
        )
