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

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

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

  export interface BasicRecordItem {
    name: string;
    code: string;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type PageResult = BasicFetchResult<RecordItem>;

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

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

  export const getOptions = (id: number) =>
    requestClient.get<BasicOptionResult[]>('/post/options', {
      params: { id },
    });

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

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

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

  export const updateStatus = (data: StatusParams) =>
    requestClient.put('/post/status', data);
}
