package com.ga.common.model;

import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

/**
 * 페이징 응답 (PageHelper 연동).
 * 사용 예: return PageResponse.of(mapper.selectList(param));
 */
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PageResponse<T> {

    private List<T> list;
    private int pageNum;
    private int pageSize;
    private long total;
    private int pages;

    public static <T> PageResponse<T> of(List<T> list) {
        if (list == null) {
            return PageResponse.<T>builder()
                    .list(Collections.emptyList())
                    .pageNum(1).pageSize(0).total(0).pages(0).build();
        }
        PageInfo<T> info = new PageInfo<>(list);
        return PageResponse.<T>builder()
                .list(info.getList())
                .pageNum(info.getPageNum())
                .pageSize(info.getPageSize())
                .total(info.getTotal())
                .pages(info.getPages())
                .build();
    }

    public static <T> PageResponse<T> empty() {
        return PageResponse.<T>builder()
                .list(Collections.emptyList())
                .pageNum(1).pageSize(0).total(0).pages(0).build();
    }
}
