<?php

namespace Database\Seeders;

use App\Models\Course;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class CourseSeeder extends Seeder
{
    public function run(): void
    {
        $course = Course::updateOrCreate(
            ['slug' => 'investable'],
            [
                'title' => 'Investable',
                'subtitle' => 'Take the mystery out of investing and creating wealth, a step-by-step plan for informed, confident investment decisions.',
                'description' => "Investable goes beyond picking investments. You get a framework that links your values, goals and aspirations to your investment choices, so you can adapt as your life changes.\n\nWhether you're starting with a little or a lot, we get practical: real case studies, real African investment options, and a clear action plan you build as you go.",
                'emoji' => 'trending-up',
                'cover_path' => 'images/card-investable.jpg',
                'level' => 'all',
                'price_kes' => 36000,
                'price_usd' => 280,
                'is_published' => true,
                'sort' => 0,
                'outcomes' => [
                    'Create an investment plan that works for your income, goals and lifestyle',
                    'Confidently evaluate any investment opportunity, and spot the scams',
                    'Diversify your portfolio so your eggs are never in one basket',
                    'Link your values and goals to every investment decision you make',
                ],
            ]
        );

        $course->modules()->delete();

        $modules = [
            [
                'title' => 'The Principle of Seed & Fruit',
                'summary' => 'How to fund your lifestyle, and why the order of seed and fruit changes everything about how you handle money.',
                'lessons' => [
                    ['title' => 'Welcome to Investable', 'duration' => 6, 'free' => true],
                    ['title' => 'Seed, Fruit and Your Lifestyle', 'duration' => 14],
                    ['title' => 'The Mindset Shift That Unlocks Wealth', 'duration' => 12],
                ],
            ],
            [
                'title' => 'Discovering Your Why',
                'summary' => 'Your values are the compass for every shilling. Clarify them, and investment decisions become dramatically easier.',
                'lessons' => [
                    ['title' => 'The Values Behind Your Money', 'duration' => 15],
                    ['title' => 'Defining Your Financial Freedom', 'duration' => 13],
                ],
            ],
            [
                'title' => 'Understanding Investments',
                'summary' => 'Setting goals, categorising them into baskets, and matching each basket to the right kind of investment.',
                'lessons' => [
                    ['title' => 'Goals and the Three Baskets', 'duration' => 18],
                    ['title' => 'Risk, Return and Time, Without the Jargon', 'duration' => 16],
                ],
            ],
            [
                'title' => 'Investing Options Based on Goals',
                'summary' => 'Money market funds, bonds, equities, real estate, SACCOs, what they are, how they work across Africa, and which goal each serves.',
                'lessons' => [
                    ['title' => 'The African Investment Menu', 'duration' => 20],
                    ['title' => 'Matching Investments to Your Baskets', 'duration' => 17],
                    ['title' => 'Spotting Scams and "Dead Assets"', 'duration' => 12],
                ],
            ],
            [
                'title' => 'Case Studies, Getting Practical',
                'summary' => 'Real scenarios at every income level. Watch complete plans come together, then build your own.',
                'lessons' => [
                    ['title' => 'Case Study: Starting With Little', 'duration' => 15],
                    ['title' => 'Case Study: The Mid-Career Reset', 'duration' => 16],
                    ['title' => 'Your Action Plan', 'duration' => 10],
                ],
            ],
        ];

        foreach ($modules as $mIndex => $moduleData) {
            $module = $course->modules()->create([
                'title' => $moduleData['title'],
                'summary' => $moduleData['summary'],
                'sort' => $mIndex,
            ]);

            foreach ($moduleData['lessons'] as $lIndex => $lesson) {
                $module->lessons()->create([
                    'title' => $lesson['title'],
                    'slug' => Str::slug($lesson['title']),
                    'duration_minutes' => $lesson['duration'],
                    'is_free_preview' => $lesson['free'] ?? false,
                    'sort' => $lIndex,
                ]);
            }
        }

        $this->seedCatalogue();
    }

    /** The wider course catalogue beyond the flagship. */
    private function seedCatalogue(): void
    {
        $catalogue = [
            [
                'slug' => 'debt-free-blueprint',
                'title' => 'The Debt-Free Blueprint',
                'subtitle' => 'A focused short course: clear every loan, mobile, bank or family, with a system that sticks.',
                'emoji' => 'unlink',
                'cover_path' => 'images/stock/debt-free.jpg',
                'level' => 'beginner',
                'price_kes' => 4500,
                'price_usd' => 35,
                'sort' => 1,
                'outcomes' => [
                    'A written payoff plan with your debt-free date',
                    'The snowball vs avalanche decision, settled for your situation',
                    'A relapse-proof system so the debt never comes back',
                ],
                'modules' => [
                    ['title' => 'Face the Number', 'lessons' => [['title' => 'The Honest Audit', 'duration' => 12, 'free' => true], ['title' => 'Why Mobile Loans Win', 'duration' => 10]]],
                    ['title' => 'The Payoff Engine', 'lessons' => [['title' => 'Snowball vs Avalanche', 'duration' => 14], ['title' => 'Negotiating With Lenders', 'duration' => 11]]],
                    ['title' => 'Never Again', 'lessons' => [['title' => 'The Buffer That Protects the Plan', 'duration' => 9], ['title' => 'Rebuilding Your Credit Story', 'duration' => 10]]],
                ],
            ],
            [
                'slug' => 'budgeting-mastery',
                'title' => 'Budgeting Mastery',
                'subtitle' => 'Build a budget that survives real African life, family asks, school fees, and the unexpected.',
                'emoji' => 'calculator',
                'cover_path' => 'images/stock/budget-table.jpg',
                'level' => 'beginner',
                'price_kes' => 3500,
                'price_usd' => 27,
                'sort' => 2,
                'outcomes' => [
                    'A monthly budget you\'ll actually follow',
                    'A family-support line that\'s generous AND sustainable',
                    'Automated saving that happens before you can spend it',
                ],
                'modules' => [
                    ['title' => 'Where the Money Goes', 'lessons' => [['title' => 'The 7-Day Money Mirror', 'duration' => 10, 'free' => true], ['title' => 'Naming Every Shilling', 'duration' => 12]]],
                    ['title' => 'The Budget That Holds', 'lessons' => [['title' => 'Essentials, Future-You, and Life', 'duration' => 14], ['title' => 'The Black-Tax Conversation', 'duration' => 13], ['title' => 'Automate Everything', 'duration' => 9]]],
                ],
            ],
            [
                'slug' => 'diaspora-investor',
                'title' => 'The Diaspora Investor',
                'subtitle' => 'Build wealth back home from abroad, with verification, structure and zero horror stories.',
                'emoji' => 'globe',
                'cover_path' => 'images/stock/diaspora.jpg',
                'level' => 'intermediate',
                'price_kes' => 9500,
                'price_usd' => 75,
                'sort' => 3,
                'outcomes' => [
                    'The due-diligence playbook for land and property at a distance',
                    'Investment vehicles you can run entirely from abroad',
                    'A two-economy money structure: emergency funds, currencies, remittances',
                ],
                'modules' => [
                    ['title' => 'Trust, Verified', 'lessons' => [['title' => 'Why Diaspora Money Disappears', 'duration' => 11, 'free' => true], ['title' => 'Title Searches & Escrow From Abroad', 'duration' => 16]]],
                    ['title' => 'Investing Across Borders', 'lessons' => [['title' => 'MMFs, Bonds & REITs You Can Run Remotely', 'duration' => 18], ['title' => 'The Remittance Strategy', 'duration' => 13]]],
                    ['title' => 'The Return Plan', 'lessons' => [['title' => 'Building the Landing Pad', 'duration' => 12], ['title' => 'Legacy Across Jurisdictions', 'duration' => 14]]],
                ],
            ],
        ];

        foreach ($catalogue as $data) {
            $modules = $data['modules'];
            unset($data['modules']);

            // Drafts for now: these were placeholders with no real lessons/videos.
            // Publish from admin once real content is added.
            $course = Course::updateOrCreate(['slug' => $data['slug']], [...$data, 'is_published' => false]);
            $course->modules()->delete();

            foreach ($modules as $mIndex => $moduleData) {
                $module = $course->modules()->create([
                    'title' => $moduleData['title'],
                    'sort' => $mIndex,
                ]);

                foreach ($moduleData['lessons'] as $lIndex => $lesson) {
                    $module->lessons()->create([
                        'title' => $lesson['title'],
                        'slug' => Str::slug($lesson['title']),
                        'duration_minutes' => $lesson['duration'],
                        'is_free_preview' => $lesson['free'] ?? false,
                        'sort' => $lIndex,
                    ]);
                }
            }
        }
    }
}
