<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use Illuminate\Support\Facades\Storage;

class ImportUsersFromJson extends Command
{
    protected $signature = 'users:import';
    protected $description = 'Import users from a JSON file';

    public function handle()
    {
        if (!Storage::disk('local')->exists('users.json')) {
            $this->error('The users.json file does not exist.');
            return;
        }

        $json = Storage::disk('local')->get('users.json');
        $users = json_decode($json, true);

        foreach ($users as $userData) {
            User::updateOrCreate(
                ['sn' => $userData['sn']], // Assuming 'id' is the unique identifier
                $userData
            );
        }

        $this->info('Users have been imported from storage/app/users.json.');
    }
}
