<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Services\ImbangdugaServices;

class DailyImbangDuga extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'imbangduga:daily {--zone=1 : Choose which zone to update [1,2,3]}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Save Trial Balance in Database Daily';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    
    private $imbangduga;

    public function __construct(ImbangdugaServices $imbangduga)
    {
        $this->imbangduga = $imbangduga;
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $array_cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan')->json();
        $zone = $this->option('zone');

        if($zone < 1 || $zone > 3)
        {
            throw new Exception('The zone is not in the range!');
        }
        
        foreach($array_cawangan as $caw)
        {
            if($caw['version'] == 'v2')
            {
                $response = $this->imbangduga->OperationImbangDuga($caw['kodcaw']);
            }
        }
        
        return 0;
    }
}
