<?php

namespace App\Console\Commands;

use App\Models\Company;
use App\Services\Navision\LicensesService;
use Illuminate\Console\Command;

class GetCompanyLicenses extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'noldus:get-company-licenses {company_no}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Gets company licenses from navision';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(LicensesService $service)
    {
        $companyNo = $this->argument('company_no');
        if (!$companyNo) {
            $this->error('You have to specify the company nav number');
            return 0;
        }
        $company = Company::where('nav_no', $companyNo)->first();
        if (!$company) {
            $this->error('Company not found!');
            return 0;
        }
        $licenses = $service->getCompanyLicenses($company);

        echo json_encode($licenses);
        return 0;
    }
}
