import { MockupShortlistProvider } from '../provider/MockupShortlistProvider'
import type { ShortlistFilters } from '../provider/IShortlistProvider'
import type { Shortlist, CreateShortlistInput, UpdateShortlistInput, ShortlistItemInput } from '../domain/shortlist'
import type { ListResponse, ItemResponse } from './types'
import { throwServiceError } from './errors'

const provider = MockupShortlistProvider

export const shortlistService = {
  async getAll(filters?: ShortlistFilters): Promise<ListResponse<Shortlist>> {
    try {
      const data = await provider.getAll(filters)
      return { data, meta: { total: data.length, page: 1, pageSize: data.length, hasMore: false } }
    } catch (err) {
      throwServiceError(err)
    }
  },
  async getById(id: string): Promise<ItemResponse<Shortlist | null>> {
    try {
      const data = await provider.getById(id)
      return { data }
    } catch (err) {
      throwServiceError(err)
    }
  },
  async create(input: CreateShortlistInput): Promise<ItemResponse<Shortlist>> {
    try {
      const data = await provider.create(input)
      return { data }
    } catch (err) {
      throwServiceError(err)
    }
  },
  async update(id: string, input: UpdateShortlistInput): Promise<ItemResponse<Shortlist>> {
    try {
      const data = await provider.update(id, input)
      return { data }
    } catch (err) {
      throwServiceError(err)
    }
  },
  async addItem(id: string, item: ShortlistItemInput): Promise<ItemResponse<Shortlist>> {
    try {
      const data = await provider.addItem(id, item)
      return { data }
    } catch (err) {
      throwServiceError(err)
    }
  },
  async removeItem(id: string, resultId: string): Promise<ItemResponse<Shortlist>> {
    try {
      const data = await provider.removeItem(id, resultId)
      return { data }
    } catch (err) {
      throwServiceError(err)
    }
  },
  async remove(id: string): Promise<ItemResponse<void>> {
    try {
      await provider.remove(id)
      return { data: undefined }
    } catch (err) {
      throwServiceError(err)
    }
  },
}
