<?php

namespace Database\Seeders;

use App\Models\MemberWeekScore;
use App\Models\Team;
use Illuminate\Database\Seeder;

class MemberWeekScoreSeeder extends Seeder
{
    public function run(): void
    {
        $weeks = ['w1', 'w2', 'w3', 'w4'];

        $visitorNames = [
            'Aarav Mehta', 'Priya Shah', 'Rohan Joshi', 'Neha Iyer', 'Karthik Rao',
            'Sanya Kapoor', 'Vikram Rai', 'Anaya Pillai', 'Ishaan Kohli', 'Meera Banerjee',
            'Aditya Verma', 'Riya Desai', 'Manish Nair', 'Pooja Sinha', 'Rahul Bhatt',
        ];

        // Hash-seeded RNG so the same team+member+week always gets identical results
        $rand = function (string $salt, int $max) {
            return crc32($salt) % max(1, $max);
        };
        $chance = function (string $salt, float $pct) {
            return (crc32($salt) % 100) < ($pct * 100);
        };

        $teams = Team::with('members')->get();

        foreach ($teams as $team) {
            $captainId = $team->captain_id;

            // Team-level bonuses (sprinkled across teams to make ranks differ)
            $teamRank = $team->id % 4;
            $team->mcm_bonus   = [0, 200, 100, 50][$teamRank] ?? 0;
            $team->tyfcb_bonus = [0, 300, 200, 100][$teamRank] ?? 0;
            $team->save();

            foreach ($weeks as $wIdx => $week) {
                foreach ($team->members as $mi => $member) {
                    $salt = "{$team->id}-{$member->id}-{$week}";

                    // Attendance — 80% present, 8% sub, 12% absent
                    $rPresence = (crc32($salt . '-pres') % 100);
                    $present = $rPresence < 80 ? 1 : 0;
                    $sub     = (!$present && $rPresence < 88) ? 1 : 0;

                    // Punctuality bias
                    $puncRoll = crc32($salt . '-punc') % 100;
                    $punctuality = $puncRoll < 25 ? 'punc_720'
                        : ($puncRoll < 65 ? 'punc_730'
                            : ($puncRoll < 88 ? 'punc_745' : 'punc_late'));

                    // Visitors — 35% chance any, 1-2 invited, occasional open/focused
                    $visitors = [];
                    if ($chance($salt . '-vis', 0.35)) {
                        $count = ($rand($salt . '-vcount', 100) < 70) ? 1 : 2;
                        for ($v = 0; $v < $count; $v++) {
                            $cRoll = crc32($salt . "-vcat$v") % 100;
                            $cat = $cRoll < 60 ? 'invited' : ($cRoll < 85 ? 'open' : ($cRoll < 97 ? 'focused' : 'induction'));
                            $visitors[] = [
                                'category' => $cat,
                                'name' => $visitorNames[crc32($salt . "-vname$v") % count($visitorNames)],
                            ];
                        }
                    }

                    // 121s — 50% chance, 1-2 entries
                    $o2oCount = 0;
                    if ($chance($salt . '-o2o', 0.55)) {
                        $o2oCount = ($rand($salt . '-o2oc', 100) < 60) ? 1 : 2;
                    }
                    $oneToOnes = [];
                    for ($i = 0; $i < $o2oCount; $i++) $oneToOnes[] = ['with' => ''];

                    // Referrals — 30% chance, 1-3
                    $refCount = 0;
                    if ($chance($salt . '-ref', 0.30)) {
                        $refRoll = crc32($salt . '-refc') % 100;
                        $refCount = $refRoll < 60 ? 1 : ($refRoll < 90 ? 2 : 3);
                    }
                    $referrals = [];
                    for ($i = 0; $i < $refCount; $i++) $referrals[] = ['to' => '', 'note' => ''];

                    $data = [
                        'present'      => $present,
                        'sub'          => $sub,
                        'punctuality'  => $punctuality,
                        'visitors'     => $visitors,
                        'one_to_ones'  => $oneToOnes,
                        'referrals'    => $referrals,
                        'testimonial'  => $chance($salt . '-test', 0.18) ? 1 : 0,
                        'specific_ask' => $chance($salt . '-ask', 0.15) ? 1 : 0,
                        'm2c_photo'    => ($member->id === $captainId && $chance($salt . '-m2c', 0.5)) ? 1 : 0,
                        'bni_connect'  => ($wIdx === 0 && $chance($salt . '-bni', 0.65)) ? 1 : 0,
                        'training'     => $chance($salt . '-trn', 0.25) ? 1 : 0,
                        'dance_card'   => null,
                        'tyfcb'        => 0,
                    ];

                    // Dance card — only in w1, tiered by member index
                    if ($wIdx === 0) {
                        $dcRoll = crc32($salt . '-dc') % 100;
                        $data['dance_card'] = $dcRoll < 30 ? 'dc_14'
                            : ($dcRoll < 55 ? 'dc_16'
                                : ($dcRoll < 75 ? 'dc_18'
                                    : ($dcRoll < 90 ? 'dc_21' : null)));
                    }

                    // TYFCB rupee amount — occasional
                    if ($chance($salt . '-tyf', 0.20)) {
                        $data['tyfcb'] = (50000 + (crc32($salt . '-tyfa') % 300000));
                    }

                    MemberWeekScore::updateOrCreate(
                        ['member_id' => $member->id, 'week' => $week],
                        ['data' => $data],
                    );
                }
            }
        }
    }
}
