"""Data quality agent"""
from sqlalchemy.orm import Session
from datetime import datetime, timedelta
import logging
from app.agents.base import BaseAgent
from app.core.db.repositories.match_repository import MatchRepository
from app.core.db.models.match import MatchStatus
from app.core.db.models.feature import Feature

logger = logging.getLogger(__name__)


class QualityAgent(BaseAgent):
    """Agent for data quality checks"""
    
    def __init__(self, db: Session):
        super().__init__(db, "QualityAgent")
        self.match_repo = MatchRepository(db)
    
    def run(self) -> bool:
        """Run quality checks"""
        run_id = self.start_run()
        issues = []
        
        try:
            # Check for missing scores in finished matches
            finished_matches = self.match_repo.get_finished(limit=1000)
            missing_scores = [
                m for m in finished_matches
                if m.home_score is None or m.away_score is None
            ]
            
            if missing_scores:
                issues.append(f"Found {len(missing_scores)} finished matches without scores")
            
            # Check for duplicate matches
            all_matches = self.match_repo.get_upcoming(limit=10000)
            source_ids = [m.source_id for m in all_matches if m.source_id]
            duplicates = len(source_ids) - len(set(source_ids))
            
            if duplicates > 0:
                issues.append(f"Found {duplicates} potential duplicate matches")
            
            # Check for matches without features
            matches_without_features = self.db.query(Match).outerjoin(Feature).filter(
                Feature.id.is_(None),
                Match.status == MatchStatus.SCHEDULED.value,
                Match.match_date >= datetime.now(),
                Match.match_date <= datetime.now() + timedelta(days=7)
            ).count()
            
            if matches_without_features > 0:
                issues.append(f"Found {matches_without_features} upcoming matches without features")
            
            # Check for old matches without results
            old_scheduled = self.db.query(Match).filter(
                Match.status == MatchStatus.SCHEDULED.value,
                Match.match_date < datetime.now() - timedelta(days=1)
            ).count()
            
            if old_scheduled > 0:
                issues.append(f"Found {old_scheduled} old matches still marked as scheduled")
            
            # Log issues
            if issues:
                logs = "\n".join(issues)
                logger.warning(f"Quality issues found:\n{logs}")
                self.finish_run(success=True, logs=logs)
            else:
                logs = "No quality issues found"
                logger.info(logs)
                self.finish_run(success=True, logs=logs)
            
            return True
            
        except Exception as e:
            error_msg = str(e)
            logger.error(f"Quality check failed: {error_msg}", exc_info=True)
            self.finish_run(success=False, error=error_msg)
            return False

