"""
Management command to populate GlobalAIPrompt entries with default templates
"""
from django.core.management.base import BaseCommand
from igny8_core.modules.system.global_settings_models import GlobalAIPrompt


class Command(BaseCommand):
    help = 'Populate GlobalAIPrompt entries with default prompt templates'

    def handle(self, *args, **options):
        prompts_data = [
            {
                'prompt_type': 'clustering',
                'prompt_value': '''Analyze the following keywords and group them into clusters based on semantic similarity and topical relevance:

Keywords: {keywords}

Instructions:
1. Group keywords that share similar intent or topic
2. Each cluster should have 3-10 related keywords
3. Create meaningful cluster names that capture the essence
4. Prioritize high-value, commercially-relevant groupings

Return a JSON array of clusters with this structure:
[
  {
    "cluster_name": "Descriptive name",
    "keywords": ["keyword1", "keyword2", ...],
    "primary_intent": "informational|commercial|transactional"
  }
]'''
            },
            {
                'prompt_type': 'ideas',
                'prompt_value': '''Generate content ideas for the following topic cluster:

Topic: {topic}
Keywords: {keywords}
Target Audience: {audience}

Instructions:
1. Create {count} unique content ideas
2. Each idea should target different angles or subtopics
3. Consider various content formats (how-to, comparison, list, guide)
4. Focus on search intent and user value

Return a JSON array:
[
  {
    "title": "Engaging title",
    "angle": "Unique perspective or approach",
    "target_keywords": ["keyword1", "keyword2"],
    "content_type": "how-to|comparison|list|guide|analysis"
  }
]'''
            },
            {
                'prompt_type': 'content_generation',
                'prompt_value': '''Write comprehensive, SEO-optimized content for the following:

Title: {title}
Target Keywords: {keywords}
Word Count: {word_count}
Tone: {tone}

Content Brief:
{brief}

Instructions:
1. Create engaging, informative content that fully addresses the topic
2. Naturally incorporate target keywords (avoid keyword stuffing)
3. Use clear headings (H2, H3) to structure the content
4. Include actionable insights and examples where relevant
5. Write for both readers and search engines
6. Maintain the specified tone throughout

Return well-structured HTML content with proper heading tags.'''
            },
            {
                'prompt_type': 'image_prompt_extraction',
                'prompt_value': '''Analyze this article content and extract key visual concepts for image generation:

Content: {content}

Instructions:
1. Identify {count} main concepts that would benefit from visual representation
2. Focus on concrete, visualizable elements (not abstract concepts)
3. Consider what would add value for readers
4. Prioritize scenes, objects, or scenarios that can be depicted

Return a JSON array:
[
  {
    "concept": "Brief description of what to visualize",
    "placement": "header|section1|section2|...",
    "priority": "high|medium|low"
  }
]'''
            },
            {
                'prompt_type': 'image_prompt_template',
                'prompt_value': '''Create a detailed image generation prompt for:

Concept: {concept}
Style: {style}
Context: {context}

Generate a prompt that:
1. Describes the scene or subject clearly
2. Specifies composition, lighting, and perspective
3. Matches the {style} aesthetic
4. Is optimized for AI image generation

Return only the image prompt (no explanations).'''
            },
            {
                'prompt_type': 'negative_prompt',
                'prompt_value': '''text, watermark, logo, signature, username, artist name, blurry, low quality, pixelated, distorted, deformed, duplicate, cropped, out of frame, bad anatomy, bad proportions, extra limbs, missing limbs, floating limbs, disconnected limbs, mutation, mutated, ugly, disgusting, amputation, cartoon, anime'''
            },
            {
                'prompt_type': 'site_structure_generation',
                'prompt_value': '''Design a comprehensive site structure for:

Business Type: {business_type}
Primary Keywords: {keywords}
Target Audience: {audience}
Goals: {goals}

Instructions:
1. Create a logical, user-friendly navigation hierarchy
2. Include essential pages (Home, About, Services/Products, Contact)
3. Design category pages for primary keywords
4. Plan supporting content pages
5. Consider user journey and conversion paths

Return a JSON structure:
{
  "navigation": [
    {
      "page": "Page name",
      "slug": "url-slug",
      "type": "home|category|product|service|content|utility",
      "children": []
    }
  ]
}'''
            },
            {
                'prompt_type': 'product_generation',
                'prompt_value': '''Create comprehensive product content for:

Product Name: {product_name}
Category: {category}
Features: {features}
Target Audience: {audience}

Generate:
1. Compelling product description (200-300 words)
2. Key features and benefits (bullet points)
3. Technical specifications
4. Use cases or applications
5. SEO-optimized meta description

Return structured JSON with all elements.'''
            },
            {
                'prompt_type': 'service_generation',
                'prompt_value': '''Create detailed service page content for:

Service Name: {service_name}
Category: {category}
Key Benefits: {benefits}
Target Audience: {audience}

Generate:
1. Overview section (150-200 words)
2. Process or methodology (step-by-step)
3. Benefits and outcomes
4. Why choose us / differentiators
5. FAQ section (5-7 questions)
6. Call-to-action suggestions

Return structured HTML content.'''
            },
            {
                'prompt_type': 'taxonomy_generation',
                'prompt_value': '''Create a logical taxonomy structure for:

Content Type: {content_type}
Domain: {domain}
Existing Keywords: {keywords}

Instructions:
1. Design parent categories that organize content logically
2. Create subcategories for detailed organization
3. Ensure balanced hierarchy (not too deep or flat)
4. Use clear, descriptive category names
5. Consider SEO and user navigation

Return a JSON structure:
{
  "categories": [
    {
      "name": "Category Name",
      "slug": "category-slug",
      "description": "Brief description",
      "subcategories": []
    }
  ]
}'''
            },
        ]

        created_count = 0
        updated_count = 0

        for prompt_data in prompts_data:
            prompt, created = GlobalAIPrompt.objects.update_or_create(
                prompt_type=prompt_data['prompt_type'],
                defaults={'prompt_value': prompt_data['prompt_value']}
            )
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'Created: {prompt.get_prompt_type_display()}')
                )
            else:
                updated_count += 1
                self.stdout.write(
                    self.style.WARNING(f'Updated: {prompt.get_prompt_type_display()}')
                )

        self.stdout.write(
            self.style.SUCCESS(
                f'\nCompleted: {created_count} created, {updated_count} updated'
            )
        )
