import axios from 'axios'
import api from './axios'

const BASE_URL = '/admin/additional-activities'
export const activitiesAPI = {
  /**
   * Get all activities with pagination, search, and sorting
   */
  async getAll(params = {}) {
    try {
      const response = await axios.get(`${BASE_URL}/all`, { params })
      return response.data
    } catch (error) {
      console.error('Failed to fetch activities:', error)
      throw error
    }
  },

  /**
   * Get activity by ID
   */
  async getById(id, params = {}) {
    try {
      const response = await axios.get(`${BASE_URL}/${id}`, { params })
      return response.data
    } catch (error) {
      console.error('Failed to fetch activity:', error)
      throw error
    }
  },

  /**
   * Create new activity
   * @param {Object} data - Activity data
   * @param {string} data.title - Activity title
   * @param {string} data.activity_name - Activity name
   * @param {string} data.activity_heading - Activity heading
   * @param {string} data.activity_description - Activity description
   * @param {string} data.created_for - Created for field
   * @param {number} data.age - Age field
   * @param {string} data.grade - Grade field
   * @param {string} data.duration - Duration field
   * @param {string} data.slug - Slug field
   * @param {string} data.desctiprtion - Activity description (note: API has typo)
   * @param {string} data.image - Activity image URL
   * @param {Object} data.cta_button - CTA button object
   * @param {string} data.cta_button.text - Button text
   * @param {string} data.cta_button.url - Button URL
   * @param {string} data.status - Activity status (draft/publish)
   */
  async create(data) {
    try {
      const response = await axios.post(`${BASE_URL}/add`, data)
      return response.data
    } catch (error) {
      console.error('Failed to create activity:', error)
      throw error
    }
  },

  /**
   * Update existing activity
   */
  async update(id, data) {
    try {
      const response = await axios.post(`${BASE_URL}/${id}`, data)
      return response.data
    } catch (error) {
      console.error('Failed to update activity:', error)
      throw error
    }
  },

  /**
   * Delete activity
   */
  async delete(id, params = {}) {
    try {
      const response = await axios.delete(`${BASE_URL}/${id}`, { params })
      return response.data
    } catch (error) {
      console.error('Failed to delete activity:', error)
      throw error
    }
  },

  async adoptGlobal(id, language = 'en') {
    try {
      const response = await api.post(`admin/additional-activities/${id}/adopt-global`, { language })
      return response.data
    } catch (error) {
      console.error('Failed to adopt global activity translation:', error)
      throw error
    }
  },

  async resetToGlobal(id, language = 'en') {
    try {
      const response = await api.post(`admin/additional-activities/${id}/reset-global`, { language })
      return response.data
    } catch (error) {
      console.error('Failed to reset activity translation to global:', error)
      throw error
    }
  },

  async updateToLatestGlobal(id, language = 'en') {
    try {
      const response = await api.post(`admin/additional-activities/${id}/update-latest-global`, { language })
      return response.data
    } catch (error) {
      console.error('Failed to update activity translation to latest global:', error)
      throw error
    }
  },

  async getResetPreview(id, language = 'en') {
    try {
      const response = await api.get(`admin/additional-activities/${id}/reset-preview`, {
        params: { language },
      })
      return response.data
    } catch (error) {
      console.error('Failed to fetch activity reset preview:', error)
      throw error
    }
  },

  async getUpdatePreview(id, language = 'en') {
    try {
      const response = await api.get(`admin/additional-activities/${id}/update-preview`, {
        params: { language },
      })
      return response.data
    } catch (error) {
      console.error('Failed to fetch activity update preview:', error)
      throw error
    }
  },

  /**
   * Reorder activities
   * @param {Array} activities - Array of activities with id and order_no
   */
  async reorder(activities) {
    try {
      const response = await axios.post(`${BASE_URL}/reorder`, { activities })
      return response.data
    } catch (error) {
      console.error('Failed to reorder activities:', error)
      throw error
    }
  },
}

export const translateActivitiesApi = (payload) => api.post('admin/translate', payload)

export default activitiesAPI
