import json
import logging
from typing import Dict, List, Optional
from dataclasses import dataclass
from langchain_ollama import ChatOllama
from langchain.prompts import PromptTemplate
from langchain.schema import HumanMessage

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@dataclass
class Course:
    """Data class for course information"""
    course_id: str
    name: str
    short_description: str
    description: str
    skill: str

@dataclass
class CourseMatch:
    """Data class for course match with scoring"""
    course_id: str
    course_name: str
    match_percentage: int
    reason: str

@dataclass
class DomainFilterResponse:
    """Data class for domain-aware filtering response"""
    domain: str
    field_prompt: str
    total_courses_input: int
    course_matches: List[CourseMatch]
    total_matching_courses: int
    processing_summary: str

class DomainAwareCourseFilter:
    """Domain-aware course filtering using LLM in batches"""
    
    def __init__(self, model_name: str = "gemma3:12b", batch_size: int = 30):
        """Initialize the domain-aware course filter"""
        try:
            self.llm = ChatOllama(
                model=model_name,
                num_ctx=128000,  # Use full 128K context
                temperature=0.1,
                num_predict=3000  # Allow longer responses for batch analysis
            )
            self.batch_size = batch_size
            logger.info(f"Domain-Aware Course Filter initialized with model: {model_name} (Batch size: {batch_size})")
        except Exception as e:
            logger.error(f"Failed to initialize Domain-Aware Course Filter: {e}")
            raise
    
    def clean_course_data(self, courses: List[Course]) -> tuple[List[Course], int]:
        """Keep all courses - no aggressive filtering"""
        
        # Just return all courses without any filtering
        logger.info(f"Using all courses without cleaning: {len(courses)} courses")
        return courses, 0
    
    def filter_courses_by_domain(self, domain: str, field_prompt: str, courses: List[Course]) -> DomainFilterResponse:
        """
        Main method: Filter courses using domain-aware LLM analysis in batches
        
        Args:
            domain: The identified domain (e.g., "Technology", "Business", "Healthcare")
            field_prompt: The specific field (e.g., "Frontend Development", "Sales Training")
            courses: List of all courses to filter
            
        Returns:
            DomainFilterResponse with matched courses and details
        """
        try:
            original_count = len(courses)
            
            # Step 1: Use all courses (no cleaning)
            cleaned_courses, removed_count = self.clean_course_data(courses)
            
            # Step 2: Process courses in batches using LLM
            all_matches = []
            batch_summaries = []
            
            total_batches = (len(cleaned_courses) + self.batch_size - 1) // self.batch_size
            
            for i in range(0, len(cleaned_courses), self.batch_size):
                batch = cleaned_courses[i:i+self.batch_size]
                batch_num = (i // self.batch_size) + 1
                
                logger.info(f"Processing batch {batch_num}/{total_batches}: courses {i+1}-{min(i+self.batch_size, len(cleaned_courses))}")
                
                # Analyze this batch with LLM
                batch_matches = self._analyze_batch_with_domain(domain, field_prompt, batch, batch_num)
                all_matches.extend(batch_matches)
                
                batch_summaries.append(f"Batch {batch_num}: {len(batch_matches)} matches from {len(batch)} courses")
                logger.info(f"Batch {batch_num} results: {len(batch_matches)} matches")
            
            # Step 3: Sort matches by percentage (highest first)
            all_matches.sort(key=lambda x: x.match_percentage, reverse=True)
            
            # Step 4: Create response
            processing_summary = f"Processed {total_batches} batches. " + " | ".join(batch_summaries)
            
            response = DomainFilterResponse(
                domain=domain,
                field_prompt=field_prompt,
                total_courses_input=original_count,
                course_matches=all_matches,
                total_matching_courses=len(all_matches),
                processing_summary=processing_summary
            )
            
            logger.info(f"Final results: {original_count} → {len(all_matches)} courses")
            return response
            
        except Exception as e:
            logger.error(f"Error in domain-aware filtering: {e}")
            return DomainFilterResponse(
                domain=domain,
                field_prompt=field_prompt,
                total_courses_input=len(courses),
                course_matches=[],
                total_matching_courses=0,
                processing_summary=f"Error in filtering: {str(e)}"
            )
    
    def _analyze_batch_with_domain(self, domain: str, field_prompt: str, batch: List[Course], batch_num: int) -> List[CourseMatch]:
        """Analyze a batch of courses using LLM with domain context"""
        
        courses_text = self._format_batch_for_llm(batch)
        
        prompt_template = PromptTemplate(
            input_variables=["domain", "field_prompt", "courses_text", "batch_num"],
            template="""
            You are an EXPERT course curator specializing in the {domain} domain.
            
            DOMAIN: {domain}
            TARGET FIELD: {field_prompt}
            BATCH: {batch_num}
            
            TASK: Analyze each course below and determine if it's relevant for someone seeking "{field_prompt}" skills in the {domain} domain.
            
            EVALUATION CRITERIA:
            1. Course content directly relates to {field_prompt}
            2. Skills taught are applicable in {domain} field
            3. Course would help someone working in or entering {field_prompt}
            4. Content is professional-level, not basic computer literacy
            
            SCORING GUIDE:
            - 90-100%: Perfect match, essential for {field_prompt}
            - 80-89%: Very relevant, directly applicable
            - 70-79%: Somewhat relevant, useful supporting skills
            - Below 70%: Not relevant enough to recommend
            
            COURSES TO ANALYZE:
            {courses_text}
            
            RESPONSE FORMAT: Return ONLY a JSON array with this exact structure:
            [
                {{
                    "course_id": "123",
                    "course_name": "Exact Course Name",
                    "match_percentage": 95,
                    "reason": "Specific reason why this course matches {field_prompt}"
                }}
            ]
            
            IMPORTANT: 
            - Only include courses with match_percentage >= 70
            - If no courses meet the criteria, return an empty array: []
            - Be selective and strict in your evaluation
            - Ensure course_name matches exactly as provided
            """
        )
        
        try:
            formatted_prompt = prompt_template.format(
                domain=domain,
                field_prompt=field_prompt,
                courses_text=courses_text,
                batch_num=batch_num
            )
            
            response = self.llm.invoke([HumanMessage(content=formatted_prompt)])
            
            # Parse LLM response
            matches = self._parse_batch_response(response.content, batch)
            
            logger.info(f"Batch {batch_num}: LLM identified {len(matches)} matching courses")
            return matches
            
        except Exception as e:
            logger.error(f"Error in batch {batch_num} LLM analysis: {e}")
            return []
    
    def _format_batch_for_llm(self, batch: List[Course]) -> str:
        """Format a batch of courses for LLM input"""
        
        courses_formatted = []
        for course in batch:
            # Combine all course information
            course_info = f"ID: {course.course_id}\n"
            course_info += f"Name: {course.name}\n"
            
            if course.short_description and course.short_description != course.name:
                course_info += f"Short Description: {course.short_description}\n"
            
            if course.description:
                course_info += f"Description: {course.description}\n"
            
            if course.skill:
                course_info += f"Skills: {course.skill}\n"
            
            courses_formatted.append(course_info)
        
        return "\n" + "="*50 + "\n".join(courses_formatted) + "="*50
    
    def _parse_batch_response(self, response_content: str, original_batch: List[Course]) -> List[CourseMatch]:
        """Parse LLM response to extract course matches"""
        
        try:
            # Try to extract JSON from response
            import re
            json_match = re.search(r'\[.*\]', response_content, re.DOTALL)
            
            if json_match:
                parsed_response = json.loads(json_match.group())
                
                # Validate and convert to CourseMatch objects
                matches = []
                for item in parsed_response:
                    if self._validate_match_item(item):
                        match = CourseMatch(
                            course_id=str(item["course_id"]),
                            course_name=item["course_name"],
                            match_percentage=int(item["match_percentage"]),
                            reason=item["reason"]
                        )
                        matches.append(match)
                
                return matches
            
            # Fallback: try to extract information from text
            return self._fallback_parse_batch_response(response_content, original_batch)
            
        except json.JSONDecodeError as e:
            logger.warning(f"JSON parsing failed: {e}")
            return self._fallback_parse_batch_response(response_content, original_batch)
        
        except Exception as e:
            logger.error(f"Error parsing batch response: {e}")
            return []
    
    def _validate_match_item(self, item: dict) -> bool:
        """Validate that a match item has all required fields"""
        required_fields = ["course_id", "course_name", "match_percentage", "reason"]
        
        for field in required_fields:
            if field not in item:
                return False
        
        # Validate percentage is in valid range
        try:
            percentage = int(item["match_percentage"])
            if percentage < 70 or percentage > 100:
                return False
        except (ValueError, TypeError):
            return False
        
        return True
    
    def _fallback_parse_batch_response(self, response_content: str, original_batch: List[Course]) -> List[CourseMatch]:
        """Fallback parsing when JSON extraction fails"""
        
        # Simple fallback: look for course IDs mentioned positively in text
        matches = []
        
        for course in original_batch:
            if course.course_id in response_content and any(
                positive in response_content.lower() 
                for positive in ["relevant", "match", "good", "suitable", "recommend"]
            ):
                matches.append(CourseMatch(
                    course_id=course.course_id,
                    course_name=course.name,
                    match_percentage=75,  # Default reasonable score
                    reason="Identified as relevant through fallback text analysis"
                ))
        
        return matches
    
    def process_json_request(self, json_data: str) -> Dict:
        """
        Process JSON request and return JSON response
        
        Expected input format:
        {
            "domain": "Technology",
            "field_prompt": "Frontend Development",
            "courses": [...]
        }
        """
        
        try:
            request_data = json.loads(json_data)
            
            # Extract required fields
            domain = request_data.get('domain', '')
            field_prompt = request_data.get('field_prompt', '')
            
            if not domain or not field_prompt:
                return {
                    'success': False,
                    'error': 'Both domain and field_prompt are required',
                    'course_matches': []
                }
            
            # Parse courses
            courses_data = request_data.get('courses', [])
            courses = []
            
            for course_data in courses_data:
                course = Course(
                    course_id=str(course_data.get('course_id', '')),
                    name=course_data.get('course_name', ''),
                    short_description=course_data.get('course_short_description', ''),
                    description=course_data.get('course_description', ''),
                    skill=course_data.get('skill', '')
                )
                courses.append(course)
            
            # Filter courses using domain-aware method
            filter_response = self.filter_courses_by_domain(domain, field_prompt, courses)
            
            # Convert to JSON response
            course_matches_json = []
            for match in filter_response.course_matches:
                course_matches_json.append({
                    'course_id': match.course_id,
                    'course_name': match.course_name,
                    'match_percentage': match.match_percentage,
                    'reason': match.reason
                })
            
            return {
                'success': True,
                'domain': filter_response.domain,
                'field_prompt': filter_response.field_prompt,
                'total_courses_input': filter_response.total_courses_input,
                'course_matches': course_matches_json,
                'total_matching_courses': filter_response.total_matching_courses,
                'processing_summary': filter_response.processing_summary
            }
            
        except json.JSONDecodeError as e:
            logger.error(f"JSON decode error: {e}")
            return {
                'success': False,
                'error': f'Invalid JSON format: {e}',
                'course_matches': []
            }
        
        except Exception as e:
            logger.error(f"Processing error: {e}")
            return {
                'success': False,
                'error': str(e),
                'course_matches': []
            }

# Test function
def test_domain_aware_filtering():
    """Test the domain-aware filtering system"""
    
    # Sample courses for testing
    test_courses = [
    {
      "course_id": "33",
      "course_name": "Current Affairs",
      "short_description": "Current Affairs",
      "description": "",
      "skills": []
    },
    {
      "course_id": "35",
      "course_name": "I-Lead A leadership Development Intervention for Emerging Markets team",
      "short_description": "Leadership Development intervention for emerging markets team at Emcure",
      "description": "I-Lead Program was designed to support the Emerging Markets team Leaders at Emcure Pharmaceuticals to develop specific leadership competencies and to take their performance to the next level in achieve desired business goal.",
      "skills": []
    },
    {
      "course_id": "43",
      "course_name": "Python Programming for Complete Beginners",
      "short_description": "Python Programming for Complete Beginners",
      "description": "",
      "skills": []
    },
    {
      "course_id": "49",
      "course_name": "Sales MasterClass",
      "short_description": "A complete Sales Training Program",
      "description": "Today's selling environment is tough, and only getting tougher. The old tactics are no longer working, and the current economy is only making selling more difficult. You need sales tactics and strategies that work now and work fast.",
      "skills": []
    },
    {
      "course_id": "59",
      "course_name": "Codeigntier Beginner",
      "short_description": "Codeigntier Beginner",
      "description": "",
      "skills": []
    },
    {
      "course_id": "86",
      "course_name": "Java For Beginners",
      "short_description": "Java For Beginners",
      "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.",
      "skills": []
    },
    {
      "course_id": "360",
      "course_name": "Scorm Set-1",
      "short_description": "Set- 1",
      "description": "",
      "skills": []
    },
    {
      "course_id": "367",
      "course_name": "Scorm",
      "short_description": "Scorm",
      "description": "",
      "skills": []
    },
    {
      "course_id": "374",
      "course_name": "Production test 9-6-22",
      "short_description": "Production Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "408",
      "course_name": "Course for Learning path",
      "short_description": "Course GFor Learning path",
      "description": "",
      "skills": [
        {
          "skill_id": "285",
          "skill_type": "1",
          "skill_name": "Manager Level 1"
        },
        {
          "skill_id": "284",
          "skill_type": "1",
          "skill_name": "Wicketkipper"
        },
        {
          "skill_id": "283",
          "skill_type": "1",
          "skill_name": "Boller"
        }
      ]
    },
    {
      "course_id": "426",
      "course_name": "Learn Spoken English",
      "short_description": "Learn to speak English in 90 days",
      "description": "This is a 60 days self paced course. It consists of more than 3 hours of video lectures, more than 2 hours of reading material and hundreds of interesting questions.",
      "skills": []
    },
    {
      "course_id": "476",
      "course_name": "Induction",
      "short_description": "training",
      "description": "",
      "skills": []
    },
    {
      "course_id": "485",
      "course_name": "New Scorm Course",
      "short_description": "New Scorm Course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "496",
      "course_name": "Scorm Course Test",
      "short_description": "Scorm Course Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "503",
      "course_name": "Scorm Server Test",
      "short_description": "Scorm Server Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "505",
      "course_name": "Scorm Posh Course",
      "short_description": "InfoSec Posh",
      "description": "",
      "skills": []
    },
    {
      "course_id": "513",
      "course_name": "Scorm Info Security",
      "short_description": "Scorm Info Security",
      "description": "Miami-Dade County's partnership with OBE Power is providing electric vehicle (EV) charging as a service to parking patrons at key locations throughout the County's public garages in Downtown Miami.",
      "skills": []
    },
    {
      "course_id": "536",
      "course_name": "assesment test",
      "short_description": "assesment",
      "description": "",
      "skills": []
    },
    {
      "course_id": "544",
      "course_name": "Only Classic quiz",
      "short_description": "quiz",
      "description": "",
      "skills": []
    },
    {
      "course_id": "554",
      "course_name": "Course for testing direct coure 100%",
      "short_description": "Course for testing direct coure 100%",
      "description": "",
      "skills": []
    },
    {
      "course_id": "562",
      "course_name": "New Assessment for Infobeans",
      "short_description": "New Assessment for Infobeans",
      "description": "",
      "skills": []
    },
    {
      "course_id": "565",
      "course_name": "Video testing",
      "short_description": "video",
      "description": "",
      "skills": []
    },
    {
      "course_id": "570",
      "course_name": "Cultural Pillars",
      "short_description": "Cultural Pillars",
      "description": "",
      "skills": []
    },
    {
      "course_id": "599",
      "course_name": "All resourses",
      "short_description": "All",
      "description": "",
      "skills": []
    },
    {
      "course_id": "616",
      "course_name": "CODE OF CONDUCT, ANTI-BRIBERY AND CORRUPTION POLICY",
      "short_description": "CODE OF CONDUCT, ANTI-BRIBERY AND CORRUPTION POLICY",
      "description": "",
      "skills": []
    },
    {
      "course_id": "650",
      "course_name": "ISO Test",
      "short_description": "ISO Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "656",
      "course_name": "New Course For Production build testing 20-7-23",
      "short_description": "Production Build",
      "description": "",
      "skills": []
    },
    {
      "course_id": "657",
      "course_name": "Reminder Test For Course",
      "short_description": "Reminder",
      "description": "",
      "skills": []
    },
    {
      "course_id": "673",
      "course_name": "Developing Digital Etiquette",
      "short_description": "Developing Digital Etiquette",
      "description": "In today's digital world, the way we communicate online matters more than ever. Developing Digital Etiquette is designed to help you navigate virtual interactions with professionalism, clarity, and respect.",
      "skills": [
        {
          "skill_id": "1544",
          "skill_type": "1",
          "skill_name": "Digital Marketing Strategy"
        },
        {
          "skill_id": "1540",
          "skill_type": "1",
          "skill_name": "Robotics Engineering"
        }
      ]
    },
    {
      "course_id": "683",
      "course_name": "Storage System",
      "short_description": "Storage System",
      "description": "",
      "skills": []
    },
    {
      "course_id": "684",
      "course_name": "Module Lock Test",
      "short_description": "Modul Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "700",
      "course_name": "Course Lock",
      "short_description": "Course Lock",
      "description": "",
      "skills": []
    },
    {
      "course_id": "710",
      "course_name": "Course Flow Check Same As KC",
      "short_description": "KC",
      "description": "",
      "skills": []
    },
    {
      "course_id": "711",
      "course_name": "Ascent UK B2B Test",
      "short_description": "Ascent UK B2B Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "712",
      "course_name": "Ascent USA B2B Test",
      "short_description": "Ascent USA B2B Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "715",
      "course_name": "Developing Digital Etiquette- Cycle - 1",
      "short_description": "Developing Digital Etiquette",
      "description": "",
      "skills": []
    },
    {
      "course_id": "718",
      "course_name": "Material Handeling Equipments",
      "short_description": "Material Handeling Equipments",
      "description": "",
      "skills": []
    },
    {
      "course_id": "725",
      "course_name": "Advance - Ascent UK B2B Test- Cycle - 1",
      "short_description": "Ascent UK B2B Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "726",
      "course_name": "Advance - Ascent USA B2B Test",
      "short_description": "Ascent USA B2B Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "730",
      "course_name": "Communicating with impact",
      "short_description": "Communicating with impact",
      "description": "",
      "skills": []
    },
    {
      "course_id": "733",
      "course_name": "Test Stream Upload file",
      "short_description": "Test Stream Upload file",
      "description": "",
      "skills": []
    },
    {
      "course_id": "735",
      "course_name": "Intrapreneurship",
      "short_description": "Intrapreneurship",
      "description": "",
      "skills": []
    },
    {
      "course_id": "765",
      "course_name": "Course",
      "short_description": "Course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "790",
      "course_name": "Advance Level Communication",
      "short_description": "Advance Level Communication",
      "description": "",
      "skills": []
    },
    {
      "course_id": "795",
      "course_name": "Trainer create a course",
      "short_description": "Trainer course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "805",
      "course_name": "Coad of Conduct 23-24",
      "short_description": "23 -24",
      "description": "",
      "skills": []
    },
    {
      "course_id": "810",
      "course_name": "POSH and INFO SECK",
      "short_description": "INFO",
      "description": "",
      "skills": []
    },
    {
      "course_id": "845",
      "course_name": "All Content",
      "short_description": "All",
      "description": "",
      "skills": []
    },
    {
      "course_id": "851",
      "course_name": "Section Export",
      "short_description": "Section",
      "description": "",
      "skills": []
    },
    {
      "course_id": "852",
      "course_name": "Course New create test",
      "short_description": "Course New create test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "857",
      "course_name": "Developing People at Work",
      "short_description": "Learn how to develop people at work",
      "description": "People feel valued when the organizations they work for care for their development. This course is designed to give you a lens through which you can assess the developmental needs of your Team members.",
      "skills": []
    },
    {
      "course_id": "900",
      "course_name": "Dock Ack Test",
      "short_description": "Dock",
      "description": "",
      "skills": []
    },
    {
      "course_id": "914",
      "course_name": "Posh for Bloom (neha)",
      "short_description": "Bloom",
      "description": "",
      "skills": []
    },
    {
      "course_id": "915",
      "course_name": "Manager Course- Mangesh",
      "short_description": "Mangesh",
      "description": "",
      "skills": []
    },
    {
      "course_id": "936",
      "course_name": "Mechanical Demo",
      "short_description": "Mechanical Demo",
      "description": "",
      "skills": []
    },
    {
      "course_id": "977",
      "course_name": "Test course 15-5-2024",
      "short_description": "Test course 14/05/24",
      "description": "Test course 14/05/24",
      "skills": []
    },
    {
      "course_id": "1000",
      "course_name": "The Five Dysfunctions of a team",
      "short_description": "The Five Dysfunctions of a team",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1017",
      "course_name": "The Art of Coaching",
      "short_description": "A step by step approach to initiate coaching conversations with your Team Members",
      "description": "This course aims to introduce the Art of Coaching to managers and leaders who wish to take that extra step to develop their Team Members. Coaching is often misunderstood because we witness it from a perspective where the coach is pushing and telling people what to do.",
      "skills": []
    },
    {
      "course_id": "1026",
      "course_name": "All resourses- Cycle - 2",
      "short_description": "All",
      "description": "कार्यस्थल में महिलाओं के यौन उत्पीड़न का निवारण",
      "skills": []
    },
    {
      "course_id": "1048",
      "course_name": "Assignment",
      "short_description": "Assignment",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1059",
      "course_name": "POSH Hindi",
      "short_description": "POSH Hindi",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1081",
      "course_name": "Extc",
      "short_description": "Demo",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1084",
      "course_name": "Learning Path course 1",
      "short_description": "Lp1",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1085",
      "course_name": "Electrical Engineering",
      "short_description": "All",
      "description": "कार्यस्थल में महिलाओं के यौन उत्पीड़न का निवारण",
      "skills": []
    },
    {
      "course_id": "1090",
      "course_name": "Electrical Vehical",
      "short_description": "All",
      "description": "कार्यस्थल में महिलाओं के यौन उत्पीड़न का निवारण",
      "skills": []
    },
    {
      "course_id": "1101",
      "course_name": "Mechanical",
      "short_description": "All",
      "description": "कार्यस्थल में महिलाओं के यौन उत्पीड़न का निवारण",
      "skills": []
    },
    {
      "course_id": "1131",
      "course_name": "Communication Skill",
      "short_description": "All",
      "description": "कार्यस्थल में महिलाओं के यौन उत्पीड़न का निवारण",
      "skills": []
    },
    {
      "course_id": "1132",
      "course_name": "Demo Course",
      "short_description": "Demo",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1158",
      "course_name": "Dis Function",
      "short_description": "5",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1182",
      "course_name": "Report Test",
      "short_description": "Report Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1188",
      "course_name": "Email Etiquitee Scorm Web",
      "short_description": "Email Etiquitee Scorm Web",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1189",
      "course_name": "Stress Management",
      "short_description": "Stress Management",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1248",
      "course_name": "Course reference matrial acknoldge",
      "short_description": "Course reference matrial acknoldge",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1278",
      "course_name": "NICPL & HR Induction",
      "short_description": "NICPL & HR Induction",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1288",
      "course_name": "Assignment Test",
      "short_description": "Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1293",
      "course_name": "Coursera Course",
      "short_description": "Course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1298",
      "course_name": "Assignment One",
      "short_description": "Assignment",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1304",
      "course_name": "BGSS Demo Course 1",
      "short_description": "one",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1305",
      "course_name": "Time Spent course One",
      "short_description": "Time spent",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1350",
      "course_name": "Assignment Test",
      "short_description": "Assignment Test",
      "description": "",
      "skills": [
        {
          "skill_id": "981",
          "skill_type": "1",
          "skill_name": "Ancibel"
        }
      ]
    },
    {
      "course_id": "1360",
      "course_name": "Time Spent course One - Cycle - 1",
      "short_description": "Time spent",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1376",
      "course_name": "Cleanmax Requirement",
      "short_description": "Multimodule Support",
      "description": "",
      "skills": [
        {
          "skill_id": "981",
          "skill_type": "1",
          "skill_name": "Ancibel"
        },
        {
          "skill_id": "282",
          "skill_type": "1",
          "skill_name": "Batsman"
        },
        {
          "skill_id": "972",
          "skill_type": "3",
          "skill_name": "Linux"
        }
      ]
    },
    {
      "course_id": "1377",
      "course_name": "Multiple Scorm in one module",
      "short_description": "Multiple Scorm in one module",
      "description": "Multiple Scorm in one module",
      "skills": []
    },
    {
      "course_id": "1378",
      "course_name": "All resourses for testing",
      "short_description": "All",
      "description": "कार्यस्थल में महिलाओं के यौन उत्पीड़न का निवारण",
      "skills": []
    },
    {
      "course_id": "1388",
      "course_name": "Manager feed Back",
      "short_description": "Manager Feed Back",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1389",
      "course_name": "Advance Feedback Test",
      "short_description": "Advance Feedback Test",
      "description": "Advance Feedback Test",
      "skills": []
    },
    {
      "course_id": "1411",
      "course_name": "Advance Feedback Test- Cycle - 3",
      "short_description": "Advance Feedback Test",
      "description": "Advance Feedback Test",
      "skills": []
    },
    {
      "course_id": "1415",
      "course_name": "BGSS Demo",
      "short_description": "BGSS Demo",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1424",
      "course_name": "Advance Feedback Test- Cycle - 3- Cycle - 1",
      "short_description": "Advance Feedback Test",
      "description": "Advance Feedback Test",
      "skills": []
    },
    {
      "course_id": "1426",
      "course_name": "Midland Demo",
      "short_description": "Midland Demo",
      "description": "Edurigo empowers managers with powerful tools to seamlessly track their team's progress. Tabs like Courses, Programs, Assessments, and StoriGo provide instant visibility into completion statuses.",
      "skills": []
    },
    {
      "course_id": "1435",
      "course_name": "Advance Feedback Testing",
      "short_description": "Advance Feedback Testing",
      "description": "Advance Feedback Testing",
      "skills": []
    },
    {
      "course_id": "1438",
      "course_name": "Advance Feedback Testing- Cycle - 2",
      "short_description": "Advance Feedback Testing",
      "description": "Advance Feedback Testing",
      "skills": []
    },
    {
      "course_id": "1446",
      "course_name": "Advance Feedback Testing- Cycle - 2- Cycle - 1",
      "short_description": "Advance Feedback Testing",
      "description": "Advance Feedback Testing",
      "skills": []
    },
    {
      "course_id": "1447",
      "course_name": "Weeak ly Course",
      "short_description": "Weeak ly Course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1455",
      "course_name": "Assignment Test- Cycle - 1",
      "short_description": "Assignment Test",
      "description": "",
      "skills": [
        {
          "skill_id": "981",
          "skill_type": "1",
          "skill_name": "Ancibel"
        }
      ]
    },
    {
      "course_id": "1456",
      "course_name": "HR Course 1",
      "short_description": "Hr",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1457",
      "course_name": "upload assignment course test",
      "short_description": "upload assignment course test",
      "description": "upload assignment course test",
      "skills": []
    },
    {
      "course_id": "1458",
      "course_name": "Assignment Test-for new device",
      "short_description": "Assignment Test",
      "description": "",
      "skills": [
        {
          "skill_id": "981",
          "skill_type": "1",
          "skill_name": "Ancibel"
        }
      ]
    },
    {
      "course_id": "1469",
      "course_name": "upload assignment course test- Cycle - 1",
      "short_description": "upload assignment course test",
      "description": "upload assignment course test",
      "skills": []
    },
    {
      "course_id": "1476",
      "course_name": "m1",
      "short_description": "m1",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1481",
      "course_name": "All Content- Cycle - 1",
      "short_description": "All",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1498",
      "course_name": "Frontend Development - HTML & CSS",
      "short_description": "Introduction to HTML and CSS basics",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1499",
      "course_name": "Frontend Development - JavaScript",
      "short_description": "Introduction to JavaScript (JS)",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1500",
      "course_name": "Frontend Development - ReactJs",
      "short_description": "Introduction to ReactJs",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1505",
      "course_name": "v1",
      "short_description": "v1",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1510",
      "course_name": "All resource course",
      "short_description": "All Resource course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1562",
      "course_name": "Scorm 1 vedak",
      "short_description": "Scorm 1",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1572",
      "course_name": "iframe video",
      "short_description": "iframe video",
      "description": "iframe video",
      "skills": []
    },
    {
      "course_id": "1577",
      "course_name": "Assignment Test",
      "short_description": "Assignment Test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1591",
      "course_name": "Video Test",
      "short_description": "v",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1597",
      "course_name": "Vijaysales Demo",
      "short_description": "Vijaysales Demo",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1616",
      "course_name": "Handling Difficult Conversations",
      "short_description": "Handling Difficult Conversations",
      "description": "Track and calculate average learning hours per user annually.",
      "skills": []
    },
    {
      "course_id": "1628",
      "course_name": "Midland Use Case",
      "short_description": "Midland Use Case",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1636",
      "course_name": "HR Induction Course",
      "short_description": "HR Induction Course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1649",
      "course_name": "Demo Nirmal",
      "short_description": "Demo Nirmal",
      "description": "",
      "skills": [
        {
          "skill_id": "1591",
          "skill_type": "2",
          "skill_name": "Active Listening"
        }
      ]
    },
    {
      "course_id": "1991",
      "course_name": "ANgular Basics",
      "short_description": "ANgular Basicss",
      "description": "",
      "skills": [
        
      ]
    },
    {
      "course_id": "1668",
      "course_name": "functional demo",
      "short_description": "functional demo",
      "description": "",
      "skills": [
        {
          "skill_id": "1591",
          "skill_type": "1",
          "skill_name": "Active Listening"
        },
        {
          "skill_id": "1342",
          "skill_type": "2",
          "skill_name": "Big Data Analysis"
        }
      ]
    },
    {
      "course_id": "1669",
      "course_name": "Course test without badges",
      "short_description": "Course test without badges",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1674",
      "course_name": "Lock seeak bar",
      "short_description": "Lock seeak bar",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1675",
      "course_name": "hide seek bar testing",
      "short_description": "hide seek bar testing",
      "description": "hide seek bar testing",
      "skills": []
    },
    {
      "course_id": "1676",
      "course_name": "Two Bagje Only",
      "short_description": "Two Bagje Only",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1677",
      "course_name": "badge test",
      "short_description": "badge test",
      "description": "badge test",
      "skills": []
    },
    {
      "course_id": "1678",
      "course_name": "badge test- Cycle - 1",
      "short_description": "badge test",
      "description": "badge test",
      "skills": []
    },
    {
      "course_id": "1679",
      "course_name": "badge test 2",
      "short_description": "badge test 2",
      "description": "badge test 2",
      "skills": []
    },
    {
      "course_id": "1680",
      "course_name": "badge test- Cycle - 2",
      "short_description": "badge test",
      "description": "badge test",
      "skills": []
    },
    {
      "course_id": "1681",
      "course_name": "badge test- Cycle - 2- Cycle - 1",
      "short_description": "badge test",
      "description": "badge test",
      "skills": []
    },
    {
      "course_id": "1682",
      "course_name": "Assignment Flow test",
      "short_description": "Assignment flow test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1683",
      "course_name": "Assignment test",
      "short_description": "Assignment flow test",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1685",
      "course_name": "Single Badge With Single roul",
      "short_description": "Single Badge With Single roul",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1686",
      "course_name": "Single Badge: Completion by first set of learners",
      "short_description": "Completion by first set of learners",
      "description": "",
      "skills": [
        {
          "skill_id": "1599",
          "skill_type": "1",
          "skill_name": "Blockchain Security Measures"
        }
      ]
    },
    {
      "course_id": "1687",
      "course_name": "Single Badge : Score exceeds defined percentage",
      "short_description": "Score exceeds defined percentage",
      "description": "",
      "skills": [
        {
          "skill_id": "1499",
          "skill_type": "1",
          "skill_name": "Accounting and Auditing"
        }
      ]
    },
    {
      "course_id": "1688",
      "course_name": "Single Badge : With 2 Quiz",
      "short_description": "Single Badge : With 2 Quiz",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1689",
      "course_name": "Completion before specified date",
      "short_description": "Completion before specified date",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1690",
      "course_name": "Completion before specified date- Cycle - 1",
      "short_description": "Completion before specified date",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1691",
      "course_name": "3 Badge : Score exceeds defined percentage : diffrant percentage",
      "short_description": "3 Badge : Score exceeds defined percentage : diffrant percentage",
      "description": "",
      "skills": [
        {
          "skill_id": "1499",
          "skill_type": "1",
          "skill_name": "Accounting and Auditing"
        }
      ]
    },
    {
      "course_id": "1692",
      "course_name": "Single Badge With Multipal Roul Or condition",
      "short_description": "Single Badge With Multipal Roul",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1693",
      "course_name": "Single Badge With Multipal Roul AND condition",
      "short_description": "Single Badge With Multipal Roul",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1694",
      "course_name": "Single Badge With Single roul: API Change",
      "short_description": "Single Badge With Single roul",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1695",
      "course_name": "Single Badge: Completion by first set of learners : API Change",
      "short_description": "Completion by first set of learners",
      "description": "",
      "skills": [
        {
          "skill_id": "1599",
          "skill_type": "1",
          "skill_name": "Blockchain Security Measures"
        }
      ]
    },
    {
      "course_id": "1696",
      "course_name": "Single Badge : Score exceeds defined percentage- Cycle - 2",
      "short_description": "Score exceeds defined percentage",
      "description": "",
      "skills": [
        {
          "skill_id": "1499",
          "skill_type": "1",
          "skill_name": "Accounting and Auditing"
        }
      ]
    },
    {
      "course_id": "1697",
      "course_name": "Single Badge : Score exceeds defined percentage- Cycle - 2- Cycle - 1",
      "short_description": "Score exceeds defined percentage",
      "description": "",
      "skills": [
        {
          "skill_id": "1499",
          "skill_type": "1",
          "skill_name": "Accounting and Auditing"
        }
      ]
    },
    {
      "course_id": "1698",
      "course_name": "3 Badge : Score exceeds defined percentage : diffrant percentage- Cycle - 1",
      "short_description": "3 Badge : Score exceeds defined percentage : diffrant percentage",
      "description": "",
      "skills": [
        {
          "skill_id": "1499",
          "skill_type": "1",
          "skill_name": "Accounting and Auditing"
        }
      ]
    },
    {
      "course_id": "1700",
      "course_name": "All Resource",
      "short_description": "All Resource",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1701",
      "course_name": "Zero Point Course",
      "short_description": "Zero Point Course",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1702",
      "course_name": "New One",
      "short_description": "New One",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1703",
      "course_name": "New One- Cycle - 1",
      "short_description": "New One",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1704",
      "course_name": "Decision Making",
      "short_description": "Decision Making",
      "description": "",
      "skills": []
    },
    {
      "course_id": "1705",
      "course_name": "Time and Stress Management",
      "short_description": "Time and Stress Management",
      "description": "",
      "skills": []
    }
  ]
    
    # Test cases
    test_cases = [
        {
            "domain": "Technology",
            "field_prompt": "I want to improev my tech skills"
        },
        {
            "domain": "Business",
            "field_prompt": "Sales Training"
        },
        {
            "domain": "Technology", 
            "field_prompt": "I want to improve only my python skills"
        }
    ]
    
    print("=== TESTING DOMAIN-AWARE COURSE FILTER ===")
    filter_system = DomainAwareCourseFilter(batch_size=30)
    
    for test_case in test_cases:
        print(f"\n{'='*60}")
        print(f"TESTING: {test_case['domain']} - {test_case['field_prompt']}")
        print(f"{'='*60}")
        
        # Create test request
        test_request = {
            "domain": test_case["domain"],
            "field_prompt": test_case["field_prompt"],
            "courses": test_courses
        }
        
        # Process request
        result = filter_system.process_json_request(json.dumps(test_request))
        
        # Display results
        print(f"✅ SUCCESS: {result['success']}")
        print(f"📊 STATS:")
        print(f"   Input courses: {result['total_courses_input']}")
        print(f"   Final matches: {result['total_matching_courses']}")
        
        print(f"\n🎯 COURSE MATCHES:")
        for match in result['course_matches']:
            print(f"   📋 {match['course_name']}")
            print(f"      Match: {match['match_percentage']}%")
            print(f"      Reason: {match['reason']}")
            print()
        
        if not result['course_matches']:
            print(f"   No matches found for {test_case['field_prompt']}")
        
        print(f"📝 Processing: {result['processing_summary']}")

if __name__ == "__main__":
    test_domain_aware_filtering()