<?php

namespace App\Http\Controllers;

use App\EventPaymentModules\ProvidesGiroCode;
use App\Models\EventParticipant;
use Illuminate\Http\Request;

class GiroCodeGetController {
    public function __invoke(string $participantToken, Request $request)
    {
        $participant = EventParticipant::where(['identifier' => $participantToken])->first();
        if (null === $participant) {
            return response()->json(['message' => 'Participant not found'], 404);
        }

        // Die GiroCode-Erzeugung gehört zum Zahlungsmodul; ohne diese Fähigkeit (z.B. Barzahlung) -> 404.
        $module = $participant->paymentModule();
        $girocode = $module instanceof ProvidesGiroCode
            ? $module->giroCode($participant, $participant->paymentConfiguration())
            : null;

        if ($girocode === null) {
            return response()->json(['message' => 'No giro code for this payment method'], 404);
        }

        return response($girocode, 200, ['Content-Type' => 'image/png']);
    }
}
