<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Artwork;

class LegacyNginxMappingCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'naart:legacy-nginx-mapping';

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

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $oldLocations = [];
        $newLocations = [];
        $this->line('# legacy_mappings.conf');
        $this->line('');
        $this->line('map $uri $new_location {');
        $artworks = Artwork::whereNotNull('legacy_filename')->get();
        foreach ($artworks as $artwork) {
            if (!in_array($artwork->legacy_filename, $oldLocations) && !in_array($artwork->legacy_filename, $newLocations)) {
                $oldLocations[] = $artwork->legacy_filename;
                $newLocations[] = $artwork->legacy_filename;
                $this->line('    "' . $artwork->legacy_filename . '" "/legacy-asset/?legacy_filename=' . urlencode($artwork->legacy_filename) . '";');
            }
        }
        $this->line('    default $uri;');
        $this->line('}');
        $this->line('');
    }
}