<?php

namespace Modules\Dummy\Database\Seeders\Modules\Subscription\versions\v6_9_0;

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Modules\Subscription\Entities\{
    PackageSubscription,
    PackageSubscriptionMeta
};

class PackageSubscriptionsMetaTableSeeder extends Seeder
{

    /**
     * Auto generated seed file
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        DB::transaction(function () {
            $subscriptionIds = PackageSubscription::pluck('id');

            foreach ($subscriptionIds as $subscriptionId) {
                $meta = PackageSubscriptionMeta::where('package_subscription_id', $subscriptionId)
                    ->where('key', 'featureAccess')
                    ->first();

                if (!$meta) {
                    continue;
                }

                $features = json_decode($meta->value, true);
                if (!is_array($features)) {
                    continue;
                }

                if (!isset($features['ai_social_suites'])) {
                    $features['ai_social_suites'] = '1';
                    DB::table('package_subscriptions_meta')
                        ->where('id', $meta->id)
                        ->update(['value' => json_encode($features)]);
                }
            }
        });
    }
}
