"""Local file provider - for .msg and .eml files."""
import logging
from typing import List, Dict, Optional

from .base import BaseProvider, Email
from src.calibration.local_file_parser import LocalFileParser

logger = logging.getLogger(__name__)


class LocalFileProvider(BaseProvider):
    """
    Local file provider for .msg and .eml files.

    Supports:
    - Single directory with email files
    - Nested directory structure
    - Mixed .msg (Outlook) and .eml formats

    Uses the same Email data model and BaseProvider interface as other providers.
    """

    def __init__(self, directory_path: str):
        """
        Initialize local file provider.

        Args:
            directory_path: Path to directory containing email files
        """
        super().__init__(name="local_file")
        self.parser = LocalFileParser(directory_path)
        self.connected = False

    def connect(self, credentials: Dict = None) -> bool:
        """
        Connect to local file provider (no auth needed).

        Args:
            credentials: Not used for local files

        Returns:
            Always True for local files
        """
        self.connected = True
        logger.info("Connected to local file provider")
        return True

    def disconnect(self) -> bool:
        """Disconnect from local file provider."""
        self.connected = False
        logger.info("Disconnected from local file provider")
        return True

    def fetch_emails(self, limit: int = None, filters: Dict = None) -> List[Email]:
        """
        Fetch emails from local directory.

        Args:
            limit: Maximum number of emails to fetch
            filters: Optional filters (not implemented for local files)

        Returns:
            List of Email objects
        """
        if not self.connected:
            logger.warning("Not connected to local file provider")
            return []

        logger.info(f"Fetching up to {limit or 'all'} emails from local files")
        emails = self.parser.parse_emails(limit=limit)
        logger.info(f"Fetched {len(emails)} emails")

        return emails

    def update_labels(self, email_id: str, labels: List[str]) -> bool:
        """
        Update labels (not supported for local files).

        Args:
            email_id: Email ID
            labels: List of labels to add

        Returns:
            Always False for local files
        """
        logger.warning("Label updates not supported for local file provider")
        return False

    def batch_update(self, updates: List[Dict]) -> bool:
        """
        Batch update (not supported for local files).

        Args:
            updates: List of update operations

        Returns:
            Always False for local files
        """
        logger.warning("Batch updates not supported for local file provider")
        return False

    def is_connected(self) -> bool:
        """Check if provider is connected."""
        return self.connected
