import request from './request'
import { GENERATE_REQUEST_TIMEOUT } from './timeouts'

function unwrap(res) {
  return res && Object.prototype.hasOwnProperty.call(res, 'data') ? res.data : res
}

export function createProject(data) {
  return request({
    url: '/front/project/create',
    method: 'post',
    data
  }).then(unwrap)
}

export function listProjects() {
  return request({
    url: '/front/project/list',
    method: 'get'
  }).then(unwrap)
}

export function getProject(projectId) {
  return request({
    url: `/front/project/${projectId}`,
    method: 'get'
  }).then(unwrap)
}

export function updateProject(projectId, data) {
  return request({
    url: `/front/project/${projectId}`,
    method: 'put',
    data
  }).then(unwrap)
}

export function deleteProject(projectId) {
  return request({
    url: `/front/project/${projectId}`,
    method: 'delete'
  }).then(unwrap)
}

export function listAvailableModules() {
  return request({
    url: '/front/project/options/modules',
    method: 'get'
  }).then(unwrap)
}

export function generateDatabase(projectId, data) {
  return request({
    url: `/front/project/${projectId}/generate-database`,
    method: 'post',
    timeout: GENERATE_REQUEST_TIMEOUT,
    data
  }).then(unwrap)
}

export function generateAppBlueprint(projectId, data) {
  return request({
    url: `/front/project/${projectId}/generate-app-blueprint`,
    method: 'post',
    timeout: GENERATE_REQUEST_TIMEOUT,
    data
  }).then(unwrap)
}

export function generateBusinessBlueprint(projectId, data) {
  return request({
    url: `/front/project/${projectId}/generate-business-blueprint`,
    method: 'post',
    timeout: GENERATE_REQUEST_TIMEOUT,
    data
  }).then(unwrap)
}

export function createAiTask(projectId, data) {
  return request({
    url: `/front/project/${projectId}/ai-tasks`,
    method: 'post',
    data
  }).then(unwrap)
}

export function getAiTask(projectId, taskId) {
  return request({
    url: `/front/project/${projectId}/ai-tasks/${taskId}`,
    method: 'get'
  }).then(unwrap)
}

export function retryAiTask(projectId, taskId) {
  return request({
    url: `/front/project/${projectId}/ai-tasks/${taskId}/retry`,
    method: 'post'
  }).then(unwrap)
}

export function cancelAiTask(projectId, taskId) {
  return request({
    url: `/front/project/${projectId}/ai-tasks/${taskId}/cancel`,
    method: 'post'
  }).then(unwrap)
}

export function getAiQuota() {
  return request({
    url: '/front/project/ai-quota',
    method: 'get'
  }).then(unwrap)
}

export function listGenerations(projectId) {
  return request({
    url: `/front/project/${projectId}/generations`,
    method: 'get'
  }).then(unwrap)
}

export function getDatabase(projectId) {
  return request({
    url: `/front/project/${projectId}/database`,
    method: 'get'
  }).then(unwrap)
}

export function saveDatabase(projectId, data) {
  return request({
    url: `/front/project/${projectId}/database`,
    method: 'put',
    data
  }).then(unwrap)
}

export function listPageDesigns(projectId, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/page-designs`,
    method: 'get',
    params: { scope }
  }).then(unwrap)
}

export function initPageDesigns(projectId, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/page-designs/init`,
    method: 'post',
    params: { scope }
  }).then(unwrap)
}

export function createPageDesign(projectId, data, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/page-designs`,
    method: 'post',
    params: { scope },
    data
  }).then(unwrap)
}

export function getPageDesign(projectId, designId, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/page-designs/${designId}`,
    method: 'get',
    params: { scope }
  }).then(unwrap)
}

export function savePageDesign(projectId, designId, data, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/page-designs/${designId}`,
    method: 'put',
    params: { scope },
    data
  }).then(unwrap)
}

export function deletePageDesign(projectId, designId, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/page-designs/${designId}`,
    method: 'delete',
    params: { scope }
  }).then(unwrap)
}

export function listBusinessBlocks(projectId, scope = 'frontend') {
  return request({
    url: `/front/project/${projectId}/business-blocks`,
    method: 'get',
    params: { scope }
  }).then(unwrap)
}

export function generateProject(projectId) {
  return request({
    url: `/front/project/${projectId}/preview`,
    method: 'post',
    timeout: GENERATE_REQUEST_TIMEOUT
  }).then(unwrap)
}

export function getProjectStructure(projectId, templateType) {
  return request({
    url: `/front/project/${projectId}/structure`,
    method: 'get',
    params: { templateType }
  }).then(unwrap)
}

export function getFileContent(projectId, params) {
  return request({
    url: `/front/project/${projectId}/file-content`,
    method: 'get',
    params
  }).then(unwrap)
}

export function downloadProject(projectId, templateType) {
  return request({
    url: `/front/project/${projectId}/download`,
    method: 'get',
    params: templateType ? { templateType } : undefined,
    responseType: 'blob',
    timeout: GENERATE_REQUEST_TIMEOUT
  }).then(unwrap)
}

export function startRunPreview(projectId) {
  return request({
    url: `/front/project/${projectId}/run-preview`,
    method: 'post',
    timeout: GENERATE_REQUEST_TIMEOUT
  }).then(unwrap)
}

export function getRunPreviewStatus(projectId) {
  return request({
    url: `/front/project/${projectId}/run-preview`,
    method: 'get'
  }).then(unwrap)
}

export function stopRunPreview(projectId) {
  return request({
    url: `/front/project/${projectId}/run-preview/stop`,
    method: 'post'
  }).then(unwrap)
}
