<?php

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

return new class extends Migration {
    public function up()
    {
        if (Schema::hasTable('recharge_channel')) {
            $this->upsertRechargeChannel(1, 'JD钱包', 0, 100, 49999);
            $this->upsertRechargeChannel(2, 'JD钱包', 0.002, 100, 49999);
        }

        if (Schema::hasTable('recharge_channel_group')) {
            $this->appendGroupType('recharge_type');
            $this->appendGroupType('withdraw_type');
        }
    }

    public function down()
    {
        if (Schema::hasTable('recharge_channel')) {
            DB::table('recharge_channel')->where('type', 'JDpay')->delete();
        }

        if (Schema::hasTable('recharge_channel_group')) {
            $this->removeGroupType('recharge_type');
            $this->removeGroupType('withdraw_type');
        }
    }

    private function upsertRechargeChannel(int $dataType, string $name, float $rate, int $min, int $max): void
    {
        $row = [
            'data_type' => $dataType,
            'name' => $name,
            'type' => 'JDpay',
            'rate' => $rate,
            'min' => $min,
            'max' => $max,
        ];

        foreach (['key' => 'JDpay', 'fixed' => '', 'status' => 1, 'sort' => 99, 'from' => 1] as $column => $value) {
            if (Schema::hasColumn('recharge_channel', $column)) {
                $row[$column] = $value;
            }
        }

        DB::table('recharge_channel')->updateOrInsert(
            ['data_type' => $dataType, 'type' => 'JDpay'],
            $row
        );
    }

    private function appendGroupType(string $column): void
    {
        if (!Schema::hasColumn('recharge_channel_group', $column)) {
            return;
        }

        DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column) {
            foreach ($groups as $group) {
                $types = array_values(array_filter(explode(',', (string)$group->{$column})));
                if (!in_array('JDpay', $types, true)) {
                    $types[] = 'JDpay';
                    DB::table('recharge_channel_group')->where('id', $group->id)->update([
                        $column => implode(',', $types),
                    ]);
                }
            }
        });
    }

    private function removeGroupType(string $column): void
    {
        if (!Schema::hasColumn('recharge_channel_group', $column)) {
            return;
        }

        DB::table('recharge_channel_group')->orderBy('id')->chunkById(100, function ($groups) use ($column) {
            foreach ($groups as $group) {
                $types = array_values(array_diff(array_filter(explode(',', (string)$group->{$column})), ['JDpay']));
                DB::table('recharge_channel_group')->where('id', $group->id)->update([
                    $column => implode(',', $types),
                ]);
            }
        });
    }
};
