<?php

namespace FluxCms\Core\Commands;

use Illuminate\Console\Command;

class PublishCommand extends Command
{
    protected $signature = 'flux-cms:publish
                            {--force : Overwrite existing files}
                            {--tag= : Specific tag to publish}';

    protected $description = 'Publish Flux CMS assets and configuration';

    public function handle(): int
    {
        $this->info('📦 Publishing Flux CMS assets...');

        $tag = $this->option('tag');
        $force = $this->option('force');

        if ($tag) {
            $this->publishTag($tag, $force);
        } else {
            $this->publishAll($force);
        }

        $this->info('✅ Flux CMS assets published successfully!');

        return 0;
    }

    protected function publishAll(bool $force): void
    {
        $this->info('Publishing all Flux CMS assets...');

        // Publish config
        $this->call('vendor:publish', [
            '--tag' => 'flux-cms-config',
            '--force' => $force,
        ]);

        // Publish migrations
        $this->call('vendor:publish', [
            '--tag' => 'flux-cms-migrations',
            '--force' => $force,
        ]);

        // Publish views
        $this->call('vendor:publish', [
            '--tag' => 'flux-cms-views',
            '--force' => $force,
        ]);

        // Publish assets
        $this->call('vendor:publish', [
            '--tag' => 'flux-cms-assets',
            '--force' => $force,
        ]);

        // Publish translations
        $this->call('vendor:publish', [
            '--tag' => 'flux-cms-translations',
            '--force' => $force,
        ]);
    }

    protected function publishTag(string $tag, bool $force): void
    {
        $this->info("Publishing tag: {$tag}");

        $this->call('vendor:publish', [
            '--tag' => $tag,
            '--force' => $force,
        ]);
    }
}
