import random
from datetime import timedelta
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.crm.models import Category, Teacher, Project, Coupon, Banner, Student, StudentCoupon, StudentProject, StudentHonor

class Command(BaseCommand):
    help = 'Populate database with mock data for demonstration'

    def handle(self, *args, **kwargs):
        self.stdout.write('Starting to populate mock data...')

        # 1. Create Categories
        self.stdout.write('Creating Categories...')
        categories_data = [
            {'name': '少儿编程', 'color': 'bg-blue-100 text-blue-600'},
            {'name': '美术创意', 'color': 'bg-pink-100 text-pink-600'},
            {'name': '口才演讲', 'color': 'bg-purple-100 text-purple-600'},
            {'name': '科学实验', 'color': 'bg-green-100 text-green-600'},
            {'name': '围棋象棋', 'color': 'bg-yellow-100 text-yellow-600'},
        ]
        
        categories = []
        for data in categories_data:
            cat, created = Category.objects.get_or_create(name=data['name'], defaults=data)
            categories.append(cat)

        # 2. Create Teachers
        self.stdout.write('Creating Teachers...')
        teachers_data = [
            {'name': '李老师', 'phone': '13800138001', 'bio': '资深少儿编程讲师，拥有5年教学经验。'},
            {'name': '王老师', 'phone': '13800138002', 'bio': '美术学院毕业，擅长创意美术教学。'},
            {'name': '张老师', 'phone': '13800138003', 'bio': '播音主持专业，专注于少儿口才培养。'},
            {'name': '赵老师', 'phone': '13800138004', 'bio': '科学教育专家，带队多次获得科技大赛奖项。'},
        ]
        
        teachers = []
        for data in teachers_data:
            teacher, created = Teacher.objects.get_or_create(name=data['name'], defaults=data)
            teachers.append(teacher)

        # 3. Create Projects
        self.stdout.write('Creating Projects...')
        projects_data = [
            {
                'title': 'Python趣味编程入门',
                'project_type': 'training',
                'teacher': teachers[0],
                'image': 'https://images.unsplash.com/photo-1526379095098-d400fd0bf935',
                'detail': '通过趣味游戏学习Python编程基础，培养逻辑思维能力。',
                'rating': 4.9,
                'duration': '12 课时'
            },
            {
                'title': '创意色彩大师班',
                'project_type': 'training',
                'teacher': teachers[1],
                'image': 'https://images.unsplash.com/photo-1513364776144-60967b0f800f',
                'detail': '探索色彩奥秘，激发艺术潜能，创作属于自己的艺术作品。',
                'rating': 4.8,
                'duration': '8 课时'
            },
            {
                'title': '小小主持人特训营',
                'project_type': 'training',
                'teacher': teachers[2],
                'image': 'https://images.unsplash.com/photo-1590602847861-f357a9332bbc',
                'detail': '提升自信心和表达能力，掌握舞台主持技巧。',
                'rating': 5.0,
                'duration': '10 课时'
            },
            {
                'title': '全国青少年科技创新大赛',
                'project_type': 'competition',
                'teacher': teachers[3],
                'image': 'https://images.unsplash.com/photo-1532094349884-543bc11b234d',
                'detail': '备战全国大赛，专业导师一对一指导。',
                'rating': 5.0,
                'duration': '全程指导'
            },
            {
                'title': '围棋段位考试冲刺',
                'project_type': 'grading',
                'teacher': teachers[0], # Reusing teacher for simplicity
                'image': 'https://images.unsplash.com/photo-1529699211952-734e80c4d42b',
                'detail': '针对段位考试重点难点进行突击训练。',
                'rating': 4.7,
                'duration': '2 周'
            }
        ]
        
        projects = []
        for data in projects_data:
            proj, created = Project.objects.get_or_create(title=data['title'], defaults=data)
            projects.append(proj)

        # 4. Create Coupons
        self.stdout.write('Creating Coupons...')
        coupons_data = [
            {
                'title': '新人体验券',
                'amount': '50',
                'unit': '元',
                'desc': '全场通用，无门槛',
                'expiry': timezone.now().date() + timedelta(days=30),
                'status': 'available',
                'issue_type': 'registration',
                'color': 'from-blue-500 to-cyan-400',
                'bgStart': 'from-blue-50',
                'bgEnd': 'to-cyan-50',
                'shadow': 'shadow-blue-100'
            },
            {
                'title': '课程立减券',
                'amount': '100',
                'unit': '元',
                'desc': '满1000元可用',
                'expiry': timezone.now().date() + timedelta(days=60),
                'status': 'available',
                'issue_type': 'manual',
                'color': 'from-purple-500 to-pink-400',
                'bgStart': 'from-purple-50',
                'bgEnd': 'to-pink-50',
                'shadow': 'shadow-purple-100'
            },
             {
                'title': '暑期特惠券',
                'amount': '200',
                'unit': '元',
                'desc': '暑期课程专用',
                'expiry': timezone.now().date() + timedelta(days=90),
                'status': 'available',
                'issue_type': 'manual',
                'color': 'from-orange-500 to-red-400',
                'bgStart': 'from-orange-50',
                'bgEnd': 'to-red-50',
                'shadow': 'shadow-orange-100'
            }
        ]

        coupons = []
        for data in coupons_data:
            coup, created = Coupon.objects.get_or_create(title=data['title'], defaults=data)
            coupons.append(coup)

        # 5. Create Banners
        self.stdout.write('Creating Banners...')
        banners_data = [
            {
                'image': 'https://images.unsplash.com/photo-1503676260728-1c00da094a0b',
                'project': projects[0],
                'sort_order': 1
            },
            {
                'image': 'https://images.unsplash.com/photo-1454165804606-c3d57bc86b40',
                'project': projects[2],
                'sort_order': 2
            }
        ]
        
        for data in banners_data:
            Banner.objects.get_or_create(image=data['image'], defaults=data)

        # 6. Create Students
        self.stdout.write('Creating Students...')
        students_data = [
            {
                'name': '张小明',
                'phone': '13900000001',
                'age': 8,
                'parent': '张伟',
                'teacher': teachers[0],
                'avatar': 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde'
            },
            {
                'name': '李小红',
                'phone': '13900000002',
                'age': 9,
                'parent': '李强',
                'teacher': teachers[1],
                'avatar': 'https://images.unsplash.com/photo-1438761681033-6461ffad8d80'
            },
            {
                'name': '王小华',
                'phone': '13900000003',
                'age': 10,
                'parent': '王磊',
                'teacher': teachers[2],
                'avatar': 'https://images.unsplash.com/photo-1599566150163-29194dcaad36'
            }
        ]
        
        students = []
        for data in students_data:
            stu, created = Student.objects.get_or_create(name=data['name'], phone=data['phone'], defaults=data)
            students.append(stu)

        # 7. Create Student Projects (Enrollments)
        self.stdout.write('Enrolling Students...')
        # Enroll first student in first project
        StudentProject.objects.get_or_create(
            student=students[0],
            project=projects[0],
            defaults={'status': 'enrolled'}
        )
        # Enroll first student in second project (completed)
        StudentProject.objects.get_or_create(
            student=students[0],
            project=projects[1],
            defaults={'status': 'completed', 'completed_at': timezone.now()}
        )
        
        # 8. Create Student Honors
        self.stdout.write('Creating Honors...')
        honors_data = [
            {
                'student': students[0],
                'title': '优秀学员奖',
                'date': timezone.now().date() - timedelta(days=10),
                'image': 'https://images.unsplash.com/photo-1579548122080-c35fd6820ecb',
                'description': '在Python编程课程中表现优异，特发此证。'
            },
            {
                'student': students[0],
                'title': '小小演说家',
                'date': timezone.now().date() - timedelta(days=50),
                'image': 'https://images.unsplash.com/photo-1579548122080-c35fd6820ecb',
                'description': '在演讲比赛中获得一等奖。'
            }
        ]
        
        for data in honors_data:
            StudentHonor.objects.get_or_create(student=data['student'], title=data['title'], defaults=data)

        # 9. Assign Coupons to Student
        self.stdout.write('Assigning Coupons...')
        StudentCoupon.objects.get_or_create(
            student=students[0],
            coupon=coupons[0],
            defaults={'status': 'assigned'}
        )
        StudentCoupon.objects.get_or_create(
            student=students[0],
            coupon=coupons[1],
            defaults={'status': 'used', 'used_at': timezone.now()}
        )

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