/**
 * DDL 실행 API 클라이언트
 */

import { apiClient } from "./client";
import {
  CreateTableRequest,
  AddColumnRequest,
  DDLExecutionResult,
  ValidationResult,
  DDLExecutionLog,
  DDLStatistics,
} from "../../types/ddl";

export const ddlApi = {
  /**
   * 새 테이블 생성
   */
  createTable: async (request: CreateTableRequest): Promise<DDLExecutionResult> => {
    const response = await apiClient.post("/ddl/tables", request);
    return response.data;
  },

  /**
   * 기존 테이블에 컬럼 추가
   */
  addColumn: async (tableName: string, request: AddColumnRequest): Promise<DDLExecutionResult> => {
    const response = await apiClient.post(`/ddl/tables/${tableName}/columns`, request);
    return response.data;
  },

  /**
   * 테이블 삭제 (DROP TABLE)
   */
  dropTable: async (tableName: string): Promise<DDLExecutionResult> => {
    const response = await apiClient.delete(`/ddl/tables/${tableName}`);
    return response.data;
  },

  /**
   * 테이블 생성 사전 검증 (실제 생성하지 않고 검증만)
   */
  validateTableCreation: async (request: CreateTableRequest): Promise<ValidationResult> => {
    const response = await apiClient.post("/ddl/validate/table", request);
    return response.data.data;
  },

  /**
   * DDL 실행 로그 조회
   */
  getDDLLogs: async (params?: {
    limit?: number;
    userId?: string;
    ddlType?: string;
  }): Promise<{
    logs: DDLExecutionLog[];
    total: number;
  }> => {
    const response = await apiClient.get("/ddl/logs", { params });
    return response.data.data;
  },

  /**
   * DDL 실행 통계 조회
   */
  getDDLStatistics: async (params?: { fromDate?: string; toDate?: string }): Promise<DDLStatistics> => {
    const response = await apiClient.get("/ddl/statistics", { params });
    return response.data.data;
  },

  /**
   * 특정 테이블의 DDL 히스토리 조회
   */
  getTableDDLHistory: async (
    tableName: string,
  ): Promise<{
    tableName: string;
    history: DDLExecutionLog[];
    total: number;
  }> => {
    const response = await apiClient.get(`/ddl/tables/${tableName}/history`);
    return response.data.data;
  },

  /**
   * 생성된 테이블 정보 조회
   */
  getTableInfo: async (
    tableName: string,
  ): Promise<{
    tableInfo: any;
    columns: any[];
  }> => {
    const response = await apiClient.get(`/ddl/tables/${tableName}/info`);
    return response.data.data;
  },

  /**
   * 오래된 DDL 로그 정리
   */
  cleanupOldLogs: async (
    retentionDays?: number,
  ): Promise<{
    deletedCount: number;
    retentionDays: number;
  }> => {
    const response = await apiClient.delete("/ddl/logs/cleanup", {
      params: { retentionDays },
    });
    return response.data.data;
  },

  /**
   * DDL 서비스 정보 조회
   */
  getServiceInfo: async (): Promise<any> => {
    const response = await apiClient.get("/ddl/info");
    return response.data.data;
  },

  /**
   * DDL 서비스 헬스체크
   */
  healthCheck: async (): Promise<{
    status: string;
    timestamp: string;
    checks: {
      database: string;
      service: string;
    };
  }> => {
    const response = await apiClient.get("/ddl/health");
    return response.data;
  },
};
