"""
Management command to create SEO statistics dummy data
"""

from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import datetime, timedelta
import random


class Command(BaseCommand):
    help = 'Create SEO statistics dummy data'

    def handle(self, *args, **options):
        try:
            # Create dummy SEO statistics data
            seo_stats = {
                'page_views': random.randint(1500, 5000),
                'unique_visitors': random.randint(800, 2500),
                'bounce_rate': round(random.uniform(35, 65), 1),
                'seo_score': random.randint(75, 95),
                'total_pages': random.randint(25, 50),
                'total_posts': random.randint(15, 30),
                'total_images': random.randint(100, 300),
                'avg_session_duration': f"{random.randint(2, 8)}:{random.randint(0, 59):02d}",
                'top_keywords': [
                    'desa pulo sarok',
                    'pantai pulo sarok',
                    'aceh singkil',
                    'hutan mangrove',
                    'wisata aceh',
                    'kecamatan singkil',
                    'pemerintahan desa',
                    'layanan administrasi'
                ],
                'social_shares': {
                    'facebook': random.randint(50, 200),
                    'instagram': random.randint(30, 150),
                    'twitter': random.randint(20, 100),
                    'whatsapp': random.randint(100, 300)
                },
                'traffic_sources': {
                    'organic': random.randint(40, 60),
                    'direct': random.randint(20, 35),
                    'social': random.randint(10, 25),
                    'referral': random.randint(5, 15)
                },
                'device_breakdown': {
                    'mobile': random.randint(60, 80),
                    'desktop': random.randint(15, 30),
                    'tablet': random.randint(5, 15)
                },
                'geographic_data': {
                    'aceh_singkil': random.randint(40, 60),
                    'aceh_utara': random.randint(15, 25),
                    'medan': random.randint(10, 20),
                    'jakarta': random.randint(5, 15),
                    'other': random.randint(10, 20)
                }
            }

            self.stdout.write(
                self.style.SUCCESS(
                    f'✅ SEO Statistics dummy data berhasil dibuat!\n'
                    f'📊 Statistik SEO:\n'
                    f'   • Page Views: {seo_stats["page_views"]:,}\n'
                    f'   • Unique Visitors: {seo_stats["unique_visitors"]:,}\n'
                    f'   • Bounce Rate: {seo_stats["bounce_rate"]}%\n'
                    f'   • SEO Score: {seo_stats["seo_score"]}/100\n'
                    f'   • Total Pages: {seo_stats["total_pages"]}\n'
                    f'   • Total Posts: {seo_stats["total_posts"]}\n'
                    f'   • Total Images: {seo_stats["total_images"]}\n'
                    f'   • Avg Session: {seo_stats["avg_session_duration"]}\n'
                    f'\n🔍 Top Keywords:\n'
                    f'   • {", ".join(seo_stats["top_keywords"][:5])}\n'
                    f'\n📱 Social Shares:\n'
                    f'   • Facebook: {seo_stats["social_shares"]["facebook"]}\n'
                    f'   • Instagram: {seo_stats["social_shares"]["instagram"]}\n'
                    f'   • Twitter: {seo_stats["social_shares"]["twitter"]}\n'
                    f'   • WhatsApp: {seo_stats["social_shares"]["whatsapp"]}\n'
                    f'\n🌍 Traffic Sources:\n'
                    f'   • Organic: {seo_stats["traffic_sources"]["organic"]}%\n'
                    f'   • Direct: {seo_stats["traffic_sources"]["direct"]}%\n'
                    f'   • Social: {seo_stats["traffic_sources"]["social"]}%\n'
                    f'   • Referral: {seo_stats["traffic_sources"]["referral"]}%\n'
                    f'\n📱 Device Breakdown:\n'
                    f'   • Mobile: {seo_stats["device_breakdown"]["mobile"]}%\n'
                    f'   • Desktop: {seo_stats["device_breakdown"]["desktop"]}%\n'
                    f'   • Tablet: {seo_stats["device_breakdown"]["tablet"]}%\n'
                    f'\n🗺️ Geographic Data:\n'
                    f'   • Aceh Singkil: {seo_stats["geographic_data"]["aceh_singkil"]}%\n'
                    f'   • Aceh Utara: {seo_stats["geographic_data"]["aceh_utara"]}%\n'
                    f'   • Medan: {seo_stats["geographic_data"]["medan"]}%\n'
                    f'   • Jakarta: {seo_stats["geographic_data"]["jakarta"]}%\n'
                    f'   • Other: {seo_stats["geographic_data"]["other"]}%\n'
                )
            )

            # Save to a temporary file for reference
            import json
            with open('seo_stats_dummy.json', 'w', encoding='utf-8') as f:
                json.dump(seo_stats, f, indent=2, ensure_ascii=False)
            
            self.stdout.write(
                self.style.SUCCESS('💾 Data tersimpan di seo_stats_dummy.json untuk referensi')
            )

        except Exception as e:
            self.stdout.write(
                self.style.ERROR(f'❌ Error creating SEO statistics dummy data: {str(e)}')
            )
