<?php

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

class UpdateVoucherCampaignsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (env('APP_ENV') !== 'production') {
            DB::table('voucher_campaigns')->where('id', 1)->update(['campaign_id' => 'camp_snxv2JQlh5v9LEDBbIBOnibD']);
            DB::table('voucher_campaigns')->where('id', 2)->update(['campaign_id' => 'camp_lEM7WhLHFlhcRKRR0C4bXH0F']);
            DB::table('voucher_campaigns')->where('id', 3)->update(['campaign_id' => 'camp_JP9qBGBYEinVjzAE1vjynMe0']);
            DB::table('voucher_campaigns')->where('id', 4)->update(['campaign_id' => 'camp_zsUnlrlJB1G0TCu7YeLluAdR']);
            DB::table('voucher_campaigns')->where('id', 5)->update(['campaign_id' => 'camp_8ntTRkr3E5h0Ul1dR4UW8SBl']);
            DB::table('voucher_campaigns')->where('id', 6)->update(['campaign_id' => 'camp_YIHTJ3UxxjQDahWfrRaEwUil']);
            DB::table('voucher_campaigns')->where('id', 7)->update(['campaign_id' => 'camp_dNiljXXYU4CEfk2SwSRvgjHB']);

            $voucherCampaignIds = [1, 2, 3];
            foreach ($voucherCampaignIds as $id) {
                $campaignId = DB::table('voucher_campaigns')->where('id', $id)->value('campaign_id');
                $ownerId = DB::table('voucher_campaigns')->where('id', $id)->value('id');
                $upperCampaignId = strtoupper($campaignId);

                $keysToUpdate = DB::table('key_value_pairs')
                    ->where('key', 'like', '%_TOTAL%')
                    ->where('owner_type', 'App\Models\VoucherCampaign')
                    ->where('owner_id', (int) $ownerId)
                    ->get();

                foreach ($keysToUpdate as $entry) {
                    $parts = explode('_', $entry->key);
                    $lastTwoParts = array_slice($parts, -2); // Get the last two elements
                    $newKey = "{$upperCampaignId}_" . implode('_', $lastTwoParts);

                    DB::table('key_value_pairs')
                        ->where('id', $entry->id)
                        ->update(['key' => $newKey]);
                }
            }
        }

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (env('APP_ENV') !== 'production') {
            DB::table('voucher_campaigns')->where('id', 1)->update(['campaign_id' => '']);
            DB::table('voucher_campaigns')->where('id', 2)->update(['campaign_id' => '']);
            DB::table('voucher_campaigns')->where('id', 3)->update(['campaign_id' => '']);
            DB::table('voucher_campaigns')->where('id', 4)->update(['campaign_id' => '']);
            DB::table('voucher_campaigns')->where('id', 5)->update(['campaign_id' => '']);
            DB::table('voucher_campaigns')->where('id', 6)->update(['campaign_id' => '']);
            DB::table('voucher_campaigns')->where('id', 7)->update(['campaign_id' => '']);
        }
    }
}
