import { request } from '@/api/request'

export interface ApiResponse<T = any> {
  code: number
  data?: T
  details?: T
  message?: string
}

export interface BigScreen {
  id: number
  name: string
  description?: string
  thumbnail?: string
  group_id?: number
  goview_project_id?: string
  width: number
  height: number
  status: number
  publish_status: 'draft' | 'published' | 'offline' | string
  is_public?: boolean
  creator_name?: string
  created_at?: string
  updated_at?: string
}

export interface BigScreenGroup {
  id: number
  name: string
  description?: string
  sort_order: number
  status: number
}

export interface BigScreenPublish {
  id: number
  screen_id: number
  goview_project_id: string
  version: string
  publish_type: number
  status: number
  publisher_name?: string
  created_at?: string
}

export interface BigScreenCarousel {
  id: number
  name: string
  screen_ids: Record<string, any>
  interval_seconds: number
  enabled: boolean
}

export interface BigScreenSnapshot {
  id: number
  screen_id: number
  publish_id?: number
  file_name: string
  file_path: string
  file_size: number
  checksum?: string
  exporter_name?: string
  created_at?: string
}

export interface PageResult<T> {
  total: number
  page: number
  page_size: number
  data: T[]
}

export const fetchBigScreens = (params?: { page?: number; page_size?: number; keyword?: string }) =>
  request.get<ApiResponse<PageResult<BigScreen>>>('/Mgt/v1/screen', { params })

export const createBigScreen = (data: Partial<BigScreen>) =>
  request.post<ApiResponse<BigScreen>>('/Mgt/v1/screen', data)

export const updateBigScreen = (id: number, data: Partial<BigScreen>) =>
  request.put<ApiResponse<BigScreen>>(`/Mgt/v1/screen/${id}`, data)

export const publishBigScreen = (data: { screen_id: number; publish_type: 1 | 2; change_log?: string }) =>
  request.post<ApiResponse<BigScreenPublish>>('/Mgt/v1/publish', data)

export const fetchBigScreenPublishes = (params: { screen_id: number; page?: number; page_size?: number }) =>
  request.get<ApiResponse<PageResult<BigScreenPublish>>>('/Mgt/v1/publish', { params })

export const fetchBigScreenGroups = () =>
  request.get<ApiResponse<BigScreenGroup[]>>('/Mgt/v1/big-screen/groups')

export const createBigScreenGroup = (data: Partial<BigScreenGroup>) =>
  request.post<ApiResponse<BigScreenGroup>>('/Mgt/v1/big-screen/groups', data)

export const fetchBigScreenCarousels = () =>
  request.get<ApiResponse<BigScreenCarousel[]>>('/Mgt/v1/big-screen/carousels')

export const createBigScreenCarousel = (data: Partial<BigScreenCarousel>) =>
  request.post<ApiResponse<BigScreenCarousel>>('/Mgt/v1/big-screen/carousels', data)

export const fetchBigScreenSnapshots = (params?: { screen_id?: number }) =>
  request.get<ApiResponse<BigScreenSnapshot[]>>('/Mgt/v1/big-screen/snapshots', { params })

export const createBigScreenSnapshot = (data: Partial<BigScreenSnapshot>) =>
  request.post<ApiResponse<BigScreenSnapshot>>('/Mgt/v1/big-screen/snapshots', data)
