<?php

namespace App\Console\Commands;

use App\Imports\EmployeeBulkImport;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class ImportEmployeesBulk extends Command
{
    protected $signature = 'employees:import {path : CSV/XLSX using the standard import template}';

    protected $description = 'Bulk-import employees from the standard header-based template (new-institution setup).';

    public function handle(): int
    {
        $path = $this->argument('path');
        if (! is_file($path)) {
            $this->error("File not found: {$path}");

            return self::FAILURE;
        }

        $import = new EmployeeBulkImport;
        Excel::import($import, $path);

        $this->table(['Metric', 'Value'], [
            ['Created', $import->created],
            ['Updated', $import->updated],
            ['Skipped', $import->skipped],
        ]);

        foreach (array_slice($import->errors, 0, 20) as $e) {
            $this->line("  row {$e['row']} [{$e['file']}]: ".implode('; ', $e['messages']));
        }

        return self::SUCCESS;
    }
}
