import { request } from '@umijs/max';
import {StoryItem, StoryItemTimeQueryParams, StoryType} from './data.d';
import {CommonListResponse, CommonResponse} from "@/types/common";

type ParamsType = {
  count?: number;
  instanceId?: string;
  storyName?: string;
  pageSize?: number;
  current?: number;
} & Partial<StoryType> & Partial<StoryItem> & Partial<StoryItemTimeQueryParams>;

export async function queryTimelineList(
  params: ParamsType,
): Promise<{ data:  StoryType[] }> {
  return await request('/story/list', {
    params,
  });
}

export async function deleteStory(params: ParamsType): Promise<{ data: { list: StoryType[] } }> {
  return request(`/story/${params.instanceId}`, {
    method: 'DELETE',
  });
}

export async function addStory(params: ParamsType): Promise<{ data: { list: StoryType[] } }> {
  return request('/story/add', {
    method: 'POST',
    data: {
      ...params,
      method: 'post',
    },
  });
}

export async function updateStory(params: ParamsType): Promise<{ data: { list: StoryType[] } }> {
  return await request(`/story/${params.instanceId}`, {
    method: 'PUT',
    data: {
      ...params,
      method: 'put',
    },
  });
}
export async function queryStoryDetail(itemId: string): Promise<{ data: StoryType }> {
  return request(`/story/${itemId}`, {
    method: 'GET',
  });
}
export async function addStoryItem(params: FormData): Promise<any> {
  return request(`/story/item`, {
    method: 'POST',
    data: params,
    requestType: 'form',
    getResponse: true,
  });
}
export async function updateStoryItem(params: FormData): Promise<any> {
  return request(`/story/item`, {
    method: 'PUT',
    data: params,
    requestType: 'form',
    getResponse: true,
  });
}

export async function queryStoryItem(params: ParamsType): Promise<{ data: CommonListResponse<StoryItem> }> {
  return request(`/story/item/list`, {
    method: 'GET',
    params: params,
  });
}

export async function queryStoryItemDetail(itemId: string): Promise<{ data: StoryItem }> {
  return request(`/story/item/${itemId}`, {
    method: 'GET',
  });
}
export async function countStoryItem(storyInstanceId: string): Promise<{ data: StoryItem }> {
  return request(`/story/item/count/${storyInstanceId}`, {
    method: 'GET',
  });
}

export async function queryStoryItemImages(itemId: string): Promise<{ data: string[] }> {
  return request(`/story/item/images/${itemId}`, {
    method: 'GET',
  });
}
export async function removeStoryItem(itemId: string): Promise<CommonResponse<string>> {
  return request(`/story/item/${itemId}`, {
    method: 'DELETE',
  });
}

export async function fetchImage(imageInstanceId: string): Promise<any> {
  return request(`/file/image/${imageInstanceId}`, {
    method: 'GET',
    responseType: 'blob',
    getResponse: true,
  });
}

export async function authorizeStoryPermission(params: {userId: string, storyInstanceId: string, permissionType: number}) {
  return request('/story/permission/authorize', {
    method: 'POST',
    data: params,
  });
}