import type { BasicFetchResult, BasicPageParams } from '#/api/model';

import { requestClient } from '#/api/request';

export namespace QueryApi {
  export interface PageParams extends BasicPageParams {
    name?: string;
    code?: string;
  }

  export interface BasicRecordItem {
    name: string;
    code: string;
    sort: number;
    remark: string;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type AppPageResult = BasicFetchResult<RecordItem>;

  export interface Columns {
    tableAlias: string;
    alias: string;
    name: string;
    remark: string;
  }

  export interface Tables {
    remark: string;
    name: string;
    alias: string;
    isMain: boolean;
    columns: Columns[];
    columnKeys: string[];
  }
  export const getPage = (params: PageParams) =>
    requestClient.get<AppPageResult>('/query/page', { params });

  export const getDetail = (id: number) =>
    requestClient.get<RecordItem>('/query/entity', {
      params: { id },
    });

  export const addDetail = (data: BasicRecordItem) =>
    requestClient.post('/query', data);

  export const editDetail = (data: RecordItem) =>
    requestClient.put('/query', data);

  export const deleteDetail = (id: number) =>
    requestClient.delete('/query', { data: { id } });

  export const getColumns = (id: number) =>
    requestClient.get('/query/column-list', { params: { id } });

  export const postExecute = (data: Record<string, any>) =>
    requestClient.post('/query/execute', data);

  export const postExecuteReal = (data: Record<string, any>) =>
    requestClient.post('/query/execute-real', data);
}
