<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class CopyS3ToS3 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:copy-s3-to-s3';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $folders = [
            config('global.flag_image_upload_dir'),
            config('global.user_image_upload_dir'),
            config('global.dress_code_image_upload_dir'),
            config('global.activity_image_upload_dir'),
            config('global.sport_image_upload_dir'),
            config('global.badge_image_upload_dir'),
            config('global.notification_image_upload_dir'),
            config('global.home_logo_image_upload_dir'),
        ];

        foreach ($folders as $folder) {
            $this->info("🔍 Scanning folder: {$folder}");
            $files = Storage::disk('s3_dx')->allFiles($folder);

            foreach ($files as $file) {
                try {
                    $contents = Storage::disk('s3_dx')->get($file); // Source: s3_dx
                    Storage::disk('s3')->put($file, $contents);     // Destination: s3
                    $this->info("✅ Copied: {$file}");
                } catch (\Exception $e) {
                    $this->error("❌ Failed to copy {$file}: " . $e->getMessage());
                }
            }
        }

        $this->info("✅ All files copied from s3_dx to s3.");
    }
}
