package fr.titionfire.ffsaf.domain.service;

import fr.titionfire.ffsaf.rest.client.dto.HelloassoNotification;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class WebhookService {

    @Inject
    CheckoutService checkoutService;

    @Inject
    CompetitionService competitionService;

    @ConfigProperty(name = "helloasso.organizationSlug")
    String organizationSlug;

    public Uni<Response> helloAssoNotification(HelloassoNotification notification) {
        if (notification.getEventType().equals("Payment")) {
            if (notification.getData().getOrder().getFormType().equals("Checkout")) {
                if (notification.getData().getOrder().getOrganizationSlug().equalsIgnoreCase(organizationSlug)) {
                    return checkoutService.paymentStatusChange(notification.getData().getState(),
                            notification.getMetadata());
                }
            } else if (notification.getData().getOrder().getFormType().equals("Event")) {
                return competitionService.unregisterHelloAsso(notification.getData());
            }
        }else if (notification.getEventType().equals("Order")){
            if (notification.getData().getFormType().equals("Event")) {
                return competitionService.registerHelloAsso(notification.getData());
            }
        }

        return Uni.createFrom().item(Response.ok().build());
    }
}
