<?php

namespace App\Http\Controllers\Public;

use App\Domain\Files\PatientFileService;
use App\Http\Controllers\Controller;
use App\Models\WaMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

/**
 * Serves a WhatsApp PDF attachment to the provider via a short-lived signed
 * URL (no auth — the signature IS the auth). Provider fetches it once.
 */
class WaMediaController extends Controller
{
    public function show(Request $request, string $message)
    {
        abort_unless($request->hasValidSignature(), 403);

        $wa = WaMessage::query()->withoutGlobalScope('tenant')
            ->where('uuid', $message)->firstOrFail();

        abort_unless($wa->media_path && Storage::disk(PatientFileService::DISK)->exists($wa->media_path), 404);

        return Storage::disk(PatientFileService::DISK)->response(
            $wa->media_path, $wa->media_filename ?? 'document.pdf',
            ['Content-Type' => $wa->media_mime ?? 'application/pdf'],
        );
    }
}
