package com.svix;

import com.svix.api.*;

import lombok.Getter;

import okhttp3.HttpUrl;

import java.util.HashMap;
import java.util.Map;

@Getter
public class Svix {
    private final SvixHttpClient httpClient;
    private final Application application;
    private final Authentication authentication;
    private final BackgroundTask backgroundTask;
    private final Endpoint endpoint;
    private final EventType eventType;
    private final Ingest ingest;
    private final Integration integration;
    private final Message message;
    private final MessageAttempt messageAttempt;
    private final Statistics statistics;
    private final Streaming streaming;
    private final OperationalWebhook operationalWebhook;

    public Svix(String token) {
        this(token, new SvixOptions());
    }

    public Svix(String token, SvixOptions options) {
        String[] tokenParts = token.split("\\.");

        if (options.getServerUrl() == null) {
            String region = tokenParts[tokenParts.length - 1];
            switch (region) {
                case "us":
                    options.setServerUrl("https://api.us.svix.com");
                    break;
                case "eu":
                    options.setServerUrl("https://api.eu.svix.com");
                    break;
                case "in":
                    options.setServerUrl("https://api.in.svix.com");
                    break;
                case "ca":
                    options.setServerUrl("https://api.ca.svix.com");
                    break;
                case "au":
                    options.setServerUrl("https://api.au.svix.com");
                    break;

                default:
                    options.setServerUrl(SvixOptions.DEFAULT_URL);
            }
        }

        HttpUrl parsedUrl = HttpUrl.parse(options.getServerUrl());
        if (parsedUrl == null) {
            throw new IllegalArgumentException("Invalid base url");
        }

        Map<String, String> defaultHeaders = new HashMap<>();
        String userAgent = (
            "svix-libs/" + Version.VERSION + "/java jre/" + System.getProperty("java.runtime.version")
        );
        defaultHeaders.put("User-Agent", userAgent);
        defaultHeaders.put("Authorization", "Bearer " + token);

        this.httpClient = new SvixHttpClient(parsedUrl, defaultHeaders, options.getRetrySchedule());

        this.application = new Application(this.httpClient);
        this.authentication = new Authentication(this.httpClient);
        this.backgroundTask = new BackgroundTask(this.httpClient);
        this.endpoint = new Endpoint(this.httpClient);
        this.eventType = new EventType(this.httpClient);
        this.ingest = new Ingest(this.httpClient);
        this.integration = new Integration(this.httpClient);
        this.message = new Message(this.httpClient);
        this.messageAttempt = new MessageAttempt(this.httpClient);
        this.statistics = new Statistics(this.httpClient);
        this.streaming = new Streaming(this.httpClient);
        this.operationalWebhook = new OperationalWebhook(this.httpClient);
    }
}
