<?php

namespace App\Console\Commands;

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

class TestImageUpload extends Command
{
    protected $signature = 'test:image-upload';
    protected $description = 'Test image upload functionality';

    public function handle()
    {
        $this->info('Testing image upload functionality...');

        try {
            // اختبار إنشاء مجلد الصور
            $profileImagesPath = 'profile_images';
            if (!Storage::disk('public')->exists($profileImagesPath)) {
                Storage::disk('public')->makeDirectory($profileImagesPath);
                $this->info("Created directory: {$profileImagesPath}");
            } else {
                $this->info("Directory exists: {$profileImagesPath}");
            }

            // اختبار الوصول إلى التخزين
            $testPath = 'profile_images/test.txt';
            Storage::disk('public')->put($testPath, 'Test content');
            $this->info("Test file created: {$testPath}");

            // حذف الملف التجريبي
            Storage::disk('public')->delete($testPath);
            $this->info("Test file deleted");

            // اختبار عرض المستخدمين مع الصور
            $users = User::all();
            $this->info("Total users: " . $users->count());

            foreach ($users as $user) {
                $this->info("User: {$user->name} - Image: " . ($user->profile_image ?: 'No image'));
            }

            $this->info("\nImage upload functionality working correctly!");
            return 0;

        } catch (\Exception $e) {
            $this->error("Error: " . $e->getMessage());
            $this->error("File: " . $e->getFile() . ":" . $e->getLine());
            return 1;
        }
    }
}
