import {
  SGW_ROUTE_PHOTO,
  SGW_ROUTE_PHOTO_TAGS,
} from '@/constants/slave/sgw/routes';
import { request } from '@umijs/max';

/**
 * Get photo from server
 * @param type Type of photo ('ship' or 'people' or 'fish')
 * @param id ID of the entity
 * @param tag Photo tag (default: 'main')
 * @returns Photo response with ArrayBuffer data
 */
export async function apiGetPhoto(
  type: SgwModel.PhotoGetParams['type'],
  id: string | number,
  tag: string = 'main',
): Promise<{ status: number; data: ArrayBuffer }> {
  const response = await request<ArrayBuffer>(
    `${SGW_ROUTE_PHOTO}/${type}/${id}/${tag}`,
    {
      method: 'GET',
      responseType: 'arraybuffer',
      getResponse: true,
    },
  );

  return {
    status: 200,
    data: response.data,
  };
}

export async function apiGetTagsPhoto(
  type: SgwModel.PhotoGetParams['type'],
  id: string | number,
) {
  return request<SgwModel.GetTagsResponse>(
    `${SGW_ROUTE_PHOTO_TAGS}/${type}/${id}`,
  );
}

/**
 * Upload photo to server
 * @param type Type of photo ('ship' or 'people' or 'fish')
 * @param id ID of the entity
 * @param file File to upload
 * @param tag Photo tag (default: 'main')
 */
export async function apiUploadPhoto(
  type: SgwModel.PhotoUploadParams['type'],
  id: string,
  file: File,
  tag: string = 'main',
): Promise<{ status: number }> {
  const formData = new FormData();
  formData.append('file', file);

  await request<void>(`${SGW_ROUTE_PHOTO}/${type}/${id}/${tag}`, {
    method: 'POST',
    data: formData,
  });

  return { status: 200 };
}

/**
 * Delete photo from server
 */
export async function apiDeletePhoto(
  type: SgwModel.PhotoGetParams['type'],
  id: string | number,
  tag: string = 'main',
): Promise<{ status: number }> {
  await request(`${SGW_ROUTE_PHOTO}/${type}/${id}/${tag}`, {
    method: 'DELETE',
  });

  return { status: 200 };
}
