<?php

namespace App\Console\Commands\Report;

use App\Helpers\ClickhouseHandler;
use App\Models\Organisation;

class ReportSyncLists extends \App\Console\Commands\BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'report-sync:lists';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Synchronise lists we maintain in clickhouse for speed. This does not really need to be efficient since it will only be occasionally ran.';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $start = microtime(true);

        $chh = new ClickhouseHandler(
            config('creatia.reporting.db'),
            config('creatia.reporting.host'),
            config('creatia.reporting.port'),
            config('creatia.reporting.user'),
            config('creatia.reporting.pass'),
            null,
            false
        );

        $organisations = Organisation::withTrashed()->select('id', 'name')->get();

        foreach($organisations as $x) {
            $chh->insert('organisation', function() { return true; }, [ [
                'id'    => $x->id,
                'name'  => $x->name
            ] ]);
        }

        return 0;
    }
}