<?php

namespace App\Http\Controllers;

use App\Support\RigData;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class DataController extends Controller
{
    /**
     * Serve bridges/cover.jpg. `no-store` so CEF never holds a stale image
     * even if a caller forgets the ?v=<hash> cache-bust. 404 returns a 1×1
     * transparent PNG so the music-box cover placeholder doesn't get a
     * broken-image icon while the bridge is starting up.
     */
    public function coverImage(RigData $rig): SymfonyResponse
    {
        $path = $rig->coverPath();
        if (is_file($path) && is_readable($path)) {
            return response()->file($path, [
                'Content-Type'  => 'image/jpeg',
                'Cache-Control' => 'no-store, max-age=0',
            ]);
        }

        // 1x1 transparent PNG (67 bytes).
        $png = base64_decode(
            'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='
        );
        return response($png, 200, [
            'Content-Type'  => 'image/png',
            'Cache-Control' => 'no-store, max-age=0',
        ]);
    }
}
