<?php

namespace App\Console\Commands;

use App\Services\Installer\InstallerService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;

class RemoteMatchInstallCommand extends Command
{
    protected $signature = 'remotematch:install
        {--seed-demo : Seed optional demo jobs and applicant data}
        {--skip-seed : Skip base seeders}
        {--force : Run in production without confirmation prompts}';

    protected $description = 'Prepare a RemoteMatch buyer installation after .env and database credentials are configured.';

    public function handle(InstallerService $installer): int
    {
        $installer->bootForInstaller();

        if ($installer->isInstalled() && ! $this->option('force')) {
            $this->error('RemoteMatch is already installed. Use the web installer for buyers, or pass --force only for controlled developer maintenance.');

            return self::FAILURE;
        }

        if (! File::exists(base_path('.env'))) {
            File::copy(base_path('.env.example'), base_path('.env'));
            $this->info('Created .env from .env.example. Update database and service credentials before launch.');
        }

        if (blank(config('app.key'))) {
            Artisan::call('key:generate', ['--force' => true]);
            $this->line(trim(Artisan::output()));
        }

        $this->info('Running migrations...');
        Artisan::call('migrate', ['--force' => true]);
        $this->line(trim(Artisan::output()));

        if (! $this->option('skip-seed')) {
            $this->info('Seeding base admin, settings, and job source records...');
            Artisan::call('db:seed', ['--force' => true]);
            $this->line(trim(Artisan::output()));
        }

        if ($this->option('seed-demo')) {
            $this->warn('Seeding demo data. Remove demo records before accepting real applicants.');
            Artisan::call('db:seed', [
                '--class' => 'Database\\Seeders\\DemoDataSeeder',
                '--force' => true,
            ]);
            $this->line(trim(Artisan::output()));
        }

        $this->info('Creating public storage link if possible...');
        Artisan::call('storage:link');
        $this->line(trim(Artisan::output()) ?: 'Storage link command completed.');

        Artisan::call('optimize:clear');
        $this->line(trim(Artisan::output()));

        $installer->lockCurrentInstallation((string) config('admin.default_email'));

        $this->info('RemoteMatch install task completed.');
        $this->line('Admin URL: '.url('/admin'));
        $this->line('Default admin email: '.config('admin.default_email'));
        $this->line('Default admin password: '.config('admin.default_password'));
        $this->warn('Change the default admin password immediately after first login.');
        $this->warn('Buyer installs should use the browser installer at /install.');
        $this->line('Run php artisan remotematch:health to review launch readiness.');

        return self::SUCCESS;
    }
}
