import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { VideoContentVO, VideoContentForm, VideoContentQuery } from './types';

/**
 * 查询视频内容信息列表
 * @param query
 * @returns {*}
 */
export const listVideoContent = (query?: VideoContentQuery): AxiosPromise<VideoContentVO[]> => {
  return request({
    url: '/physical/videoContent/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询视频内容信息详细
 * @param id
 */
export const getVideoContent = (id: string | number): AxiosPromise<VideoContentVO> => {
  return request({
    url: '/physical/videoContent/' + id,
    method: 'get'
  });
};

/**
 * 新增视频内容信息
 * @param data
 */
export const addVideoContent = (data: VideoContentForm) => {
  return request({
    url: '/physical/videoContent',
    method: 'post',
    data: data
  });
};

/**
 * 修改视频内容信息
 * @param data
 */
export const updateVideoContent = (data: VideoContentForm) => {
  return request({
    url: '/physical/videoContent',
    method: 'put',
    data: data
  });
};

/**
 * 删除视频内容信息
 * @param id
 */
export const delVideoContent = (id: string | number | Array<string | number>) => {
  return request({
    url: '/physical/videoContent/' + id,
    method: 'delete'
  });
};

/**
 * 上传视频
 * @param file 文件
 * @param title 视频标题
 * @param requiredPoints 所需积分
 * @param previewDurationSeconds 试看时长
 * @param subscriptionValidHours 订阅有效期
 * @param categoryTagId 所属标签ID
 */
export const uploadVideo = async (file: File, title: string, requiredPoints: number, previewDurationSeconds: number, subscriptionValidHours: number, categoryTagId?: string | number) => {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('title', title);
  formData.append('requiredPoints', String(requiredPoints));
  formData.append('previewDurationSeconds', String(previewDurationSeconds));
  formData.append('subscriptionValidHours', String(subscriptionValidHours));
  if (categoryTagId !== undefined) {
    formData.append('categoryTagId', String(categoryTagId));
  }

  const res = await request({
    url: '/physical/videoContent/uploadVideo',
    method: 'POST',
    data: formData,
    headers: { 'Content-Type': 'multipart/form-data' }
  });
  return res;
};

/**
 * 备用匹配接口
 * @param videoId 视频ID
 * @returns {*}
 */
export const matchFromBackup = (videoId: string | number): AxiosPromise => {
  return request({
    url: `/physical/videoContent/matchFromBackup/${videoId}`,
    method: 'POST'
  });
};

/**
 * 获取视频时长
 * @param id 视频ID
 */
export const getVideoDuration = (id: string | number): AxiosPromise<number> => {
  return request({
    url: `/physical/videoContent/getVideoDuration/${id}`,
    method: 'GET'
  });
};
