<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Inertia\Inertia;

class MissionController extends Controller
{
    private function missionPayload(): array
    {
        return [
            'user' => [
                'name' => '01792618309',
                'avatar' => 'https://i.pravatar.cc/180?img=32',
                'balance' => '৳ 0.00',
            ],
            'tabs' => [
                ['key' => 'progress', 'label' => 'In Progress'],
                ['key' => 'coming', 'label' => 'Coming soon'],
                ['key' => 'finished', 'label' => 'Finished'],
            ],
            'missions' => [
                [
                    'key' => 'new-member-daily-2026-03-28',
                    'status' => 'progress',
                    'title' => 'New Member Daily ...',
                    'date_range' => '2026.03.28~2026.03.29',
                    'remaining' => '08:32:14',
                    'milestones_done' => 0,
                    'milestones_total' => 7,
                    'progress_percent' => 0,
                    'medal_type' => 'gold',
                    'tasks' => [
                        ['target' => '100.00', 'current' => '0', 'reward_title' => 'Ramadan Wishing Wheel', 'reward_qty' => '1', 'bonus' => '1.00'],
                        ['target' => '1,000.00', 'current' => '0', 'reward_title' => 'Ramadan Wishing Wheel', 'reward_qty' => '1', 'bonus' => '5.00'],
                        ['target' => '5,000.00', 'current' => '0', 'reward_title' => 'Ramadan Wishing Wheel', 'reward_qty' => '1', 'bonus' => '10.00'],
                        ['target' => '10,000.00', 'current' => '0', 'reward_title' => 'Lucky red envelope rain', 'reward_qty' => '1', 'bonus' => '15.00'],
                        ['target' => '30,000.00', 'current' => '0', 'reward_title' => 'Lucky red envelope rain', 'reward_qty' => '1', 'bonus' => '30.00'],
                        ['target' => '60,000.00', 'current' => '0', 'reward_title' => 'S GoldenEgg', 'reward_qty' => '1', 'bonus' => '50.00'],
                        ['target' => '100,000.00', 'current' => '0', 'reward_title' => 'S GoldenEgg', 'reward_qty' => '1', 'bonus' => '100.00'],
                    ],
                    'rules_html' => '
                        <h2>নতুন সদস্যের দৈনিক চ্যালেঞ্জ</h2>
                        <p style="color:#35c55a;font-weight:700;">লক্ষ্য দর্শক: ভিআইপি ০ থেকে ভিআইপি ৪ এর সদস্য</p>

                        <h3 style="color:#ef7c1c;">মাইলস্টোন বোনাস টেবিল</h3>
                        <table border="1" cellspacing="0" cellpadding="8">
                            <tr>
                                <th>ধারাবাহিকতা</th>
                                <th>লটারি</th>
                                <th>অতিরিক্ত বোনাস</th>
                            </tr>
                            <tr><td>1</td><td>নাই</td><td>1</td></tr>
                            <tr><td>2</td><td>নাই</td><td>5</td></tr>
                            <tr><td>3</td><td>নাই</td><td>10</td></tr>
                            <tr><td>4</td><td>লাল খাম</td><td>15</td></tr>
                            <tr><td>5</td><td>লাল খাম</td><td>30</td></tr>
                            <tr><td>6</td><td>সুবর্ণ ডিম</td><td>50</td></tr>
                            <tr><td>7</td><td>সুবর্ণ ডিম</td><td>100</td></tr>
                        </table>

                        <h3 style="color:#ef7c1c;">র্যাংকিং প্রাইজ টেবিল</h3>
                        <table border="1" cellspacing="0" cellpadding="8">
                            <tr>
                                <th>র্যাঙ্ক</th>
                                <th>বোনাস</th>
                            </tr>
                            <tr><td>1</td><td>2000</td></tr>
                            <tr><td>2</td><td>900</td></tr>
                            <tr><td>3</td><td>800</td></tr>
                            <tr><td>4</td><td>700</td></tr>
                            <tr><td>5</td><td>600</td></tr>
                            <tr><td>6</td><td>500</td></tr>
                            <tr><td>7</td><td>400</td></tr>
                            <tr><td>8</td><td>300</td></tr>
                            <tr><td>9</td><td>200</td></tr>
                            <tr><td>10</td><td>100</td></tr>
                        </table>

                        <p style="color:#ef7c1c;font-weight:700;">
                            আপনি যদি এগুলোর সবগুলো অর্জন করেন তবে আপনি ২টি লাল খাম + ২টি সোনার পাবেন
                            এবং আপনি ১০০,০০০ টাকা পর্যন্ত বোনাস + ২১১ অতিরিক্ত নির্দিষ্ট বোনাস +
                            ১,০০০ টাকার প্রতিযোগিতার র্যাংকিং বোনাস পেতে পারেন।
                        </p>
                    ',
                    'leaderboard' => [
                        'top3' => [
                            ['rank' => 2, 'name' => 'liz******', 'avatar' => 'https://i.pravatar.cc/120?img=32', 'bet' => '1,092,252.80'],
                            ['rank' => 1, 'name' => 'mag******', 'avatar' => 'https://i.pravatar.cc/140?img=32', 'bet' => '1,250,271.62'],
                            ['rank' => 3, 'name' => 'dra******', 'avatar' => 'https://i.pravatar.cc/120?img=32', 'bet' => '765,253.78'],
                        ],
                        'others' => [
                            ['rank' => 4, 'name' => '018*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '460,822.44'],
                            ['rank' => 5, 'name' => '017*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '413,646.00'],
                            ['rank' => 6, 'name' => 't33*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '321,713.85'],
                            ['rank' => 7, 'name' => '016*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '308,136.10'],
                            ['rank' => 8, 'name' => '017*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '229,354.90'],
                            ['rank' => 9, 'name' => '013*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '166,803.32'],
                            ['rank' => 10, 'name' => '016*****', 'avatar' => 'https://i.pravatar.cc/90?img=32', 'bet' => '162,513.70'],
                        ],
                    ],
                ],
                [
                    'key' => 'new-member-daily-2026-03-29',
                    'status' => 'coming',
                    'title' => 'New Member Daily ...',
                    'date_range' => '2026.03.29~2026.03.30',
                    'remaining' => '----',
                    'milestones_done' => 0,
                    'milestones_total' => 7,
                    'progress_percent' => 0,
                    'medal_type' => 'disabled',
                    'tasks' => [
                        ['target' => '100.00', 'current' => '0', 'reward_title' => 'Ramadan Wishing Wheel', 'reward_qty' => '1', 'bonus' => '1.00'],
                        ['target' => '1,000.00', 'current' => '0', 'reward_title' => 'Ramadan Wishing Wheel', 'reward_qty' => '1', 'bonus' => '5.00'],
                        ['target' => '5,000.00', 'current' => '0', 'reward_title' => 'Ramadan Wishing Wheel', 'reward_qty' => '1', 'bonus' => '10.00'],
                        ['target' => '10,000.00', 'current' => '0', 'reward_title' => 'Lucky red envelope rain', 'reward_qty' => '1', 'bonus' => '15.00'],
                        ['target' => '30,000.00', 'current' => '0', 'reward_title' => 'Lucky red envelope rain', 'reward_qty' => '1', 'bonus' => '30.00'],
                        ['target' => '60,000.00', 'current' => '0', 'reward_title' => 'S GoldenEgg', 'reward_qty' => '1', 'bonus' => '50.00'],
                        ['target' => '100,000.00', 'current' => '0', 'reward_title' => 'S GoldenEgg', 'reward_qty' => '1', 'bonus' => '100.00'],
                    ],
                    'rules_html' => '',
                    'leaderboard' => ['top3' => [], 'others' => []],
                ],
                [
                    'key' => 'new-member-daily-2026-03-27',
                    'status' => 'finished',
                    'title' => 'New Member Daily ...',
                    'date_range' => '2026.03.27~2026.03.28',
                    'remaining' => '----',
                    'milestones_done' => 0,
                    'milestones_total' => 7,
                    'progress_percent' => 0,
                    'medal_type' => 'silver',
                    'tasks' => [],
                    'rules_html' => '',
                    'leaderboard' => ['top3' => [], 'others' => []],
                ],
                [
                    'key' => 'new-member-daily-2026-03-26',
                    'status' => 'finished',
                    'title' => 'New Member Daily ...',
                    'date_range' => '2026.03.26~2026.03.27',
                    'remaining' => '----',
                    'milestones_done' => 0,
                    'milestones_total' => 7,
                    'progress_percent' => 0,
                    'medal_type' => 'silver',
                    'tasks' => [],
                    'rules_html' => '',
                    'leaderboard' => ['top3' => [], 'others' => []],
                ],
                [
                    'key' => 'new-member-daily-2026-03-25',
                    'status' => 'finished',
                    'title' => 'New Member Daily ...',
                    'date_range' => '2026.03.25~2026.03.26',
                    'remaining' => '----',
                    'milestones_done' => 0,
                    'milestones_total' => 7,
                    'progress_percent' => 0,
                    'medal_type' => 'silver',
                    'tasks' => [],
                    'rules_html' => '',
                    'leaderboard' => ['top3' => [], 'others' => []],
                ],
            ],
        ];
    }

    public function index(Request $request)
    {
        $payload = $this->missionPayload();
        $status = $request->get('status', 'progress');

        $missions = collect($payload['missions'])
            ->where('status', $status)
            ->values()
            ->all();

        return Inertia::render('CkComponents/Mission/Index', [
            'userInfo' => $payload['user'],
            'tabs' => $payload['tabs'],
            'status' => $status,
            'missions' => $missions,
        ]);
    }

    public function rules(string $missionKey)
    {
        $mission = collect($this->missionPayload()['missions'])->firstWhere('key', $missionKey);

        abort_unless($mission, 404);

        return Inertia::render('CkComponents/Mission/Rules', [
            'mission' => [
                'key' => $mission['key'],
                'title' => $mission['title'],
                'rules_html' => $mission['rules_html'],
            ],
        ]);
    }

    public function leaderboard(string $missionKey)
    {
        $mission = collect($this->missionPayload()['missions'])->firstWhere('key', $missionKey);

        abort_unless($mission, 404);

        return Inertia::render('CkComponents/Mission/Leaderboard', [
            'mission' => $mission,
        ]);
    }
}