<?php

namespace App\Console\Commands\Report;

use App\Helpers\ClickhouseHandler;
use App\Models\Organisation;
use App\Repositories\ReportRepository;
use Illuminate\Support\Facades\Redis;

class ReportCacheDestinationTableThread extends \App\Console\Commands\BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'report:cache-destination-table-thread {--organisation=} {--order=} {--direction=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This serves to cache a specific iteration of a singular list.';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $organisation = $this->option('organisation');
        $order = $this->option('order');
        $direction = $this->option('direction');

        $rr = new ReportRepository();
        
        $list = $rr->get_list_singular(1, $organisation == 0 ? [] : [ 'organisation_id' => $organisation ], 0, 100, $order, $direction);

        Redis::setex($organisation == 0 ? "report-singular-admin-list-$order-$direction" : "report-singular-$organisation-list", 172800, json_encode($list, JSON_UNESCAPED_SLASHES));

        return 0;
    }
}