#!/usr/bin/env python3
"""
File monitoring and discovery for NEFT Inward files.
Scans SFTP directories for new files across multiple banks.

Filename convention handled:
    DDMMYYYY_HH_NEFT_INWARD.TXT
Example:
    06032026_14_NEFT_INWARD.TXT
"""

import re
from typing import List, Tuple, Dict
from logging_config import get_logger
from config import get_config
from .sftp_client import SFTPClient

logger = get_logger(__name__)


class FileMonitor:
    """Monitors SFTP for new NEFT Outward files."""

    def __init__(self, sftp_client: SFTPClient = None):
        """
        Initialize file monitor.

        Args:
            sftp_client: SFTPClient instance (optional)
        """
        self.config = get_config()
        self.sftp_client = sftp_client or SFTPClient()

    def scan_for_new_files(self, processed_filenames: List[str]) -> List[Tuple[str, str, str]]:
        """
        Scan all bank directories for new NEFT files.

        Args:
            processed_filenames: List of already processed filenames to skip

        Returns:
            List of (filename, bankcode, full_remote_path) tuples
        """
        new_files: List[Tuple[str, str, str]] = []

        for bank_code in self.config.bank_codes:
            # Adjust subfolder name here if required (e.g., 'NEFT_INWARD' or other)
            remote_path = f"{self.config.sftp_base_path}/{bank_code}/NEFT"

            # Match any NEFT inward file for any date/hour
            files = self.sftp_client.list_files(remote_path, pattern='*_NEFT_OUTWARD.TXT')

            for filename in files:
                if filename not in processed_filenames:
                    full_path = f"{remote_path}/{filename}"
                    new_files.append((filename, bank_code, full_path))
                    logger.info(f"Found new NEFT file: {filename} (bank: {bank_code})")
                else:
                    logger.debug(f"Skipping already processed NEFT file: {filename}")

        logger.info(f"NEFT scan complete: Found {len(new_files)} new files")
        return new_files

    @staticmethod
    def parse_filename(filename: str) -> Dict[str, str]:
        """
        Parse NEFT filename to extract metadata.

        Expected format:
            DDMMYYYY_HH_NEFT_OUTWARD.TXT
        Example:
            06032026_14_NEFT_OUTWARD.TXT

        Args:
            filename: Filename to parse

        Returns:
            Dictionary with extracted metadata or empty dict if parse fails
        """
        # Groups: DD, MM, YYYY, HH
        pattern = r'^(\d{2})(\d{2})(\d{4})_(\d{2})_NEFT_OUTWARD\.TXT$'
        match = re.match(pattern, filename, flags=re.IGNORECASE)

        if not match:
            logger.warning(f"Could not parse NEFT filename: {filename}")
            return {}

        day, month, year, hour = match.groups()

        return {
            'filename': filename,
            'day': day,
            'month': month,
            'year': year,
            'hour': hour,
            'timestamp': f"{day}/{month}/{year} {hour}:00:00"
        }

    def __enter__(self):
        """Context manager entry."""
        if not self.sftp_client.sftp:
            self.sftp_client.connect()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Context manager exit."""
        self.sftp_client.disconnect()

