<?php

namespace App\Console\Commands;

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

class TestFtpConnection extends Command
{
    protected $signature = 'ftp:test';
    protected $description = '測試 FTP 連線、上傳、下載、刪除';

    public function handle()
    {
        $disk = Storage::disk('ftp_test');
        $testFile = 'laravel_ftp_test.txt';
        $testContent = '這是 Laravel 的 FTP 測試檔案';

        // 嘗試上傳
        $this->info("🔼 上傳中...");
        if (!$disk->put($testFile, $testContent)) {
            return $this->error("❌ 上傳失敗");
        }

        // 嘗試讀取
        $this->info("📥 下載檢查...");
        $content = $disk->get($testFile);
        if ($content !== $testContent) {
            return $this->error("❌ 檔案內容不符");
        }

        // 嘗試刪除
        $this->info("🗑️ 刪除測試檔案...");
        if (!$disk->delete($testFile)) {
            return $this->error("❌ 刪除失敗");
        }

        $this->info("✅ FTP 測試成功！");
    }
}