<?php

declare(strict_types=1);


namespace App\Console\Commands;

use App\Blog\Support\ThemeManager;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class ThemePublish extends Command
{
    protected $signature = 'theme:publish {name? : 主题名（默认全部）}';

    protected $description = '把主题 assets 拷贝到 public/themes 供生产环境直接托管';

    public function handle(ThemeManager $themes): int
    {
        $names = $this->argument('name')
            ? [$this->argument('name')]
            : array_keys($themes->all());

        foreach ($names as $name) {
            if (! $themes->exists($name)) {
                $this->warn("跳过不存在的主题：{$name}");
                continue;
            }

            $source = $themes->path($name).'/assets';
            $target = public_path('themes/'.$name);

            if (! is_dir($source)) {
                $this->warn("主题 {$name} 没有 assets 目录");
                continue;
            }

            File::deleteDirectory($target);
            File::copyDirectory($source, $target);
            $this->info("已发布：{$name} -> public/themes/{$name}");
        }

        return self::SUCCESS;
    }
}
