<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;

use App\Jobs\ImportCosts as Job;
use App\Models\{Site,Supplier};

class ImportCosts extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'costs:import {siteid : Site ID} {supplier : Supplier Name} {file : Filename} {date : Date}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import Costs from file';

    /**
     * Execute the console command.
     *
     * @return void
	 */
    public function handle()
    {
		if (! str_starts_with($this->argument('file'),'files/'))
			throw new \Exception('Filename must start with files/');

		Job::dispatchSync(
			Site::findOrFail($this->argument('siteid')),
			Supplier::where('name',$this->argument('supplier'))->singleOrFail(),
			Carbon::create($this->argument('date')),
			$this->argument('file'),
		);
    }
}
