import { defineStore } from 'pinia'
import api from '../axios'

export const usePreguntaStore = defineStore('pregunta', {
  state: () => ({
    preguntas: [],
    pregunta: null,
    loading: false,
    errors: null,
  }),

  actions: {
 
    async fetchPreguntasByCurso(cursoId, params = {}) {
      this.loading = true
      this.errors = null

      try {
        const res = await api.get(
          `/admin/cursos/${cursoId}/preguntas`,
          { params }
        )

        this.preguntas = res.data.data.preguntas.data || []
        return res.data.data
      } catch (error) {
        this.errors = error.response?.data || error.message
        return null
      } finally {
        this.loading = false
      }
    },

    async fetchPregunta(id) {
      this.loading = true
      this.errors = null

      try {
        const res = await api.get(`/admin/preguntas/${id}`)
        this.pregunta = res.data.data
        return res.data.data
      } catch (error) {
        this.errors = error.response?.data || error.message
        return null
      } finally {
        this.loading = false
      }
    },

async crearPregunta(formData) {
      this.loading = true
      this.errors = null
      
      try {
        const response = await api.post('/admin/preguntas', formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        
        if (response.data.success) {
          return response.data
        } else {
          throw new Error(response.data.message || 'Error al crear pregunta')
        }
      } catch (error) {
        if (error.response?.status === 422) {
          this.errors = error.response.data.errors
        }
        throw error
      } finally {
        this.loading = false
      }
    },
    
    async actualizarPregunta(id, formData) {
      this.loading = true
      this.errors = null
      
      try {
        const response = await api.post(`/admin/preguntas/${id}`, formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
          
        })
        
        if (response.data.success) {
          return response.data
        } else {
          throw new Error(response.data.message || 'Error al actualizar pregunta')
        }
      } catch (error) {
        if (error.response?.status === 422) {
          this.errors = error.response.data.errors
        }
        throw error
      } finally {
        this.loading = false
      }
    },

async actualizarPregunta(id, formData) {
  this.loading = true
  this.errors = null
  
  try {

    const response = await api.put(`/admin/preguntas/${id}`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    
    if (response.data.success) {
      return response.data
    } else {
      throw new Error(response.data.message || 'Error al actualizar pregunta')
    }
  } catch (error) {
    if (error.response?.status === 422) {
      this.errors = error.response.data.errors
    }
    throw error
  } finally {
    this.loading = false
  }
},


    async eliminarPregunta(id) {
      this.loading = true
      this.errors = null

      try {
        await api.delete(`/admin/preguntas/${id}`)
        this.preguntas = this.preguntas.filter(p => p.id !== id)
        return true
      } catch (error) {
        this.errors = error.response?.data || error.message
        throw error
      } finally {
        this.loading = false
      }
    },

    clearPregunta() {
      this.pregunta = null
      this.errors = null
    },
  },
})
