import axios from 'axios'
import http from './http'
import type { Queue, QueuePostData } from '@/types/Queue'

/**
 * ดึงรายการ Queue ทั้งหมดจาก backend
 */
export async function listQueues(): Promise<Queue[]> {
  const { data } = await http.get('/queues')
  return data
}

/**
 * ดึงข้อมูล Queue รายการเดียวโดยใช้ QueueID
 */
export async function getQueue(id: number): Promise<Queue> {
  const { data } = await http.get(`/queues/${id}`)
  return data
}

/**
 * สร้าง Queue ใหม่ (ใช้ QueuePostData เพื่อให้โครงสร้างถูกต้อง)
 */
export async function createQueue(payload: QueuePostData): Promise<Queue> {
  const { data } = await http.post('/queues', payload)
  return data
}

/**
 * อัปเดต Queue ตาม QueueID (ใช้ QueuePostData)
 */
export async function updateQueue(id: number, payload: QueuePostData): Promise<Queue> {
  const { data } = await http.patch(`/queues/${id}`, payload)
  return data
}

/**
 * ลบ Queue ตาม QueueID
 */
export async function deleteQueue(id: number): Promise<void> {
  await http.delete(`/queues/${id}`)
}

export async function runScheduleDirect(payload: {
  produceBuffer: boolean
  secondRound: boolean
  startDate: string
  pageID: number
}) {
  const { data } = await axios.post('http://localhost:9000/run-schedule', payload, {
    headers: {
      'Content-Type': 'application/json'
    }
  })
  return data
}