import apiClient from './client'
import type {
  Step,
  StepListItem,
  StepCreate,
  StepUpdate,
  StepListParams,
  PopularTag,
  RatingCreate,
  RatingUpdate,
  Rating,
  Review
} from '@/types/step'

export const stepsApi = {
  async list(params?: StepListParams): Promise<StepListItem[]> {
    const response = await apiClient.get<StepListItem[]>('/steps', { params })
    return response.data
  },

  async get(id: string): Promise<Step> {
    const response = await apiClient.get<Step>(`/steps/${id}`)
    return response.data
  },

  async create(data: StepCreate): Promise<Step> {
    const response = await apiClient.post<Step>('/steps', data)
    return response.data
  },

  async update(id: string, data: StepUpdate): Promise<Step> {
    const response = await apiClient.put<Step>(`/steps/${id}`, data)
    return response.data
  },

  async delete(id: string): Promise<void> {
    await apiClient.delete(`/steps/${id}`)
  },

  async search(query: string): Promise<StepListItem[]> {
    const response = await apiClient.get<StepListItem[]>('/steps/search', {
      params: { q: query }
    })
    return response.data
  },

  async getPopularTags(): Promise<PopularTag[]> {
    const response = await apiClient.get<PopularTag[]>('/steps/tags/popular')
    return response.data
  },

  async rate(id: string, data: RatingCreate): Promise<Rating> {
    const response = await apiClient.post<Rating>(`/steps/${id}/ratings`, data)
    return response.data
  },

  async updateRating(id: string, data: RatingUpdate): Promise<Rating> {
    const response = await apiClient.put<Rating>(`/steps/${id}/ratings`, data)
    return response.data
  },

  async deleteRating(id: string): Promise<void> {
    await apiClient.delete(`/steps/${id}/ratings`)
  },

  async getReviews(id: string): Promise<Review[]> {
    const response = await apiClient.get<Review[]>(`/steps/${id}/reviews`)
    return response.data
  }
}

export default stepsApi
