<?php

namespace Database\Seeders;

use App\Models\Assessment;
use Illuminate\Database\Seeder;

class FinancialHealthSeeder extends Seeder
{
    public function run(): void
    {
        $assessment = Assessment::updateOrCreate(
            ['slug' => 'financial-health'],
            [
                'title' => 'Financial Health Check',
                'tagline' => 'Your money score in 3 minutes, honest, judgement-free, jargon-free.',
                'description' => 'Answer 20 quick questions about how you earn, spend, save, borrow and invest. We\'ll show you exactly where you stand, and the very next step to take.',
                'emoji' => 'heart-pulse',
                'est_minutes' => 3,
                'cooldown_days' => 30,
                'is_active' => true,
            ]
        );

        $assessment->allQuestions()->delete();
        $assessment->bands()->delete();

        $sections = [
            'spending' => [
                'label' => 'Money Habits',
                'icon' => 'wallet',
                'feedback' => 'Great start! Now let\'s talk about your safety net.',
            ],
            'saving' => [
                'label' => 'Your Safety Net',
                'icon' => 'life-buoy',
                'feedback' => 'Halfway there, you\'re doing great. Next up: debt.',
            ],
            'debt' => [
                'label' => 'Debt Freedom',
                'icon' => 'unlink',
                'feedback' => 'Almost done! Let\'s see how your money is growing.',
            ],
            'investing' => [
                'label' => 'Growing Wealth',
                'icon' => 'trending-up',
                'feedback' => 'Last stretch, how protected is everything you\'re building?',
            ],
            'protection' => [
                'label' => 'Protecting Tomorrow',
                'icon' => 'shield-check',
                'feedback' => null,
            ],
        ];

        $questions = [
            // ---------- MONEY HABITS (spending) ----------
            [
                'dimension' => 'spending',
                'prompt' => 'Payday! What happens in the first 48 hours?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'target', 'label' => 'My money already has assignments, savings and bills go out first', 'points' => 3],
                    ['key' => 'b', 'icon' => 'receipt', 'label' => 'I pay the important bills, then play it by ear', 'points' => 2],
                    ['key' => 'c', 'icon' => 'smile', 'label' => 'A little enjoyment first, then I get serious', 'points' => 1],
                    ['key' => 'd', 'icon' => 'wind', 'label' => 'Honestly? It disappears and I can\'t explain where', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'spending',
                'prompt' => 'Do you know, roughly, what you spent last month?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'chart-bar', 'label' => 'Yes, I track it, I could tell you per category', 'points' => 3],
                    ['key' => 'b', 'icon' => 'search', 'label' => 'I have a rough idea', 'points' => 2],
                    ['key' => 'c', 'icon' => 'receipt', 'label' => 'Only the big things, rent, fees, transport', 'points' => 1],
                    ['key' => 'd', 'icon' => 'eye-off', 'label' => 'No idea. My mobile money knows, I don\'t', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'spending',
                'prompt' => 'A friend invites you for a weekend trip you didn\'t plan for. What do you do?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'check-circle', 'label' => 'Check my fun budget, if it fits, I go; if not, I pass', 'points' => 3],
                    ['key' => 'b', 'icon' => 'scale', 'label' => 'I go, but I cut back somewhere else to cover it', 'points' => 2],
                    ['key' => 'c', 'icon' => 'hourglass', 'label' => 'I go and figure out the damage later', 'points' => 1],
                    ['key' => 'd', 'icon' => 'alert-triangle', 'label' => 'I borrow to make it happen', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'spending',
                'prompt' => 'How do you feel when you check your account balance?',
                'type' => 'emoji_scale',
                'options' => [
                    ['key' => 'a', 'icon' => 'smile', 'label' => 'Calm, I know what I\'ll find', 'points' => 3],
                    ['key' => 'b', 'icon' => 'meh', 'label' => 'Neutral, sometimes good, sometimes not', 'points' => 2],
                    ['key' => 'c', 'icon' => 'frown', 'label' => 'A bit anxious, to be honest', 'points' => 1],
                    ['key' => 'd', 'icon' => 'eye-off', 'label' => 'I avoid checking it altogether', 'points' => 0],
                ],
            ],

            // ---------- SAFETY NET (saving) ----------
            [
                'dimension' => 'saving',
                'prompt' => 'If you lost your income today, how long could you keep the lights on?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'landmark', 'label' => '6 months or more, I\'ve built a real cushion', 'points' => 3],
                    ['key' => 'b', 'icon' => 'home', 'label' => 'About 3 months', 'points' => 2],
                    ['key' => 'c', 'icon' => 'hourglass', 'label' => 'A few weeks, maybe a month', 'points' => 1],
                    ['key' => 'd', 'icon' => 'wind', 'label' => 'I\'d be in trouble by next week', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'saving',
                'prompt' => 'How does saving actually happen in your life?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'repeat', 'label' => 'Automatically, it leaves before I can touch it', 'points' => 3],
                    ['key' => 'b', 'icon' => 'check-circle', 'label' => 'Manually, but I\'m consistent most months', 'points' => 2],
                    ['key' => 'c', 'icon' => 'hourglass', 'label' => 'Only when there\'s something left over', 'points' => 1],
                    ['key' => 'd', 'icon' => 'frown', 'label' => 'Saving? In this economy?', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'saving',
                'prompt' => 'An emergency hits, money is needed today. Where does it come from?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'life-buoy', 'label' => 'My emergency fund, that\'s what it\'s for', 'points' => 3],
                    ['key' => 'b', 'icon' => 'landmark', 'label' => 'My savings, though it would sting', 'points' => 2],
                    ['key' => 'c', 'icon' => 'users', 'label' => 'Family, friends, my chama or stokvel', 'points' => 1],
                    ['key' => 'd', 'icon' => 'alert-circle', 'label' => 'A mobile loan, Fuliza, Tala, OPay, MoMo…', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'saving',
                'prompt' => 'Are you saving towards something specific right now?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'target', 'label' => 'Yes, clear goals with amounts and dates', 'points' => 3],
                    ['key' => 'b', 'icon' => 'map', 'label' => 'Sort of, I have goals but no real plan', 'points' => 2],
                    ['key' => 'c', 'icon' => 'moon', 'label' => 'I save generally, for "the future"', 'points' => 1],
                    ['key' => 'd', 'icon' => 'x', 'label' => 'No goals, no saving happening yet', 'points' => 0],
                ],
            ],

            // ---------- DEBT FREEDOM (debt) ----------
            [
                'dimension' => 'debt',
                'prompt' => 'What\'s your current relationship with debt?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'badge-check', 'label' => 'Debt-free, or only debt that builds assets', 'points' => 3],
                    ['key' => 'b', 'icon' => 'chart-line', 'label' => 'I have debt, but a clear plan and I\'m on schedule', 'points' => 2],
                    ['key' => 'c', 'icon' => 'repeat', 'label' => 'I\'m juggling a few loans, it\'s getting heavy', 'points' => 1],
                    ['key' => 'd', 'icon' => 'alert-triangle', 'label' => 'I borrow from one place to pay another', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'debt',
                'prompt' => 'Quick check: do you know the TOTAL amount you owe right now?',
                'type' => 'this_or_that',
                'options' => [
                    ['key' => 'a', 'icon' => 'calculator', 'label' => 'Yes, to the shilling', 'points' => 3],
                    ['key' => 'b', 'icon' => 'eye-off', 'label' => 'Not exactly… I\'d have to add it up', 'points' => 1],
                ],
            ],
            [
                'dimension' => 'debt',
                'prompt' => 'Mobile loans (Fuliza, OPay, MoMo, app loans…), how often do they feature in your month?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'shield-check', 'label' => 'Never, I don\'t touch them', 'points' => 3],
                    ['key' => 'b', 'icon' => 'moon', 'label' => 'Rarely, and I clear them immediately', 'points' => 2],
                    ['key' => 'c', 'icon' => 'repeat', 'label' => 'Most months, to bridge the gap', 'points' => 1],
                    ['key' => 'd', 'icon' => 'alert-circle', 'label' => 'I live inside the overdraft at this point', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'debt',
                'prompt' => 'Before taking a loan, what\'s your process?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'search', 'label' => 'I check the true cost, terms, and exactly how I\'ll repay', 'points' => 3],
                    ['key' => 'b', 'icon' => 'file-text', 'label' => 'I look at the monthly payment and decide if I can manage', 'points' => 2],
                    ['key' => 'c', 'icon' => 'rocket', 'label' => 'If I qualify, I take it and trust myself to sort it', 'points' => 1],
                    ['key' => 'd', 'icon' => 'alert-triangle', 'label' => 'Process? If I need money, I borrow', 'points' => 0],
                ],
            ],

            // ---------- GROWING WEALTH (investing) ----------
            [
                'dimension' => 'investing',
                'prompt' => 'Is your money making money while you sleep?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'trending-up', 'label' => 'Yes, I invest regularly in things I understand', 'points' => 3],
                    ['key' => 'b', 'icon' => 'sprout', 'label' => 'I\'ve started small, a savings account, money market fund or SACCO, a little here and there', 'points' => 2],
                    ['key' => 'c', 'icon' => 'lightbulb', 'label' => 'Not yet, but I\'m genuinely planning to start', 'points' => 1],
                    ['key' => 'd', 'icon' => 'moon', 'label' => 'My money sleeps when I sleep', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'investing',
                'prompt' => 'Someone offers you a "guaranteed 30% per month" opportunity. You…',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'flag', 'label' => 'Run. If it sounds too good to be true, it is', 'points' => 3],
                    ['key' => 'b', 'icon' => 'search', 'label' => 'Dig into it properly before deciding', 'points' => 2],
                    ['key' => 'c', 'icon' => 'hourglass', 'label' => 'Put in a small amount "to test"', 'points' => 1],
                    ['key' => 'd', 'icon' => 'rocket', 'label' => 'Jump in before the window closes!', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'investing',
                'prompt' => 'How do you choose where to invest?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'compass', 'label' => 'Based on my own goals, values and timelines', 'points' => 3],
                    ['key' => 'b', 'icon' => 'book-open', 'label' => 'I research products and compare options', 'points' => 2],
                    ['key' => 'c', 'icon' => 'users', 'label' => 'I follow what friends/family recommend', 'points' => 1],
                    ['key' => 'd', 'icon' => 'alert-circle', 'label' => 'I haven\'t chosen anything yet, it\'s overwhelming', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'investing',
                'prompt' => 'Do you know the difference between saving and investing?',
                'type' => 'this_or_that',
                'options' => [
                    ['key' => 'a', 'icon' => 'lightbulb', 'label' => 'Yes, and I use both for different jobs', 'points' => 3],
                    ['key' => 'b', 'icon' => 'meh', 'label' => 'Honestly, they blur together for me', 'points' => 1],
                ],
            ],

            // ---------- PROTECTING TOMORROW (protection) ----------
            [
                'dimension' => 'protection',
                'prompt' => 'If you were unwell for 3 months, what happens to your finances?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'shield-check', 'label' => 'Covered, insurance plus savings would carry me', 'points' => 3],
                    ['key' => 'b', 'icon' => 'shield', 'label' => 'Insurance would help, but it would be tight', 'points' => 2],
                    ['key' => 'c', 'icon' => 'users', 'label' => 'Family and fundraising would have to step in', 'points' => 1],
                    ['key' => 'd', 'icon' => 'eye-off', 'label' => 'I genuinely don\'t want to think about it', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'protection',
                'prompt' => 'Retirement. When you think about it, you feel…',
                'type' => 'emoji_scale',
                'options' => [
                    ['key' => 'a', 'icon' => 'smile', 'label' => 'Confident, I\'m building towards it already', 'points' => 3],
                    ['key' => 'b', 'icon' => 'meh', 'label' => 'Hopeful, I\'ve started something small', 'points' => 2],
                    ['key' => 'c', 'icon' => 'frown', 'label' => 'Worried, I know I\'m behind', 'points' => 1],
                    ['key' => 'd', 'icon' => 'eye-off', 'label' => 'I\'m avoiding the topic completely', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'protection',
                'prompt' => 'Does anyone else depend on your income (kids, parents, siblings)? If so, are they protected if something happens to you?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'file-text', 'label' => 'Yes, I have life cover and my affairs are in order', 'points' => 3],
                    ['key' => 'b', 'icon' => 'shield', 'label' => 'Partially, some cover, but gaps exist', 'points' => 2],
                    ['key' => 'c', 'icon' => 'hourglass', 'label' => 'It\'s on my list, but nothing in place yet', 'points' => 1],
                    ['key' => 'd', 'icon' => 'x', 'label' => 'No one depends on me / nothing in place', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'protection',
                'prompt' => 'Last one! Do your money decisions reflect what truly matters to you, your values, your faith, your family?',
                'type' => 'choice',
                'options' => [
                    ['key' => 'a', 'icon' => 'heart', 'label' => 'Yes, my money follows my values, deliberately', 'points' => 3],
                    ['key' => 'b', 'icon' => 'compass', 'label' => 'Mostly, though I drift sometimes', 'points' => 2],
                    ['key' => 'c', 'icon' => 'map', 'label' => 'I\'ve never connected money to my values', 'points' => 1],
                    ['key' => 'd', 'icon' => 'wind', 'label' => 'My money runs me, not the other way around', 'points' => 0],
                ],
            ],
        ];

        $sort = 0;
        foreach ($questions as $q) {
            $sectionMeta = $sections[$q['dimension']];

            // Option icons are stored under the legacy 'emoji' key, holding x-icon names.
            $options = array_map(function ($option) {
                $option['emoji'] = $option['icon'];
                unset($option['icon']);

                return $option;
            }, $q['options']);

            $assessment->allQuestions()->create([
                'section' => $sectionMeta['label'],
                'section_feedback' => null,
                'prompt' => $q['prompt'],
                'help' => null,
                'type' => $q['type'],
                'dimension' => $q['dimension'],
                'options' => $options,
                'sort' => $sort++,
                'is_active' => true,
            ]);
        }

        // Attach interstitial feedback to the LAST question of each dimension
        foreach ($sections as $dimension => $meta) {
            if ($meta['feedback']) {
                $assessment->allQuestions()
                    ->where('dimension', $dimension)
                    ->orderByDesc('sort')
                    ->first()
                    ?->update(['section_feedback' => $meta['feedback']]);
            }
        }

        // ---------- Overall result bands (emoji column stores x-icon names) ----------
        $bands = [
            [
                'min_percent' => 0, 'max_percent' => 34,
                'label' => 'The Fresh Starter', 'emoji' => 'sprout', 'color' => '#f59e0b',
                'headline' => 'Every expert was once a beginner, and you just took the hardest step.',
                'message' => 'Your money is currently running the show, and that\'s exhausting, the paycheck-to-paycheck cycle, the borrowing to bridge gaps, the avoidance. Here\'s the truth: none of this means you\'re bad with money. It means nobody ever showed you a system. That changes today.',
                'advice' => [
                    'Start with ONE habit: track everything you spend for 7 days, no judgement, just awareness.',
                    'Open the Debt Payoff Planner and list every debt. Naming the number takes away its power.',
                    'Save something weekly, however small. The habit matters infinitely more than the amount.',
                ],
            ],
            [
                'min_percent' => 35, 'max_percent' => 59,
                'label' => 'The Foundation Builder', 'emoji' => 'layers', 'color' => '#58b9e6',
                'headline' => 'You\'ve started building, now let\'s make the foundation solid.',
                'message' => 'You\'re trying, and it shows: some saving here, some planning there. But your system has gaps, and money slips through them, an emergency wipes out your savings, a loan creeps in, a goal stays a dream. You don\'t need more discipline. You need a tighter system.',
                'advice' => [
                    'Build your emergency fund to 1 month of expenses before anything else, use the Emergency Fund tool to set the target.',
                    'Automate your saving: have it leave your account the day money comes in.',
                    'Clear mobile-loan debt completely, it\'s the most expensive money on the continent.',
                ],
            ],
            [
                'min_percent' => 60, 'max_percent' => 84,
                'label' => 'The Wealth Builder', 'emoji' => 'building', 'color' => '#36498b',
                'headline' => 'You\'re ahead of most, now make your money work as hard as you do.',
                'message' => 'Solid habits, a real safety net, debt under control, you\'ve done the hard part. Your next frontier is growth: investing deliberately, diversifying beyond the familiar, and connecting your money to your bigger goals so it\'s not just sitting safe, it\'s building your future.',
                'advice' => [
                    'Run the Compound Growth calculator, see what your current monthly saving becomes in 10 years if invested.',
                    'Diversify past one asset: money market, bonds, equities, matched to each goal\'s timeline.',
                    'Put your goals in writing with amounts and dates, vague goals get vague results.',
                ],
            ],
            [
                'min_percent' => 85, 'max_percent' => 100,
                'label' => 'The Money-Wise', 'emoji' => 'crown', 'color' => '#1c2b5f',
                'headline' => 'Exceptional. You manage money the way we wish everyone did.',
                'message' => 'You have the habits, the cushion, the plan and the protection. For you, the conversation shifts from building wealth to multiplying and preserving it, legacy, estate planning, giving with purpose, and possibly teaching others the way you\'ve mastered.',
                'advice' => [
                    'Pressure-test your protection: is your cover, will and estate plan current?',
                    'Explore wealth preservation, diversification across asset classes and borders.',
                    'Multiply your impact: mentor someone, or bring financial wellness to your workplace.',
                ],
            ],
        ];

        foreach ($bands as $i => $band) {
            $assessment->bands()->create([...$band, 'dimension' => null, 'sort' => $i]);
        }
    }
}
