"""
Unified service for invoice movement operations.
This service delegates to specialized handlers for each import type.
"""

import logging
from sqlalchemy.orm import Session
from typing import List

from .schemas import (
    ImportTemporaryFilter,
    ImportDefinitiveFilter,
    ImportRepairFilter,
    ExportFilter,
    MovementItem,
    MovementItemDetailed
)
from .services.temporary import TemporaryImportService
from .services.definitive import DefinitiveImportService
from .services.repair import RepairImportService
from .services.export import ExportService

logger = logging.getLogger(__name__)


class MovementService:
    """
    Unified service for handling all types of movements.
    Delegates to specialized services for each movement type.
    """
    
    def __init__(self):
        self.temporary_service = TemporaryImportService()
        self.definitive_service = DefinitiveImportService()
        self.repair_service = RepairImportService()
        self.export_service = ExportService()
    
    # ===== TEMPORARY IMPORTS =====
    
    def get_temporary_import_movements(
        self,
        db: Session,
        filters: ImportTemporaryFilter
    ) -> List[MovementItem]:
        """Get temporary import movements (normal mode - grouped by invoice)."""
        return self.temporary_service.get_movements(db, filters)
    
    def get_temporary_import_movements_detailed(
        self,
        db: Session,
        filters: ImportTemporaryFilter
    ) -> List[MovementItemDetailed]:
        """Get temporary import movements (detailed mode - line by line)."""
        return self.temporary_service.get_movements_detailed(db, filters)
    
    # ===== DEFINITIVE IMPORTS =====
    
    def get_definitive_import_movements(
        self,
        db: Session,
        filters: ImportDefinitiveFilter
    ) -> List[MovementItem]:
        """Get definitive import movements (normal mode - grouped by invoice)."""
        return self.definitive_service.get_movements(db, filters)
    
    def get_definitive_import_movements_detailed(
        self,
        db: Session,
        filters: ImportDefinitiveFilter
    ) -> List[MovementItemDetailed]:
        """Get definitive import movements (detailed mode - line by line)."""
        return self.definitive_service.get_movements_detailed(db, filters)
    
    # ===== REPAIR IMPORTS =====
    
    def get_repair_import_movements(
        self,
        db: Session,
        filters: ImportRepairFilter
    ) -> List[MovementItem]:
        """Get repair import movements (normal mode - grouped by invoice)."""
        return self.repair_service.get_movements(db, filters)
    
    def get_repair_import_movements_detailed(
        self,
        db: Session,
        filters: ImportRepairFilter
    ) -> List[MovementItemDetailed]:
        """Get repair import movements (detailed mode - line by line)."""
        return self.repair_service.get_movements_detailed(db, filters)
    
    # ===== EXPORTS =====
    
    def get_export_movements(
        self,
        db: Session,
        filters: ExportFilter
    ) -> List[MovementItem]:
        """Get export movements (normal mode - grouped by invoice)."""
        return self.export_service.get_movements(db, filters)
    
    def get_export_movements_detailed(
        self,
        db: Session,
        filters: ExportFilter
    ) -> List[MovementItemDetailed]:
        """Get export movements (detailed mode - line by line)."""
        return self.export_service.get_movements_detailed(db, filters)


# Singleton instance
movement_service = MovementService()
