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

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

export namespace DatabaseApi {
  export interface DbTableItem {
    name: string;
    entityName: string;
    description: string;
  }

  export interface DbColumnItem {
    entityName: string;
    columnName: string;
    description: string;
    dataType: string;
    netType: string;
  }

  export interface PageRecordItem {
    description: string;
    name: string;
    configId: string;
    id: number;
  }

  export interface BasicColumnRecord {
    columnName: string;
    dataType: string;
    description: string;
    isPrimarykey: boolean;
    isNullable: boolean;
    decimalDigits: number;
    isIdentity: boolean;
    length: number;
  }

  export interface BasicRecordItem {
    name: string;
    description: string;
    configId: string;
    columns: BasicColumnRecord[];
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export interface PageParams extends BasicPageParams {
    name?: string;
    description?: string;
  }

  export type PageResult = BasicFetchResult<PageRecordItem>;

  export const getList = (configid: string) =>
    requestClient.get<DbTableItem[]>(`/database/table/list/${configid}`);

  export const getColumnList = (tablename: string, configid: string) =>
    requestClient.get<DbColumnItem[]>(
      `/database/column/list/${tablename}/${configid}`,
    );

  export const getPage = (params: PageParams) =>
    requestClient.get<PageResult>('/database/table/page', { params });

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

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

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

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

  export const getCreate = (id: number) =>
    requestClient.get<RecordItem>('/database/table/create', {
      params: { id },
    });
}
