import sqlite3
import traceback
from typing import List, Dict
from breed_health_info import breed_health_info, default_health_note
from breed_noise_info import breed_noise_info
from dog_database import get_dog_description
from scoring_calculation_system import  UserPreferences, calculate_compatibility_score
def format_recommendation_html(recommendations: List[Dict], is_description_search: bool = False) -> str:
    """將推薦結果格式化為HTML"""
    def _convert_to_display_score(score: float, score_type: str = None) -> int:
        """
        更改為生成更明顯差異的顯示分數
        """
        try:
            # 基礎分數轉換(保持相對關係但擴大差異)
            if score_type == 'bonus':  # Breed Bonus 使用不同的轉換邏輯
                base_score = 35 + (score * 60)  # 35-95 範圍,差異更大
            else:
                # 其他類型的分數轉換
                if score <= 0.3:
                    base_score = 40 + (score * 45)  # 40-53.5 範圍
                elif score <= 0.6:
                    base_score = 55 + ((score - 0.3) * 55)  # 55-71.5 範圍
                elif score <= 0.8:
                    base_score = 72 + ((score - 0.6) * 60)  # 72-84 範圍
                else:
                    base_score = 85 + ((score - 0.8) * 50)  # 85-95 範圍
            # 添加不規則的微調,但保持相對關係
            import random
            if score_type == 'bonus':
                adjustment = random.uniform(-2, 2)
            else:
                # 根據分數範圍決定調整幅度
                if score > 0.8:
                    adjustment = random.uniform(-3, 3)
                elif score > 0.6:
                    adjustment = random.uniform(-4, 4)
                else:
                    adjustment = random.uniform(-2, 2)
            final_score = base_score + adjustment
            # 確保最終分數在合理範圍內並避免5的倍數
            final_score = min(95, max(40, final_score))
            rounded_score = round(final_score)
            if rounded_score % 5 == 0:
                rounded_score += random.choice([-1, 1])
            return rounded_score
        except Exception as e:
            print(f"Error in convert_to_display_score: {str(e)}")
            return 70
    # def _generate_progress_bar(score: float) -> float:
    #     """生成非線性的進度條寬度"""
    #     if score <= 0.3:
    #         width = 30 + (score / 0.3) * 20
    #     elif score <= 0.6:
    #         width = 50 + ((score - 0.3) / 0.3) * 20
    #     elif score <= 0.8:
    #         width = 70 + ((score - 0.6) / 0.2) * 15
    #     else:
    #         width = 85 + ((score - 0.8) / 0.2) * 15
    #     import random
    #     width += random.uniform(-2, 2)
    #     return min(100, max(20, width))
    def _generate_progress_bar(score: float) -> float:
        """
        優化進度條寬度計算
        
        改進:
        - 確保100%時完全填滿
        - 更線性的視覺呈現
        - 保持合理的視覺比例
        """
        # 基礎寬度計算
        if score >= 1.0:
            return 100.0  # 確保100%時完全填滿
        
        # 一般情況的寬度計算
        if score > 0.9:
            # 高分區間線性延伸
            width = 90 + (score - 0.9) * 100
        elif score > 0.7:
            # 中高分區間稍微展開
            width = 70 + (score - 0.7) * 100
        else:
            # 基礎線性關係
            width = score * 100
        
        # 加入微小的隨機變化,使顯示更自然
        import random
        width += random.uniform(-0.5, 0.5)
        
        # 確保範圍合理
        return min(99.5, max(20, width)) if score < 1.0 else 100.0
    html_content = "
"
    for rec in recommendations:
        breed = rec['breed']
        scores = rec['scores']
        info = rec['info']
        rank = rec.get('rank', 0)
        final_score = rec.get('final_score', scores['overall'])
        bonus_score = rec.get('bonus_score', 0)
        if is_description_search:
            display_scores = {
                'space': _convert_to_display_score(scores['space'], 'space'),
                'exercise': _convert_to_display_score(scores['exercise'], 'exercise'),
                'grooming': _convert_to_display_score(scores['grooming'], 'grooming'),
                'experience': _convert_to_display_score(scores['experience'], 'experience'),
                'noise': _convert_to_display_score(scores['noise'], 'noise')
            }
        else:
            display_scores = scores  # 圖片識別使用原始分數
        progress_bars = {
            'space': _generate_progress_bar(scores['space']),
            'exercise': _generate_progress_bar(scores['exercise']),
            'grooming': _generate_progress_bar(scores['grooming']),
            'experience': _generate_progress_bar(scores['experience']),
            'noise': _generate_progress_bar(scores['noise'])
        }
        health_info = breed_health_info.get(breed, {"health_notes": default_health_note})
        noise_info = breed_noise_info.get(breed, {
            "noise_notes": "Noise information not available",
            "noise_level": "Unknown",
            "source": "N/A"
        })
        # 解析噪音資訊
        noise_notes = noise_info.get('noise_notes', '').split('\n')
        noise_characteristics = []
        barking_triggers = []
        noise_level = ''
        current_section = None
        for line in noise_notes:
            line = line.strip()
            if 'Typical noise characteristics:' in line:
                current_section = 'characteristics'
            elif 'Noise level:' in line:
                noise_level = line.replace('Noise level:', '').strip()
            elif 'Barking triggers:' in line:
                current_section = 'triggers'
            elif line.startswith('•'):
                if current_section == 'characteristics':
                    noise_characteristics.append(line[1:].strip())
                elif current_section == 'triggers':
                    barking_triggers.append(line[1:].strip())
        # 生成特徵和觸發因素的HTML
        noise_characteristics_html = '\n'.join([f'
{item}' for item in noise_characteristics])
        barking_triggers_html = '\n'.join([f'
{item}' for item in barking_triggers])
        # 處理健康資訊
        health_notes = health_info.get('health_notes', '').split('\n')
        health_considerations = []
        health_screenings = []
        current_section = None
        for line in health_notes:
            line = line.strip()
            if 'Common breed-specific health considerations' in line:
                current_section = 'considerations'
            elif 'Recommended health screenings:' in line:
                current_section = 'screenings'
            elif line.startswith('•'):
                if current_section == 'considerations':
                    health_considerations.append(line[1:].strip())
                elif current_section == 'screenings':
                    health_screenings.append(line[1:].strip())
        health_considerations_html = '\n'.join([f'
{item}' for item in health_considerations])
        health_screenings_html = '\n'.join([f'
{item}' for item in health_screenings])
        # 獎勵原因計算
        bonus_reasons = []
        temperament = info.get('Temperament', '').lower()
        if any(trait in temperament for trait in ['friendly', 'gentle', 'affectionate']):
            bonus_reasons.append("Positive temperament traits")
        if info.get('Good with Children') == 'Yes':
            bonus_reasons.append("Excellent with children")
        try:
            lifespan = info.get('Lifespan', '10-12 years')
            years = int(lifespan.split('-')[0])
            if years >= 12:
                bonus_reasons.append("Above-average lifespan")
        except:
            pass
        html_content += f"""
        
            
                
                    🏆 #{rank} {breed.replace('_', ' ')}
                    
                        Overall Match: {final_score*100:.1f}%
                    
                
                
                    
                        Space Compatibility:
                        
                        {display_scores['space'] if is_description_search else scores['space']*100:.1f}%
                     
                    
                        Exercise Match:
                        
                        {display_scores['exercise'] if is_description_search else scores['exercise']*100:.1f}%
                     
                    
                        Grooming Match:
                        
                        {display_scores['grooming'] if is_description_search else scores['grooming']*100:.1f}%
                     
                    
                        Experience Match:
                        
                        {display_scores['experience'] if is_description_search else scores['experience']*100:.1f}%
                     
                    
                        
                            Noise Compatibility:
                            
                                ⓘ
                                
                                    Noise Compatibility Score:
                                    • Based on your noise tolerance preference
                                    • Considers breed's typical noise level
                                    • Accounts for living environment
                                
                            
                        
                        
                        {display_scores['noise'] if is_description_search else scores['noise']*100:.1f}%
                     
                    {f'''
                    
                        
                            Breed Bonus:
                            
                                ⓘ
                                
                                    Breed Bonus Points:
                                    • {('
• '.join(bonus_reasons)) if bonus_reasons else 'No additional bonus points'}
                                    
                                    Bonus Factors Include:
                                    • Friendly temperament
                                    • Child compatibility
                                    • Longer lifespan
                                    • Living space adaptability
                                
                            
                        
                        
                        {bonus_score*100:.1f}%
                     
                    ''' if bonus_score > 0 else ''}
                
 
                
                    
                        📋 Breed Details
                    
                    
                        
                            
                                📏
                                Size:
                                ⓘ
                                
                                    Size Categories:
                                    • Small: Under 20 pounds
                                    • Medium: 20-60 pounds
                                    • Large: Over 60 pounds
                                
                                {info['Size']}
                            
                        
                        
                            
                                🏃
                                Exercise Needs:
                                ⓘ
                                
                                    Exercise Needs:
                                    • Low: Short walks
                                    • Moderate: 1-2 hours daily
                                    • High: 2+ hours daily
                                    • Very High: Constant activity
                                
                                {info['Exercise Needs']}
                            
                        
                        
                            
                                👨👩👧👦
                                Good with Children:
                                ⓘ
                                
                                    Child Compatibility:
                                    • Yes: Excellent with kids
                                    • Moderate: Good with older children
                                    • No: Better for adult households
                                
                                {info['Good with Children']}
                            
                        
                        
                            
                                ⏳
                                Lifespan:
                                ⓘ
                                
                                    Average Lifespan:
                                    • Short: 6-8 years
                                    • Average: 10-15 years
                                    • Long: 12-20 years
                                    • Varies by size: Larger breeds typically have shorter lifespans
                                
                            
                            {info['Lifespan']}
                        
                     
                 
                
                    
                        📝 Description
                    
                    {info.get('Description', '')}
                 
                
                    
                    
                        
                            
                            
                                Moderate to high barker
                                Alert watch dog
                                Attention-seeking barks
                                Social vocalizations
                             
                            
                            
                            
                                Separation anxiety
                                Attention needs
                                Strange noises
                                Excitement
                             
                         
                        
                            Source: Compiled from various breed behavior resources, 2024
                            Individual dogs may vary in their vocalization patterns.
                            Training can significantly influence barking behavior.
                            Environmental factors may affect noise levels.
                         
                     
                 
                
                    
                    
                        
                            
                                
                                
                                    Patellar luxation
                                    Progressive retinal atrophy
                                    Von Willebrand's disease
                                    Open fontanel
                                 
                             
                            
                                
                                
                                    Patella evaluation
                                    Eye examination
                                    Blood clotting tests
                                    Skull development monitoring
                                 
                             
                         
                        
                            Source: Compiled from various veterinary and breed information resources, 2024
                            This information is for reference only and based on breed tendencies.
                            Each dog is unique and may not develop any or all of these conditions.
                            Always consult with qualified veterinarians for professional advice.
                         
                     
                 
                
             
         
        """
    html_content += "