package org.example.petshopdesktop.api.endpoints;

import com.fasterxml.jackson.core.type.TypeReference;
import org.example.petshopdesktop.api.ApiClient;
import org.example.petshopdesktop.api.dto.common.DropdownOption;

import java.util.List;

public class DropdownApi {
    private static final DropdownApi INSTANCE = new DropdownApi();
    private final ApiClient apiClient;

    private DropdownApi() {
        this.apiClient = ApiClient.getInstance();
    }

    public static DropdownApi getInstance() {
        return INSTANCE;
    }

    public List<DropdownOption> getCategories() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/categories");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from categories endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getProductCategories() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/product-categories");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from product categories endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getPetSpecies() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/pet-species");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from pet species endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getPetBreeds(String species) throws Exception {
        String encoded = java.net.URLEncoder.encode(species, java.nio.charset.StandardCharsets.UTF_8);
        String response = apiClient.getRawResponse("/api/v1/dropdowns/pet-breeds?species=" + encoded);
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from pet breeds endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getProducts() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/products");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from products endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getSuppliers() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/suppliers");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from suppliers endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getServices() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/services");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from services endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getCustomers() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/customers");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from customers endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getAppointmentCustomers() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/appointment-customers");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from appointment customers endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getPets() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/pets");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from pets endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getAdoptionPets() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/adoption-pets");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from adoption pets endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getAdoptionPets(Long storeId) throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/adoption-pets?storeId=" + storeId);
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from adoption pets endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getCustomerPets(Long customerId) throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/customers/" + customerId + "/pets");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from customer pets endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getStores() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/stores");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from stores endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getStoreEmployees(Long storeId) throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/stores/" + storeId + "/employees");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from store employees endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }

    public List<DropdownOption> getEmployees() throws Exception {
        String response = apiClient.getRawResponse("/api/v1/dropdowns/employees");
        if (response == null || response.isEmpty()) {
            throw new IllegalStateException("Empty response from all employees endpoint");
        }
        return apiClient.getObjectMapper().readValue(response, new TypeReference<List<DropdownOption>>() {});
    }
}
