from django.core.management.base import BaseCommand
from apps.crm.models import Category, Project, Coupon, Banner, Student
from django.utils import timezone
import random

class Command(BaseCommand):
    help = 'Initialize CRM mock data'

    def handle(self, *args, **options):
        self.stdout.write('Cleaning up old data...')
        Category.objects.all().delete()
        Project.objects.all().delete()
        Coupon.objects.all().delete()
        Banner.objects.all().delete()
        Student.objects.all().delete()

        self.stdout.write('Creating Categories...')
        categories_data = [
            {'name': '前端开发', 'color': 'bg-blue-100 text-blue-600'},
            {'name': '后端开发', 'color': 'bg-green-100 text-green-600'},
            {'name': '人工智能', 'color': 'bg-purple-100 text-purple-600'},
            {'name': '移动开发', 'color': 'bg-yellow-100 text-yellow-600'},
            {'name': '运维测试', 'color': 'bg-red-100 text-red-600'},
        ]
        categories = []
        for data in categories_data:
            cat = Category.objects.create(**data)
            categories.append(cat)
            self.stdout.write(f'Created category: {cat.name}')

        self.stdout.write('Creating Projects...')
        projects_data = [
            {'title': 'Vue3实战教程', 'detail': '深入学习Vue3核心原理与实战应用...'},
            {'title': 'Django REST Framework入门', 'detail': '从零构建RESTful API...'},
            {'title': 'Python数据分析', 'detail': '使用Pandas和NumPy进行数据分析...'},
            {'title': 'Flutter跨平台开发', 'detail': '构建高性能移动应用...'},
            {'title': 'Docker容器化部署', 'detail': '掌握Docker容器技术...'},
            {'title': 'React Hooks进阶', 'detail': '精通React Hooks...'},
            {'title': 'Go语言高并发编程', 'detail': 'Go语言核心特性与并发模式...'},
            {'title': '机器学习基础', 'detail': '常用机器学习算法原理与实现...'},
            {'title': 'iOS Swift开发', 'detail': 'Swift语言基础与iOS应用开发...'},
            {'title': 'Kubernetes集群管理', 'detail': 'K8s集群部署与维护...'},
        ]
        
        projects = []
        for i, data in enumerate(projects_data):
            cat = categories[i % len(categories)]
            proj = Project.objects.create(
                title=data['title'],
                category=cat,
                image=f'https://picsum.photos/seed/{i}/800/600',
                detail=data['detail'],
                students=random.randint(100, 1000),
                rating=round(random.uniform(4.0, 5.0), 1),
                duration=f'{random.randint(2, 8)} 周'
            )
            projects.append(proj)
            self.stdout.write(f'Created project: {proj.title}')

        self.stdout.write('Creating Coupons...')
        coupons_data = [
            {'amount': '50', 'title': '新人专享券', 'desc': '全场通用，无门槛'},
            {'amount': '100', 'title': '进阶课程券', 'desc': '满500可用'},
            {'amount': '200', 'title': '高级课程券', 'desc': '满1000可用'},
            {'amount': '8.8', 'title': '限时折扣', 'desc': '全场8.8折', 'unit': '折'},
            {'amount': '500', 'title': 'VIP专属', 'desc': 'VIP会员专用'},
        ]
        
        for data in coupons_data:
            Coupon.objects.create(
                amount=data['amount'],
                unit=data.get('unit', '元'),
                title=data['title'],
                desc=data['desc'],
                expiry=timezone.now().date() + timezone.timedelta(days=random.randint(7, 30))
            )
            self.stdout.write(f'Created coupon: {data["title"]}')

        self.stdout.write('Creating Students...')
        for i in range(10):
            Student.objects.create(
                name=f'学员{i+1}',
                phone=f'1380013800{i}',
                age=random.randint(18, 35),
                education=random.choice(['本科', '硕士', '大专']),
                address=f'北京市海淀区某街道{i+1}号',
                learning_count=random.randint(1, 5),
                coupon_count=random.randint(0, 3),
                study_hours=random.randint(10, 100)
            )
            self.stdout.write(f'Created student: {i+1}')

        self.stdout.write('Creating Banners...')
        for i in range(3):
            proj = random.choice(projects)
            Banner.objects.create(
                image=f'https://picsum.photos/seed/banner{i}/1920/600',
                project=proj,
                sort_order=i,
                is_active=True
            )
            self.stdout.write(f'Created banner {i+1} linking to {proj.title}')

        self.stdout.write(self.style.SUCCESS('Successfully initialized mock data'))
