"""
音乐模块工具函数
"""
from django.conf import settings

OSS_BASE = "https://qy-rtc.oss-cn-beijing.aliyuncs.com"
DEFAULT_COVER = f"{OSS_BASE}/music/defaults/Capybara.png"

DEFAULT_TRACKS = [
    {
        "title": "卡皮巴拉蹦蹦蹦",
        "audio_url": f"{OSS_BASE}/music/defaults/卡皮巴拉蹦蹦蹦.mp3",
        "cover_url": DEFAULT_COVER,
        "lyrics": (
            "卡皮巴拉\n啦啦啦啦\n卡皮巴拉\n啦啦啦啦\n\n"
            "卡皮巴拉 蹦蹦蹦\n一整天都 在发疯\n"
            "卡皮巴拉 转一圈\n左一脚 右一脚 (嘿)\n\n"
            "卡皮巴拉 蹦蹦蹦\n洗脑节奏 响空中\n"
            "卡皮巴拉 不要停\n跟着我 一起疯\n\n"
            "一口菜叶 卡一巴\n两口草莓 巴一拉\n"
            "三口西瓜 啦一啦\n嘴巴圆圆 哈哈哈 (哦耶)"
        ),
    },
    {
        "title": "卡皮巴拉快乐水",
        "audio_url": f"{OSS_BASE}/music/defaults/卡皮巴拉快乐水.mp3",
        "cover_url": DEFAULT_COVER,
        "lyrics": (
            "卡皮巴拉\n卡皮巴拉\n卡皮巴拉\n啦啦啦啦\n\n"
            "卡皮巴拉趴地上\n一动不动好嚣张\n"
            "心里其实在上网\n刷到我就笑出响 (哈哈哈)\n\n"
            "卡皮巴拉 巴拉巴拉\n压力来啦 它说算啦\n"
            "一点不慌 就是躺啦\n世界太吵 它在发呆呀"
        ),
    },
    {
        "title": "卡皮巴拉快乐营业",
        "audio_url": f"{OSS_BASE}/music/defaults/卡皮巴拉快乐营业.mp3",
        "cover_url": DEFAULT_COVER,
        "lyrics": (
            "早八打工人\n心却躺平人\n"
            "桌面壁纸换上\n卡皮巴拉一整屏 (嘿)\n\n"
            "它坐在河边\n像个退休中年\n"
            "我卷生卷死\n它只发呆发呆再发呆\n\n"
            "卡皮巴拉 卡皮巴拉 拉\n看你就把压力清空啦 (啊对对对)\n"
            "谁骂我韭菜我就回他\n我已经转职水豚啦"
        ),
    },
]


def ensure_default_tracks(user):
    """确保用户有 3 首默认曲目，没有则创建"""
    from .models import Track

    if Track.objects.filter(user=user, is_default=True).exists():
        return

    tracks = []
    for item in DEFAULT_TRACKS:
        tracks.append(Track(
            user=user,
            title=item["title"],
            lyrics=item["lyrics"],
            audio_url=item["audio_url"],
            cover_url=item["cover_url"],
            is_default=True,
            generation_status='completed',
        ))
    Track.objects.bulk_create(tracks)
