/**
 * 화면 임베딩 및 데이터 전달 시스템 API 클라이언트
 */

import apiClient from "./client";
import type {
  ScreenEmbedding,
  ScreenDataTransfer,
  ScreenSplitPanel,
  CreateScreenEmbeddingRequest,
  CreateScreenDataTransferRequest,
  CreateScreenSplitPanelRequest,
  ApiResponse,
} from "@/types/screen-embedding";

// ============================================
// 1. 화면 임베딩 API
// ============================================

/**
 * 화면 임베딩 목록 조회
 */
export async function getScreenEmbeddings(
  parentScreenId: number
): Promise<ApiResponse<ScreenEmbedding[]>> {
  try {
    const response = await apiClient.get("/screen-embedding", {
      params: { parentScreenId },
    });
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "화면 임베딩 목록 조회 실패",
    };
  }
}

/**
 * 화면 임베딩 상세 조회
 */
export async function getScreenEmbeddingById(
  id: number
): Promise<ApiResponse<ScreenEmbedding>> {
  try {
    const response = await apiClient.get(`/screen-embedding/${id}`);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "화면 임베딩 조회 실패",
    };
  }
}

/**
 * 화면 임베딩 생성
 */
export async function createScreenEmbedding(
  data: CreateScreenEmbeddingRequest
): Promise<ApiResponse<ScreenEmbedding>> {
  try {
    const response = await apiClient.post("/screen-embedding", data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "화면 임베딩 생성 실패",
    };
  }
}

/**
 * 화면 임베딩 수정
 */
export async function updateScreenEmbedding(
  id: number,
  data: Partial<CreateScreenEmbeddingRequest>
): Promise<ApiResponse<ScreenEmbedding>> {
  try {
    const response = await apiClient.put(`/screen-embedding/${id}`, data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "화면 임베딩 수정 실패",
    };
  }
}

/**
 * 화면 임베딩 삭제
 */
export async function deleteScreenEmbedding(
  id: number
): Promise<ApiResponse<void>> {
  try {
    const response = await apiClient.delete(`/screen-embedding/${id}`);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "화면 임베딩 삭제 실패",
    };
  }
}

// ============================================
// 2. 데이터 전달 API
// ============================================

/**
 * 데이터 전달 설정 조회
 */
export async function getScreenDataTransfer(
  sourceScreenId: number,
  targetScreenId: number
): Promise<ApiResponse<ScreenDataTransfer>> {
  try {
    const response = await apiClient.get("/screen-data-transfer", {
      params: { sourceScreenId, targetScreenId },
    });
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "데이터 전달 설정 조회 실패",
    };
  }
}

/**
 * 데이터 전달 설정 생성
 */
export async function createScreenDataTransfer(
  data: CreateScreenDataTransferRequest
): Promise<ApiResponse<ScreenDataTransfer>> {
  try {
    const response = await apiClient.post("/screen-data-transfer", data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "데이터 전달 설정 생성 실패",
    };
  }
}

/**
 * 데이터 전달 설정 수정
 */
export async function updateScreenDataTransfer(
  id: number,
  data: Partial<CreateScreenDataTransferRequest>
): Promise<ApiResponse<ScreenDataTransfer>> {
  try {
    const response = await apiClient.put(`/screen-data-transfer/${id}`, data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "데이터 전달 설정 수정 실패",
    };
  }
}

/**
 * 데이터 전달 설정 삭제
 */
export async function deleteScreenDataTransfer(
  id: number
): Promise<ApiResponse<void>> {
  try {
    const response = await apiClient.delete(`/screen-data-transfer/${id}`);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "데이터 전달 설정 삭제 실패",
    };
  }
}

// ============================================
// 3. 분할 패널 API
// ============================================

/**
 * 분할 패널 설정 조회
 */
export async function getScreenSplitPanel(
  screenId: number
): Promise<ApiResponse<ScreenSplitPanel>> {
  try {
    const response = await apiClient.get(`/screen-split-panel/${screenId}`);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "분할 패널 설정 조회 실패",
    };
  }
}

/**
 * 분할 패널 설정 생성
 */
export async function createScreenSplitPanel(
  data: CreateScreenSplitPanelRequest
): Promise<ApiResponse<ScreenSplitPanel>> {
  try {
    const response = await apiClient.post("/screen-split-panel", data);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "분할 패널 설정 생성 실패",
    };
  }
}

/**
 * 분할 패널 설정 수정
 */
export async function updateScreenSplitPanel(
  id: number,
  layoutConfig: any
): Promise<ApiResponse<ScreenSplitPanel>> {
  try {
    const response = await apiClient.put(`/screen-split-panel/${id}`, {
      layoutConfig,
    });
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "분할 패널 설정 수정 실패",
    };
  }
}

/**
 * 분할 패널 설정 삭제
 */
export async function deleteScreenSplitPanel(
  id: number
): Promise<ApiResponse<void>> {
  try {
    const response = await apiClient.delete(`/screen-split-panel/${id}`);
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      error: error.response?.data?.message || "분할 패널 설정 삭제 실패",
    };
  }
}

// ============================================
// 4. 유틸리티 함수
// ============================================

/**
 * 화면 임베딩 전체 설정 조회 (분할 패널 포함)
 */
export async function getFullScreenEmbeddingConfig(
  screenId: number
): Promise<ApiResponse<ScreenSplitPanel>> {
  return getScreenSplitPanel(screenId);
}

