import apiClient from './client'

// ============================================================================
// Types
// ============================================================================

export interface PhysicalTapeLibrary {
  id: string
  name: string
  serial_number: string
  vendor: string
  model: string
  changer_device_path: string
  changer_stable_path: string
  slot_count: number
  drive_count: number
  is_active: boolean
  discovered_at: string
  last_inventory_at?: string
  created_at: string
  updated_at: string
}

export interface VirtualTapeLibrary {
  id: string
  name: string
  mhvtl_library_id: number
  storage_path: string
  vendor?: string
  slot_count: number
  drive_count: number
  is_active: boolean
  created_at: string
  updated_at: string
}

export interface TapeDrive {
  id: string
  library_id: string
  drive_number: number
  device_path?: string
  status: string
  current_tape_id?: string
  created_at: string
  updated_at: string
}

export interface VirtualTape {
  id: string
  library_id: string
  barcode: string
  slot_number: number
  size_bytes: number
  status: string
  created_at: string
  updated_at: string
}

export interface CreateVTLRequest {
  name: string
  slot_count: number
  drive_count: number
  storage_path?: string
}

export interface CreateTapeRequest {
  barcode: string
  size_bytes: number
}

export interface LoadTapeRequest {
  drive_id: string
  tape_id: string
}

export interface UnloadTapeRequest {
  drive_id: string
}

// ============================================================================
// Physical Tape Libraries
// ============================================================================

export const physicalTapeAPI = {
  listLibraries: async (): Promise<PhysicalTapeLibrary[]> => {
    const response = await apiClient.get('/tape/physical/libraries')
    return response.data.libraries || []
  },

  getLibrary: async (id: string): Promise<PhysicalTapeLibrary> => {
    const response = await apiClient.get(`/tape/physical/libraries/${id}`)
    return response.data.library
  },

  discoverLibraries: async (): Promise<{ task_id: string }> => {
    const response = await apiClient.post('/tape/physical/libraries/discover')
    return response.data
  },

  performInventory: async (id: string): Promise<{ task_id: string }> => {
    const response = await apiClient.post(`/tape/physical/libraries/${id}/inventory`)
    return response.data
  },

  loadTape: async (libraryId: string, data: LoadTapeRequest): Promise<{ task_id: string }> => {
    const response = await apiClient.post(`/tape/physical/libraries/${libraryId}/load`, data)
    return response.data
  },

  unloadTape: async (libraryId: string, data: UnloadTapeRequest): Promise<{ task_id: string }> => {
    const response = await apiClient.post(`/tape/physical/libraries/${libraryId}/unload`, data)
    return response.data
  },
}

// ============================================================================
// Virtual Tape Libraries (VTL)
// ============================================================================

export const vtlAPI = {
  listLibraries: async (): Promise<VirtualTapeLibrary[]> => {
    const response = await apiClient.get('/tape/vtl/libraries')
    return response.data.libraries || []
  },

  getLibrary: async (id: string): Promise<{
    library: VirtualTapeLibrary
    drives: TapeDrive[]
    tapes: VirtualTape[]
  }> => {
    const response = await apiClient.get(`/tape/vtl/libraries/${id}`)
    return response.data
  },

  createLibrary: async (data: CreateVTLRequest): Promise<VirtualTapeLibrary> => {
    const response = await apiClient.post('/tape/vtl/libraries', data)
    return response.data.library
  },

  deleteLibrary: async (id: string): Promise<void> => {
    await apiClient.delete(`/tape/vtl/libraries/${id}`)
  },

  getLibraryDrives: async (id: string): Promise<TapeDrive[]> => {
    const response = await apiClient.get(`/tape/vtl/libraries/${id}/drives`)
    return response.data.drives || []
  },

  getLibraryTapes: async (id: string): Promise<VirtualTape[]> => {
    const response = await apiClient.get(`/tape/vtl/libraries/${id}/tapes`)
    return response.data.tapes || []
  },

  createTape: async (libraryId: string, data: CreateTapeRequest): Promise<VirtualTape> => {
    const response = await apiClient.post(`/tape/vtl/libraries/${libraryId}/tapes`, data)
    return response.data.tape
  },

  loadTape: async (libraryId: string, data: LoadTapeRequest): Promise<{ task_id: string }> => {
    const response = await apiClient.post(`/tape/vtl/libraries/${libraryId}/load`, data)
    return response.data
  },

  unloadTape: async (libraryId: string, data: UnloadTapeRequest): Promise<{ task_id: string }> => {
    const response = await apiClient.post(`/tape/vtl/libraries/${libraryId}/unload`, data)
    return response.data
  },
}

