<?php

namespace App\Console\Commands;

use App\Models\Ecommerce\Product\Product\Product;
use Illuminate\Console\Command;

class SyncProductPropertiesJson extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'products:sync-properties-json';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Syncs all product properties to the new JSON column';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Starting sync of product properties JSON...');

        // Using cursor to be memory efficient
        $products = Product::cursor();
        $count = Product::count();

        $bar = $this->output->createProgressBar($count);
        $bar->start();

        foreach ($products as $product) {
            $product->syncPropertiesJson();
            $bar->advance();
        }

        $bar->finish();
        $this->newLine();
        $this->info('Sync completed successfully!');
    }
}
