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

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

export namespace TenantApi {
  export interface PageParams extends BasicPageParams {
    host?: string;
    name?: string;
    phone: string;
  }

  export interface BasicRecordItem {
    name: string;
    adminName: string;
    host: string;
    connection: string;
    email: string;
    phone: string;
    schema: string;
    remark: string;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type PageResult = BasicFetchResult<RecordItem>;

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

  export const getOptions = () =>
    requestClient.get<BasicOptionResult[]>('/tenant/options');

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

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

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

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

  export const getMenuIds = (id: number) =>
    requestClient.get<number[]>('/tenant/menu-ids', { params: { id } });

  export const updateGrant = (data: RelationRequest) =>
    requestClient.post('/tenant/grant', data);

  export const createDb = (id: number) =>
    requestClient.post('/tenant/db', { id });
}
