<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('leadership_ranks')) {
            return;
        }

        $now = now();
        $rows = [
            ['key' => 'performance_leader', 'name' => 'পারফরম্যান্স লিডার', 'sort_order' => 1],
            ['key' => 'student_leader',     'name' => 'স্টুডেন্ট লিডার',     'sort_order' => 2],
            ['key' => 'rising_leader',      'name' => 'রাইজিং লিডার',       'sort_order' => 3],
            ['key' => 'power_leader',       'name' => 'পাওয়ার লিডার',        'sort_order' => 4],
            ['key' => 'power_max_leader',   'name' => 'পাওয়ার ম্যাক্স লিডার', 'sort_order' => 5],
        ];

        foreach ($rows as $row) {
            DB::table('leadership_ranks')->updateOrInsert(
                ['key' => $row['key']],
                [
                    'name'                   => $row['name'],
                    'leader_name'            => null,
                    'display_title'          => null,
                    'display_image'          => null,
                    'notice_text'            => 'আপডেট নোটিশ শীঘ্রই আসছে',
                    'need_verified_members'  => 0,
                    'need_gmail_completed'   => 0,
                    'need_fb_approved'       => 0,
                    'reward_amount'          => 0,
                    'sort_order'             => $row['sort_order'],
                    'is_active'              => 1,
                    'updated_at'             => $now,
                    'created_at'             => $now,
                ]
            );
        }
    }

    public function down(): void
    {
        if (!Schema::hasTable('leadership_ranks')) {
            return;
        }

        $keys = [
            'performance_leader',
            'student_leader',
            'rising_leader',
            'power_leader',
            'power_max_leader',
        ];

        DB::table('leadership_ranks')->whereIn('key', $keys)->delete();
    }
};
