import {
  LayoutStandard,
  CreateLayoutRequest,
  UpdateLayoutRequest,
  GetLayoutsResponse,
  LayoutCategory,
  LayoutType,
} from "@/types/layout";
import { apiClient } from "./client";

export interface GetLayoutsParams {
  page?: number;
  size?: number;
  category?: LayoutCategory;
  layoutType?: LayoutType;
  searchTerm?: string;
  includePublic?: boolean;
}

export interface DuplicateLayoutRequest {
  newName: string;
}

class LayoutApiService {
  private basePath = "/layouts";

  /**
   * 레이아웃 목록 조회
   */
  async getLayouts(params: GetLayoutsParams = {}): Promise<GetLayoutsResponse> {
    const response = await apiClient.get(this.basePath, { params });
    return response.data.data;
  }

  /**
   * 레이아웃 상세 조회
   */
  async getLayoutById(layoutCode: string): Promise<LayoutStandard> {
    const response = await apiClient.get(`${this.basePath}/${layoutCode}`);
    return response.data.data;
  }

  /**
   * 레이아웃 생성
   */
  async createLayout(data: CreateLayoutRequest): Promise<LayoutStandard> {
    const response = await apiClient.post(this.basePath, data);
    return response.data.data;
  }

  /**
   * 레이아웃 수정
   */
  async updateLayout(layoutCode: string, data: Partial<CreateLayoutRequest>): Promise<LayoutStandard> {
    const response = await apiClient.put(`${this.basePath}/${layoutCode}`, data);
    return response.data.data;
  }

  /**
   * 레이아웃 삭제
   */
  async deleteLayout(layoutCode: string): Promise<void> {
    await apiClient.delete(`${this.basePath}/${layoutCode}`);
  }

  /**
   * 레이아웃 복제
   */
  async duplicateLayout(layoutCode: string, data: DuplicateLayoutRequest): Promise<LayoutStandard> {
    const response = await apiClient.post(`${this.basePath}/${layoutCode}/duplicate`, data);
    return response.data.data;
  }

  /**
   * 카테고리별 레이아웃 개수 조회
   */
  async getLayoutCountsByCategory(): Promise<Record<string, number>> {
    const response = await apiClient.get(`${this.basePath}/counts-by-category`);
    return response.data.data;
  }

  /**
   * 레이아웃 검색
   */
  async searchLayouts(
    searchTerm: string,
    params: Omit<GetLayoutsParams, "searchTerm"> = {},
  ): Promise<GetLayoutsResponse> {
    return this.getLayouts({ ...params, searchTerm });
  }

  /**
   * 카테고리별 레이아웃 조회
   */
  async getLayoutsByCategory(
    category: LayoutCategory,
    params: Omit<GetLayoutsParams, "category"> = {},
  ): Promise<GetLayoutsResponse> {
    return this.getLayouts({ ...params, category });
  }

  /**
   * 레이아웃 타입별 조회
   */
  async getLayoutsByType(
    layoutType: LayoutType,
    params: Omit<GetLayoutsParams, "layoutType"> = {},
  ): Promise<GetLayoutsResponse> {
    return this.getLayouts({ ...params, layoutType });
  }

  /**
   * 공개 레이아웃만 조회
   */
  async getPublicLayouts(params: Omit<GetLayoutsParams, "includePublic"> = {}): Promise<GetLayoutsResponse> {
    return this.getLayouts({ ...params, includePublic: true });
  }

  /**
   * 개인 레이아웃만 조회
   */
  async getPrivateLayouts(params: Omit<GetLayoutsParams, "includePublic"> = {}): Promise<GetLayoutsResponse> {
    return this.getLayouts({ ...params, includePublic: false });
  }
}

// 인스턴스 생성 및 내보내기
export const layoutApi = new LayoutApiService();

// 기본 내보내기
export default layoutApi;

