<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GetHeadersCSVCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'aptimap:entetes:csv {file}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Display Client CSV headers';

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

        if (!file_exists($file) || !is_readable($file)) {
            $this->error("The file {$file} does not exist or is not readable.");
            return 1;
        }

        $readingFile = fopen($file, 'r');
        $clientCSVHeaders = fgetcsv($readingFile);

        if ($clientCSVHeaders) {
            $this->info(implode($clientCSVHeaders));
        } else {
            $this->error('No headers found in the CSV file.');
        }

        fclose($readingFile);

        return 0;
    }
}
