'use server'

import {callByUser} from '@/actions/base'
import {Resource} from '@/lib/models'
import {ExtraReq, PageRecord} from '@/lib/api'

export async function listResourceShort(props: {
  page: number
  size: number
  resource_no?: string
  type?: number
  create_after?: Date
  create_before?: Date
  expire_after?: Date
  expire_before?: Date
}) {
  return await callByUser<PageRecord<Resource<1>>>('/api/resource/list/short', props)
}

export async function listResourceLong(props: {
  page: number
  size: number
  resource_no?: string
  type?: number
  create_after?: Date
  create_before?: Date
  expire_after?: Date
  expire_before?: Date
}) {
  return await callByUser<PageRecord<Resource<2>>>('/api/resource/list/long', props)
}

export async function allResource() {
  return callByUser<Resource<1 | 2>[]>('/api/resource/all')
}

export async function createResource(props: {
  type: number
  short?: {
    live: number
    mode: number
    quota: number
    expire_at: number
    daily_limit: number
  }
  long?: {
    live: number
    mode: number
    quota: number
    expire_at: number
    daily_limit: number
  }
}) {
  return await callByUser('/api/resource/create', props)
}

export async function prepareResource(props: {
  type: number
  short?: {
    live: number
    mode: number
    quota: number
    expire_at: number
    daily_limit: number
  }
  long?: {
    live: number
    mode: number
    quota: number
    expire_at: number
    daily_limit: number
  }
  payment_method: number
  payment_platform: number
}) {
  return await callByUser<{
    trade_no: string
    pay_url: string
  }>('/api/trade/create', {
    platform: props.payment_platform,
    method: props.payment_method,
    type: 1,
    resource: {
      type: props.type,
      short: props.short,
      long: props.long,
    },
  })
}

export async function completeResource(props: {
  trade_no: string
  method: number
}) {
  return callByUser('/api/trade/complete', props)
}

export async function payClose(props: {
  trade_no: string
  method: number
}) {
  return callByUser('/api/trade/cancel', props)
}
