<?php

namespace App\Console\Commands;

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

class ImportExcel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:excel {file}';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $file = $this->argument('file');

        $this->info('Importing data from: ' . $file);

        // Validation and import logic
        try {
            $this->validateInput($file);

            Excel::import(new LettersImport, $file);

            $this->info('Data imported successfully.');
        } catch (\Exception $e) {
            $this->error('Import failed: ' . $e->getMessage());
        }
    }

    protected function validateInput($file)
    {
        // Add validation rules here if needed
        if (!file_exists($file)) {
            throw new \Exception('File not found: ' . $file);
        }
    }
}
