<?php

namespace App\Http\Controllers;

use App\Models\Airline;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class LogoController extends Controller
{

    public static function deduplicateLogo(string $logo, array $correctInternalNames)
    {
        $log = fn(string $message) => print($message . PHP_EOL);

        if (empty($correctInternalNames)) {
            $nulled = Airline::where('logo', $logo)->get(['name', 'internal_name']);
            Airline::where('logo', $logo)->update(['logo' => null]);
            foreach ($nulled as $airline) {
                $log("  Logo nulled: {$airline->name} ({$airline->internal_name})");
            }
            return;
        }

        $existing = Airline::whereIn('internal_name', $correctInternalNames)
            ->pluck('internal_name')
            ->toArray();

        $missing = array_diff($correctInternalNames, $existing);

        if (!empty($missing)) {
            throw new \InvalidArgumentException(
                'The following internal names do not exist in the database: ' . implode(', ', $missing)
            );
        }

        $nulled = Airline::where('logo', $logo)
            ->whereNotIn('internal_name', $correctInternalNames)
            ->get(['name', 'internal_name']);

        Airline::where('logo', $logo)
            ->whereNotIn('internal_name', $correctInternalNames)
            ->update(['logo' => null]);

        foreach ($nulled as $airline) {
            $log("  Logo nulled: {$airline->name} ({$airline->internal_name})");
        }
    }

    public static function nullMissingLogoFiles()
    {
        $log = fn(string $message) => print($message . PHP_EOL);

        $airlines = Airline::whereNotNull('logo')->get(['id', 'name', 'logo']);

        foreach ($airlines as $airline) {
            $path = storage_path('app/private/images/logos/tail/' . $airline->logo);

            if (!file_exists($path)) {
                $airline->update(['logo' => null]);
                $log("  Logo nulled (file missing): {$airline->name} — {$airline->logo}");
            }
        }
    }

    public static function renameLogoFiles()
    {
        $log = fn(string $message) => print($message . PHP_EOL);

        $airlines = Airline::whereNotNull('logo')->get(['id', 'name', 'internal_name', 'logo']);

        foreach ($airlines as $airline) {
            $expectedName = $airline->internal_name . '.png';

            if ($airline->logo === $expectedName) {
                continue;
            }

            $oldPath = storage_path('app/private/images/logos/tail/' . $airline->logo);
            $newPath = storage_path('app/private/images/logos/tail/' . $expectedName);

            if (!file_exists($oldPath)) {
                $log("  Skipping (file missing): {$airline->name} — {$airline->logo}");
                continue;
            }

            rename($oldPath, $newPath);
            $airline->update(['logo' => $expectedName]);
            $log("  Renamed: {$airline->logo} → {$expectedName} ({$airline->name})");
        }
    }
}
