"""
Management command to copy prompt values from database and update default prompts in code
"""
from django.core.management.base import BaseCommand
from igny8_core.modules.system.models import AIPrompt


class Command(BaseCommand):
    help = 'Copy prompt values from database and update default prompts in prompts.py'

    def handle(self, *args, **options):
        prompt_types = ['clustering', 'ideas', 'content_generation']
        
        self.stdout.write("Fetching prompts from database...")
        
        prompts_data = {}
        for prompt_type in prompt_types:
            try:
                # Get the first active prompt for this type (assuming there's one per account or we take the first)
                prompt = AIPrompt.objects.filter(
                    prompt_type=prompt_type,
                    is_active=True
                ).first()
                
                if prompt:
                    prompts_data[prompt_type] = prompt.prompt_value
                    self.stdout.write(
                        self.style.SUCCESS(
                            f"✓ Found {prompt_type} prompt (ID: {prompt.id}, Account: {prompt.account_id if hasattr(prompt, 'account') else 'N/A'})"
                        )
                    )
                else:
                    self.stdout.write(
                        self.style.WARNING(
                            f"⚠ No active prompt found for {prompt_type}"
                        )
                    )
            except Exception as e:
                self.stdout.write(
                    self.style.ERROR(
                        f"✗ Error fetching {prompt_type}: {str(e)}"
                    )
                )
        
        # Print the prompts for manual copying
        self.stdout.write("\n" + "="*80)
        self.stdout.write("PROMPT VALUES FROM DATABASE:")
        self.stdout.write("="*80 + "\n")
        
        for prompt_type, prompt_value in prompts_data.items():
            self.stdout.write(f"\n# {prompt_type.upper()}")
            self.stdout.write(f"'{prompt_type}': \"\"\"{prompt_value}\"\"\",")
        
        self.stdout.write("\n" + "="*80)
        self.stdout.write("Copy the above prompts and update prompts.py DEFAULT_PROMPTS")
        self.stdout.write("="*80)

