from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password
from django.core.management.base import BaseCommand

from blog.models import Article, Tag, Category


class Command(BaseCommand):
    help = 'Initialize canteen ordering themed test data'

    def handle(self, *args, **options):
        user = get_user_model().objects.get_or_create(
            email='canteen@test.com', username='食堂管理员', password=make_password('canteen!Q@W#2025'))[0]

        # 顶级分类：校园食堂
        canteen_category = Category.objects.get_or_create(
            name='校园食堂', parent_category=None, defaults={"index": 100}
        )[0]

        # 二级分类：各食堂
        east_canteen = Category.objects.get_or_create(
            name='东区食堂', parent_category=canteen_category, defaults={"index": 90}
        )[0]
        west_canteen = Category.objects.get_or_create(
            name='西区食堂', parent_category=canteen_category, defaults={"index": 80}
        )[0]

        # 三级分类：档口
        stalls = [
            Category.objects.get_or_create(name='川味档口', parent_category=east_canteen, defaults={"index": 70})[0],
            Category.objects.get_or_create(name='面点档口', parent_category=east_canteen, defaults={"index": 60})[0],
            Category.objects.get_or_create(name='简餐档口', parent_category=west_canteen, defaults={"index": 50})[0],
            Category.objects.get_or_create(name='风味小吃', parent_category=west_canteen, defaults={"index": 40})[0],
        ]

        # 标签：口味与饮食偏好
        base_tags = [
            Tag.objects.get_or_create(name='辣味')[0],
            Tag.objects.get_or_create(name='清淡')[0],
            Tag.objects.get_or_create(name='无辣')[0],
            Tag.objects.get_or_create(name='主食')[0],
            Tag.objects.get_or_create(name='素食可选')[0],
        ]

        # 示例菜品以文章承载：标题为菜名，正文为描述/价格
        dish_specs = [
            ("宫保鸡丁套餐", "包含米饭+素菜，价格￥18，可加辣"),
            ("红烧牛肉面", "大份牛肉足量，价格￥22，清淡口可选"),
            ("番茄鸡蛋盖浇饭", "酸甜开胃，价格￥15，老少皆宜"),
            ("麻辣香锅", "自选配菜，称重计价，约￥20-30/份"),
            ("素什锦炒面", "少油少盐，价格￥14，素食友好"),
            ("鸡排饭", "现炸鸡排+配菜，价格￥19，无辣版本可选"),
            ("牛肉卷饼", "现烙饼皮，价格￥12，可加蛋+2"),
            ("酸辣粉", "地道红薯粉，价格￥13，可微辣/中特/特辣"),
        ]

        tag_cycle = base_tags * 4
        idx = 0
        for stall in stalls:
            for dish_title, dish_body in dish_specs:
                article = Article.objects.get_or_create(
                    category=stall,
                    title=f'{stall.name}-{dish_title}',
                    defaults={
                        'body': f'{dish_body}\n\n口味标签：{tag_cycle[idx % len(tag_cycle)].name}',
                        'author': user,
                        'status': 'p',
                        'comment_status': 'o',
                        'type': 'a',
                        'article_order': 0,
                        'show_toc': False,
                    }
                )[0]
                # 绑定两个标签
                article.tags.add(tag_cycle[idx % len(tag_cycle)])
                article.tags.add(tag_cycle[(idx + 1) % len(tag_cycle)])
                article.save()
                idx += 1

        from djangoblog.utils import cache
        cache.clear()
        self.stdout.write(self.style.SUCCESS('Initialized canteen ordering themed test data.'))








