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.BulkDeleteRequest;
import org.example.petshopdesktop.api.dto.common.PageResponse;
import org.example.petshopdesktop.api.dto.pet.PetRequest;
import org.example.petshopdesktop.api.dto.pet.PetResponse;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;

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

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

    public static PetApi getInstance() {
        return INSTANCE;
    }

    public List<PetResponse> listPets(String query, String species, String status, Long storeId) throws Exception {
        String path = "/api/v1/pets?page=0&size=1000";
        if (query != null && !query.isEmpty()) {
            path += "&q=" + URLEncoder.encode(query, StandardCharsets.UTF_8);
        }
        if (species != null && !species.isEmpty()) {
            path += "&species=" + URLEncoder.encode(species, StandardCharsets.UTF_8);
        }
        if (status != null && !status.isEmpty()) {
            path += "&status=" + URLEncoder.encode(status, StandardCharsets.UTF_8);
        }
        if (storeId != null) {
            path += "&storeId=" + storeId;
        }
        String response = apiClient.getRawResponse(path);
        PageResponse<PetResponse> pageResponse = apiClient.getObjectMapper().readValue(
            response,
            new TypeReference<PageResponse<PetResponse>>() {}
        );
        if (pageResponse == null) {
            throw new IllegalStateException("Null response from pets endpoint");
        }
        return pageResponse.getContent();
    }

    public List<PetResponse> listPets(String query, String species, String status) throws Exception {
        return listPets(query, species, status, null);
    }

    public List<PetResponse> listPets(String query) throws Exception {
        return listPets(query, null, null, null);
    }

    public PetResponse getPetById(Long id) throws Exception {
        return apiClient.get("/api/v1/pets/" + id, PetResponse.class);
    }

    public PetResponse createPet(PetRequest request) throws Exception {
        return apiClient.post("/api/v1/pets", request, PetResponse.class);
    }

    public PetResponse updatePet(Long id, PetRequest request) throws Exception {
        return apiClient.put("/api/v1/pets/" + id, request, PetResponse.class);
    }

    public PetResponse uploadPetImage(Long id, Path imagePath) throws Exception {
        return apiClient.postMultipart("/api/v1/pets/" + id + "/image", "image", imagePath, PetResponse.class);
    }

    public void deletePetImage(Long id) throws Exception {
        apiClient.delete("/api/v1/pets/" + id + "/image");
    }

    public byte[] getPetImage(Long id) throws Exception {
        return apiClient.getBytes("/api/v1/pets/" + id + "/image");
    }

    public void deletePets(List<Long> ids) throws Exception {
        apiClient.deleteWithBody("/api/v1/pets", new BulkDeleteRequest(ids));
    }
}
