<?php

namespace Database\Seeders;

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

class ChamaHealthSeeder extends Seeder
{
    public function run(): void
    {
        $assessment = Assessment::updateOrCreate(
            ['slug' => 'chama-health'],
            [
                'title' => 'Chama Health Check',
                'tagline' => 'Chama, stokvel, susu or esusu, is your investment group built to last, or quietly heading for the famous group drama?',
                'description' => 'Ten honest questions about governance, money handling, legal standing and growth. Get your group\'s health score and the exact fixes that protect everyone\'s contributions.',
                'emoji' => 'handshake',
                'est_minutes' => 2,
                'cooldown_days' => 90,
                'is_active' => true,
            ]
        );

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

        $sections = [
            'governance' => ['label' => 'Governance', 'feedback' => 'Good. Now, the money itself.'],
            'money' => ['label' => 'Money Handling', 'feedback' => 'Almost there. Two more areas: paperwork and growth.'],
            'legal' => ['label' => 'Legal Standing', 'feedback' => null],
            'growth' => ['label' => 'Growth & Strategy', 'feedback' => null],
        ];

        $questions = [
            // Governance
            [
                'dimension' => 'governance',
                'prompt' => 'Does your chama have a written constitution that members actually follow?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'check-circle', 'label' => 'Yes, written, signed, and referenced when disputes arise', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'file-text', 'label' => 'It exists somewhere, but nobody has read it in years', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'x', 'label' => 'No, we run on trust and vibes', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'governance',
                'prompt' => 'How are leaders chosen, and changed?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'check-circle', 'label' => 'Regular elections with defined terms', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'users', 'label' => 'The founders lead, it\'s never really been discussed', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'alert-triangle', 'label' => 'One person controls everything', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'governance',
                'prompt' => 'What happens when a member wants to leave?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'check-circle', 'label' => 'Clear written exit process, valuation, timeline, payout', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'meh', 'label' => 'We negotiate case by case, it usually gets tense', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'alert-triangle', 'label' => 'Nobody leaves. It\'s complicated', 'points' => 0],
                ],
            ],
            // Money
            [
                'dimension' => 'money',
                'prompt' => 'Who can move the chama\'s money?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'shield-check', 'label' => 'Bank account with multiple signatories, no single person can transact', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'phone', 'label' => 'Treasurer\'s mobile money or personal account', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'eye-off', 'label' => 'Honestly, we\'re not sure', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'money',
                'prompt' => 'How do members see the records?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'chart-bar', 'label' => 'Regular statements, every contribution and expense visible to all', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'file-text', 'label' => 'Records exist but you have to ask the treasurer', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'x', 'label' => 'Records? The treasurer\'s memory is the record', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'money',
                'prompt' => 'What happens when someone misses contributions?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'check-circle', 'label' => 'Written penalty rules, applied the same to everyone', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'meh', 'label' => 'Depends who it is, some members are "untouchable"', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'alert-triangle', 'label' => 'Nothing. Arrears just pile up quietly', 'points' => 0],
                ],
            ],
            // Legal
            [
                'dimension' => 'legal',
                'prompt' => 'Is the chama formally registered?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'badge-check', 'label' => 'Yes, registered (self-help group, society or company)', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'hourglass', 'label' => 'Registration is "in progress"… for a while now', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'x', 'label' => 'No, it\'s informal', 'points' => 0],
                ],
            ],
            [
                'dimension' => 'legal',
                'prompt' => 'Assets the chama buys (land, shares, equipment) are held in…',
                'options' => [
                    ['key' => 'a', 'emoji' => 'shield-check', 'label' => 'The chama\'s registered name', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'users', 'label' => 'A trusted member\'s name "for convenience"', 'points' => 0],
                    ['key' => 'c', 'emoji' => 'meh', 'label' => 'We don\'t own assets yet', 'points' => 2],
                ],
            ],
            // Growth
            [
                'dimension' => 'growth',
                'prompt' => 'Does the chama have a written investment strategy?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'target', 'label' => 'Yes, goals, risk rules and timelines we review yearly', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'lightbulb', 'label' => 'We invest in whatever sounds good at the meeting', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'landmark', 'label' => 'We only save, the money sits in the account', 'points' => 1],
                ],
            ],
            [
                'dimension' => 'growth',
                'prompt' => 'Last one, how are big investment decisions made?',
                'options' => [
                    ['key' => 'a', 'emoji' => 'check-circle', 'label' => 'Documented vote after research is shared in advance', 'points' => 3],
                    ['key' => 'b', 'emoji' => 'users', 'label' => 'Whoever talks loudest in the meeting wins', 'points' => 1],
                    ['key' => 'c', 'emoji' => 'alert-triangle', 'label' => 'The chairperson decides, the rest discover later', 'points' => 0],
                ],
            ],
        ];

        $sort = 0;
        $current = null;
        foreach ($questions as $q) {
            $assessment->allQuestions()->create([
                'section' => $sections[$q['dimension']]['label'],
                'prompt' => $q['prompt'],
                'type' => 'choice',
                'dimension' => $q['dimension'],
                'options' => $q['options'],
                'sort' => $sort++,
                'is_active' => true,
            ]);
        }

        foreach ($sections as $dimension => $meta) {
            if ($meta['feedback']) {
                $assessment->allQuestions()->where('dimension', $dimension)->orderByDesc('sort')->first()
                    ?->update(['section_feedback' => $meta['feedback']]);
            }
        }

        $bands = [
            [
                'min_percent' => 0, 'max_percent' => 39,
                'label' => 'The Drama Waiting to Happen', 'emoji' => 'alert-triangle', 'color' => '#f43f5e',
                'headline' => 'Your chama runs on goodwill, and goodwill always runs out.',
                'message' => 'Right now the group works because nothing has gone wrong yet. But every famous chama collapse, the vanished treasurer, the disputed plot, the founder who "borrowed" the kitty, started exactly here: no constitution, no transparency, assets in personal names. The good news? Every fix is straightforward, and starting them this month protects every shilling members have contributed.',
                'advice' => [
                    'Write a one-page constitution this month: leadership terms, exit process, penalty rules. Sign it together.',
                    'Open a group bank account with at least 3 signatories, retire the personal mobile-money wallet immediately.',
                    'Start sharing a simple monthly statement: money in, money out, balance. Transparency kills suspicion.',
                ],
            ],
            [
                'min_percent' => 40, 'max_percent' => 69,
                'label' => 'The Solid Hustle', 'emoji' => 'layers', 'color' => '#f59e0b',
                'headline' => 'The foundation is real, now close the gaps before they\'re tested.',
                'message' => 'Your chama has structure where it counts, and that already puts you ahead of most. The gaps you have are the quiet kind: the exit process nobody wrote down, the asset in a member\'s name, the strategy that lives in the chairperson\'s head. Groups rarely notice these gaps until a dispute, a death or a windfall tests them, and by then it\'s expensive.',
                'advice' => [
                    'Formalise the missing piece your score flagged, usually registration or the written exit process.',
                    'Move any assets held in personal names into the chama\'s registered name now, while everyone is friends.',
                    'Write a one-page investment policy: what you invest in, what you never touch, and how you vote.',
                ],
            ],
            [
                'min_percent' => 70, 'max_percent' => 100,
                'label' => 'The Institution', 'emoji' => 'building', 'color' => '#1c2b5f',
                'headline' => 'This isn\'t a chama anymore, it\'s an investment firm in the making.',
                'message' => 'Governance, transparency, legal standing, strategy, your group has the bones of something that can outlive its founders. Groups like yours are how family wealth and even SACCOs begin. Your next frontier is scale: professional-grade investments, possibly converting to a formal investment company, and onboarding the next generation without diluting the discipline.',
                'advice' => [
                    'Explore bigger vehicles: treasury bills and bonds via your central bank\'s retail portal, REITs, or a managed group portfolio.',
                    'Consider formal conversion (investment co-op or limited company) for tax and liability advantages.',
                    'Document your "operating system" so new members inherit the discipline, not just the WhatsApp group.',
                ],
            ],
        ];

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