<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use App\Console\Commands\AbstractCommand;

class SeraphSyncLabourCommand extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'seraph:sync-labour';

    /**
     * A friendly name of the command that will be used when loggin messages.
     *
     * @var string
     */
    protected $command_title = 'Seraph2 Sync Labour';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Synchronises the labour from Seraph2 repairs.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $tables = $this->option('table');

        if (empty($tables) || array_search('repairs', $tables) !== false) {
            $this->info('Syncing table: repairs');
            $total = 0;

            DB::connection('seraph2')->table('repairs')->select('repairid', 'ramount')->chunk(200, function ($repairs) use (&$total) {
                foreach ($repairs as $repair) {
                    $affected = DB::table('tasks')->where('seraph_id', $repair->repairid)->where('labour', '<=', 10)->update(['labour' => $repair->ramount]);

                    if ($affected) {
                        $this->info("Updated task #{$repair->repairid} with labour {$repair->ramount}");
                    }

                    $total += $affected;
                }
            });

            $this->info("Updated $total records in the repairs table");
        }

        if (empty($tables) || array_search('onsite', $tables) !== false) {
            $this->info('Syncing table: onsite');
            $total = 0;

            DB::connection('seraph2')->table('onsite')->select('Id', 'Title', 'Problem', 'StartDate', 'EndDate', 'timespent', 'rate', 'invoiceno', 'salesno', 'exchangeid')->chunk(200, function ($onsites) use (&$total) {
                foreach ($onsites as $onsite) {
                    $invoice_number = $onsite->invoiceno ?: $onsite->salesno;
                    $rate = $onsite->rate ?: 0;

                    $affected = DB::table('tasks')->where('type', 'onsite')->where('labour', '<=', 1)->where('title', 'like', $onsite->Title)->where('problem', 'like', $onsite->Problem)->where('onsite_start', $onsite->StartDate)->where('onsite_end', $onsite->EndDate)->where('created_at', '<', '2013-09-18 00:00:00') // the date when we migrated
                                                ->update(['labour' => $rate, 'time_spent' => $onsite->timespent, 'invoice_number' => $invoice_number, 'exchange_id' => $onsite->exchangeid]);

                    if ($affected) {
                        $this->info("Updated task #{$onsite->Id} (Onsite) with labour {$rate}");
                    }

                    $total += $affected;
                }
            });

            $this->info("Updated $total records in the onsite table");
        }
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['table', 't', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Restrict the sync to specific tables', []],
        ];
    }
}
