from django.core.management.base import BaseCommand

LESSONS = [
    {
        "slug": "what-is-git",
        "order": 1,
        "title_fa": "Git چیست؟",
        "title_en": "What is Git?",
        "description_fa": "مقدمه‌ای بر Git و کنترل نسخه",
        "category": "basics",
        "content_fa": "Git یک سیستم کنترل نسخه توزیع‌شده است که در سال ۲۰۰۵ توسط لینوس توروالدز برای مدیریت کد هسته لینوکس ساخته شد.\n\nکنترل نسخه بهت اجازه میده تمام تغییرات پروژه رو ذخیره کنی و هر لحظه به نسخه قبلی برگردی.\n\nGitHub یه پلتفرم ابری برای میزبانی ریپازیتوری‌های Git هست و امکانات همکاری مثل Pull Request، Issues و Actions رو داره.",
        "commands": []
    },
    {
        "slug": "git-init-clone",
        "order": 2,
        "title_fa": "شروع با Git",
        "title_en": "Getting Started",
        "description_fa": "نصب، پیکربندی، و ساخت اولین ریپازیتوری",
        "category": "basics",
        "content_fa": "قبل از هر چیز باید Git رو نصب کنی و هویتت رو تنظیم کنی. این هویت روی تمام کامیت‌هایی که می‌زنی ثبت میشه.\n\nدو روش شروع داری: یا یه پوشه جدید رو به ریپازیتوری تبدیل کنی (git init)، یا یه پروژه موجود رو از GitHub کپی کنی (git clone).\n\nبعد از git init، یه پوشه مخفی .git ساخته میشه که تمام تاریخچه پروژه رو داره.",
        "commands": [
            {"command": "git config --global user.name \"نام شما\"", "description_fa": "تنظیم نام کاربری", "example_output": "", "order": 1},
            {"command": "git config --global user.email \"email@example.com\"", "description_fa": "تنظیم ایمیل", "example_output": "", "order": 2},
            {"command": "git init", "description_fa": "ساخت ریپازیتوری جدید", "example_output": "Initialized empty Git repository in /project/.git/", "order": 3},
            {"command": "git clone <url>", "description_fa": "کپی کردن ریپازیتوری از GitHub", "example_output": "Cloning into 'project'...\nremote: Counting objects: 100, done.", "order": 4},
        ]
    },
    {
        "slug": "add-commit",
        "order": 3,
        "title_fa": "Stage و Commit",
        "title_en": "Staging & Committing",
        "description_fa": "ذخیره تغییرات با add و commit",
        "category": "basics",
        "content_fa": "در Git ذخیره تغییرات دو مرحله داره: Stage کردن و Commit کردن.\n\nاین طراحی عمدیه — گاهی روی چند چیز همزمان کار می‌کنی و می‌خوای فقط بخشی از تغییرات رو commit کنی.\n\nبرای پیام commit از Conventional Commits استفاده کن: feat برای ویژگی جدید، fix برای رفع باگ، docs برای مستندات.",
        "commands": [
            {"command": "git status", "description_fa": "نمایش وضعیت فایل‌های تغییریافته", "example_output": "On branch main\nChanges not staged for commit:\n  modified:   index.html", "order": 1},
            {"command": "git add .", "description_fa": "اضافه کردن تمام تغییرات به stage", "example_output": "", "order": 2},
            {"command": "git add <file>", "description_fa": "اضافه کردن یک فایل به stage", "example_output": "", "order": 3},
            {"command": "git commit -m \"پیام کامیت\"", "description_fa": "ثبت تغییرات stage‌شده", "example_output": "[main 3a4f8c2] feat: add homepage\n 2 files changed, 45 insertions(+)", "order": 4},
            {"command": "git commit --amend", "description_fa": "ویرایش آخرین commit", "example_output": "", "order": 5},
        ]
    },
    {
        "slug": "branching",
        "order": 4,
        "title_fa": "شاخه‌بندی (Branching)",
        "title_en": "Branching & Merging",
        "description_fa": "کار موازی با branch و ادغام با merge",
        "category": "intermediate",
        "content_fa": "Branch مثل یه خط موازی توسعه‌ست. بدون اینکه کد اصلی رو خراب کنی، می‌تونی روی یه ویژگی جدید کار کنی.\n\nاستراتژی نام‌گذاری: feature/login برای ویژگی جدید، fix/bug برای رفع باگ، hotfix/critical برای رفع فوری.\n\nmerge تاریخچه رو کامل نگه می‌داره (توصیه‌شده برای تیم). rebase تاریخچه رو خطی می‌کنه (برای local).",
        "commands": [
            {"command": "git branch", "description_fa": "لیست تمام branch‌ها", "example_output": "* main\n  feature/login\n  fix/navbar", "order": 1},
            {"command": "git checkout -b <name>", "description_fa": "ساخت branch جدید + سوئیچ", "example_output": "Switched to a new branch 'feature/login'", "order": 2},
            {"command": "git switch <name>", "description_fa": "سوئیچ به branch موجود", "example_output": "Switched to branch 'main'", "order": 3},
            {"command": "git merge <branch>", "description_fa": "ادغام branch به branch فعلی", "example_output": "Merge made by the 'ort' strategy.\n index.html | 20 +++++", "order": 4},
            {"command": "git branch -d <name>", "description_fa": "حذف branch بعد از merge", "example_output": "Deleted branch feature/login (was 3a4f8c2).", "order": 5},
        ]
    },
    {
        "slug": "remote-github",
        "order": 5,
        "title_fa": "Remote و GitHub",
        "title_en": "Remote & GitHub",
        "description_fa": "اتصال به GitHub و همکاری تیمی",
        "category": "intermediate",
        "content_fa": "Remote یه ریپازیتوری روی سرور دیگه‌ست. با remote می‌تونی کدت رو با تیم به اشتراک بذاری.\n\norigin نام پیش‌فرض remote اصلیه. push برای ارسال به remote و pull برای دریافت از remote استفاده میشه.\n\nSSH راحت‌تره — یک بار کلید رو اضافه می‌کنی و نیازی به وارد کردن پسورد نداری.",
        "commands": [
            {"command": "git remote add origin <url>", "description_fa": "اتصال ریپازیتوری local به GitHub", "example_output": "", "order": 1},
            {"command": "git remote -v", "description_fa": "نمایش آدرس remote‌ها", "example_output": "origin  git@github.com:user/repo.git (fetch)\norigin  git@github.com:user/repo.git (push)", "order": 2},
            {"command": "git push -u origin main", "description_fa": "اولین push و تنظیم upstream", "example_output": "Branch 'main' set up to track remote branch 'main' from 'origin'.", "order": 3},
            {"command": "git pull", "description_fa": "دریافت و ادغام تغییرات remote", "example_output": "Already up to date.", "order": 4},
            {"command": "git fetch --all --prune", "description_fa": "دریافت همه تغییرات بدون ادغام", "example_output": "Fetching origin\nFrom github.com:user/repo\n * [new branch] feature/api -> origin/feature/api", "order": 5},
        ]
    },
    {
        "slug": "git-log",
        "order": 6,
        "title_fa": "بررسی تاریخچه",
        "title_en": "Inspecting History",
        "description_fa": "خواندن log، diff و blame",
        "category": "intermediate",
        "content_fa": "git log تمام تاریخچه پروژه رو نشون میده. با گزینه‌های مختلف می‌تونی خروجی رو شخصی‌سازی کنی.\n\ngit diff تفاوت بین وضعیت‌های مختلف رو نشون میده — بین working tree و stage، یا بین دو کامیت.\n\ngit blame نشون میده هر خط از کد رو چه کسی کِی نوشته. برای پیدا کردن مسئول یه باگ خیلی مفیده!",
        "commands": [
            {"command": "git log --oneline", "description_fa": "نمایش خلاصه تاریخچه", "example_output": "9b2e1f4 feat: add login page\n3a4f8c2 fix: navbar alignment\n1c7d3e8 init: project setup", "order": 1},
            {"command": "git log --oneline --graph --all", "description_fa": "نمایش گرافیکی branch‌ها", "example_output": "* 9b2e1f4 feat: add login\n| * 7a3c2d1 fix: api error\n|/\n* 3a4f8c2 init project", "order": 2},
            {"command": "git diff", "description_fa": "تفاوت فایل‌های تغییریافته", "example_output": "diff --git a/index.html b/index.html\n-  <h1>Hello</h1>\n+  <h1>Hello World</h1>", "order": 3},
            {"command": "git blame <file>", "description_fa": "نمایش نویسنده هر خط از فایل", "example_output": "3a4f8c2 (Ali  2024-01-01) const app = express();\n9b2e1f4 (Sara 2024-01-05) app.use(cors());", "order": 4},
        ]
    },
    {
        "slug": "undo",
        "order": 7,
        "title_fa": "برگشت از تغییرات",
        "title_en": "Undoing Changes",
        "description_fa": "reset، revert، stash و بازیابی خطاها",
        "category": "advanced",
        "content_fa": "در Git تقریباً هیچ چیزی غیرقابل برگشت نیست، اما باید بدونی از کدوم دستور استفاده کنی.\n\nreset --soft کامیت رو پاک می‌کنه ولی تغییرات رو stage نگه می‌داره. reset --hard همه چیز رو پاک می‌کنه (خطرناک!).\n\nrevert یه کامیت جدید می‌سازه که تغییرات قبلی رو برمی‌گردونه — ایمن برای remote برخلاف reset.",
        "commands": [
            {"command": "git stash", "description_fa": "ذخیره موقت تغییرات بدون commit", "example_output": "Saved working directory and index state WIP on main: 3a4f8c2", "order": 1},
            {"command": "git stash pop", "description_fa": "بازگرداندن آخرین stash", "example_output": "On branch main\nChanges not staged for commit:\n  modified: index.html", "order": 2},
            {"command": "git reset --soft HEAD~1", "description_fa": "برگشت آخرین commit (نگه‌داشتن stage)", "example_output": "", "order": 3},
            {"command": "git reset --hard HEAD~1", "description_fa": "برگشت کامل آخرین commit (خطرناک!)", "example_output": "HEAD is now at 3a4f8c2 fix: navbar", "order": 4},
            {"command": "git revert <hash>", "description_fa": "معکوس کردن commit بدون تغییر تاریخچه", "example_output": "[main 7c3d2a1] Revert feat: add login page\n 1 file changed, 20 deletions(-)", "order": 5},
        ]
    },
]


class Command(BaseCommand):
    help = 'Seed database with lessons'

    def handle(self, *args, **kwargs):
        from api.models import Lesson, Command as Cmd
        for lesson_data in LESSONS:
            commands_data = lesson_data.pop('commands', [])
            lesson, created = Lesson.objects.update_or_create(
                slug=lesson_data['slug'], defaults=lesson_data
            )
            action = 'Created' if created else 'Updated'
            self.stdout.write(f'  {action}: {lesson.title_fa}')
            Cmd.objects.filter(lesson=lesson).delete()
            for cmd_data in commands_data:
                Cmd.objects.create(lesson=lesson, **cmd_data)
        self.stdout.write(self.style.SUCCESS(f'\n✓ {len(LESSONS)} lessons seeded'))
