<?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\{
    Package,
    PackageMeta
};

class PackagesMetaTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        DB::transaction(function () {
            $packageIds = Package::pluck('id');

            foreach ($packageIds as $packageId) {
                $meta = PackageMeta::where('package_id', $packageId)
                    ->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('packages_meta')
                        ->where('id', $meta->id)
                        ->update(['value' => json_encode($features)]);
                }
            }
        });
    }
}
