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.PageResponse;
import org.example.petshopdesktop.api.dto.sale.SaleRequest;
import org.example.petshopdesktop.api.dto.sale.SaleResponse;

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

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

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

    public static SaleApi getInstance() {
        return INSTANCE;
    }

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

    public List<SaleResponse> listAllSales(String query, Long storeId) throws Exception {
        int page = 0;
        int size = 250;
        List<SaleResponse> allSales = new java.util.ArrayList<>();

        while (true) {
            String path = "/api/v1/sales?page=" + page + "&size=" + size;
            if (query != null && !query.isEmpty()) {
                path += "&q=" + URLEncoder.encode(query, StandardCharsets.UTF_8);
            }
            if (storeId != null) {
                path += "&storeId=" + storeId;
            }

            String response = apiClient.getRawResponse(path);
            PageResponse<SaleResponse> pageResponse = apiClient.getObjectMapper().readValue(
                response,
                new TypeReference<PageResponse<SaleResponse>>() {}
            );
            if (pageResponse == null) {
                throw new IllegalStateException("Null response from sales endpoint");
            }

            if (pageResponse.getContent() != null) {
                allSales.addAll(pageResponse.getContent());
            }

            if (pageResponse.isLast()) {
                break;
            }
            page++;
        }

        return allSales;
    }

    public SaleResponse getSale(Long id) throws Exception {
        return apiClient.get("/api/v1/sales/" + id, SaleResponse.class);
    }

    public SaleResponse createSale(SaleRequest request) throws Exception {
        return apiClient.post("/api/v1/sales", request, SaleResponse.class);
    }
}
