<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Intervention\Image\Facades\Image;
use ImageOptimizer;
use App\Models\User;
use App\Models\Artist;
use App\Models\Artwork;
use App\Models\Podcast;
use App\Models\Episode;

class ImportMissingLegacyArtworkCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'naart:import-legacy-artwork {legacy_artwork_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Used to import a single legacy artwork item that was missed in the migration.';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $legacyArtwork = DB::connection('legacy')->table('artworks')->where('id', $this->argument('legacy_artwork_id'))->first();
        $legacyEpisode = DB::connection('legacy')->table('episodes')->where('id', $legacyArtwork->episode_id)->first();
        dump($legacyEpisode);
        dump($legacyArtwork);
        $user = User::where('legacy_id', $legacyArtwork->user_id)->with('artists')->first();
        $artist = $user->artists->first();
        dump($artist);
        $podcast = Podcast::find(1);
        $episode = Episode::where('episode_number', $legacyEpisode->episode_number)->first();
        dump($episode);
        $date = Carbon::parse($legacyArtwork->created_at);
        $basename = $date->format('Y')
            . '/'
            . $date->format('m')
            . '/'
            . Str::slug($artist->name)
            . '_'
            . Str::slug($legacyArtwork->title)
            . '_'
            . Str::random(8)
            . '.jpg';
        $thumbnailName = 'thumbnails/' . $basename;
        $artworkName = 'artworks/' . $basename;
        $artworkExists = Artwork::where('artist_id', $artist->id)
            ->where('episode_id', $episode->id)
            ->where('title', $legacyArtwork->title)
            ->count();
        $thumbnailExists = false;
        $artExists = false;
        dump($artworkExists);
        dump([$thumbnailName, $artworkName]);
        if (Storage::disk('static')->exists($thumbnailName)) {
            $this->line('Thumbnail already exists.');
            $thumbnailExists = true;
        }
        if (Storage::disk('static')->exists($artworkName)) {
            $this->line('Artwork already exists.');
            $artExists = true;
        }
        dump([$thumbnailExists, $artExists]);
        if (!$artworkExists) {
            $artwork = Artwork::factory()->state([
                'title' => $legacyArtwork->title,
                'artist_id' => $artist->id,
                'description' => null,
                'overlay_id' => null,
                'podcast_id' => $podcast->id,
                'episode_id' => $episode->id,
                'approved_by' => 4,
                'filename' => $basename,
                'legacy_filename' => $legacyArtwork->path . '/' . $legacyArtwork->filename,
                'legacy_id' => $legacyArtwork->id,
                'created_at' => $legacyArtwork->created_at,
                'updated_at' => $legacyArtwork->created_at,
            ])->create();
            $img = Image::make('/var/www/html/naartgen/podcastartgenerator/static' . $artwork->legacy_filename)
                ->resize(3000, null, function($constraint) {
                    $constraint->aspectRatio();
                })
                ->encode('jpg', 100)
                ->save(Storage::disk('static')->path('/artworks') . '/' . $artwork->filename);
            $thumbImg = Image::make('/var/www/html/naartgen/podcastartgenerator/static' . $artwork->legacy_filename)
                ->resize(512, null, function($constraint) {
                    $constraint->aspectRatio();
                })
                ->encode('jpg', 100)
                ->save(Storage::disk('static')->path('/thumbnails') . '/' . $artwork->filename);
            ImageOptimizer::optimize(Storage::disk('static')->path('/artworks/' . $artwork->filename));
            ImageOptimizer::optimize(Storage::disk('static')->path('/thumbnails/' . $artwork->filename));
            dump($artwork);
        }
    }
}