<?php

namespace App\Console\Commands;

use App\Models\SyncControl;
use App\Services\Navision\ProductsService;
use Illuminate\Console\Command;

class SyncNavProducts extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'noldus:sync-nav-products {--force : Force sync products: no modified date filter and marks to sync all}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Gets products from NAV and checks and creates/updates products locally if new or changed';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(ProductsService $service)
    {
        $forceSync = $this->option('force');

        // Product collections
        $service->getAndSyncOptions();
        // Simple Products
        $service->getAndSyncItems(SyncControl::TYPE_ITEMS, $forceSync);
        // Hiden bundle components
        $service->getAndSyncItems(SyncControl::TYPE_BUNDLE_ITEMS, $forceSync);
        // Resources
        $service->getAndSyncResources($forceSync);
        // Bundles
        $service->getAndSyncItems(SyncControl::TYPE_BUNDLES, $forceSync);
        // disable product
        $service->checkDisabledItems($forceSync);
        // disable resources
        $service->checkDisabledResources($forceSync);

        return 0;
    }
}
