<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

use App\Services\AstmMessageService;
use App\DataListiner;

class ProcessAstmMessages extends Command
{
    protected $signature = 'astm:process-messages';
    protected $description = 'Process pending ASTM messages';

    private $astmMessageService;

    public function __construct(AstmMessageService $astmMessageService)
    {
        parent::__construct();
        $this->astmMessageService = $astmMessageService;
    }

    public function handle()
    {
        // Ambil data dari DataListener
        $dataListener   = DataListiner::where('processed', '0')->get();
        $dataLocal      = DB::table('lis_phoenix')->whereNull('processed')->get();
        if ($dataListener) {
            $jumlah = count($dataListener);
            $pesan = $this->astmMessageService->processAstmMessages($dataListener);
            $this->info($jumlah.' Message processed '.$pesan.' at '.date('Y-m-d H:i:s'));
        } else {
            if ($dataLocal) {
                $jumlah = count($dataLocal);
                $pesan = $this->astmMessageService->processAstmMessagesLokal($dataLocal);
                $this->info($jumlah.' Message processed Data Lokal '.$pesan.' at '.date('Y-m-d H:i:s'));
            }
            $this->info('Skipped Proses at '.date('Y-m-d H:i:s'));
        }
    }
}
