<?php

namespace App\Http\Controllers;

use App\Models\EventParticipant;
use App\Providers\GiroCodeProvider;
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);
        }

        $amount = $participant->amount;
        $amount->subtractAmount($participant->amount_paid);

        $girocodeProvider = new GiroCodeProvider(
            $participant->event()->first()->account_owner,
            $participant->event()->first()->account_iban,
            $participant->amount->getAmount(),
            $participant->payment_purpose
        );

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