import axios from 'axios'
import qs from 'qs'

const OKI_API = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337'
const AWTIS_POU_CHAK_PAJ = process.env.NEXT_PUBLIC_AWTIS_POU_CHAK_PAJ || 6
const readToken = process.env.NEXT_PUBLIC_READ_TOKEN || 'read-token'

const readAxiosInstance = axios.create({
  headers: {
    Authorization: `Bearer ${readToken}`
  }
})

export async function jwennTeksEpiSlug(slug) {
  const query = qs.stringify({
    populate: ['artistes', 'couverture'],
    filters: {
      slug: {
        $eq: slug
      }
    }
  }, {
    encodeValuesOnly: true
  })
  const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
  return data.data[0]
}

export async function jwennAwtisEpiSlug(slug) {
  const query = qs.stringify({
    populate: ['paroles', 'photo'],
    filters: {
      slug: {
        $eq: slug
      }
    }
  }, {
    encodeValuesOnly: true
  })
  const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
  return data.data[0]
}

export async function jwennTeksKantite() {
  const response = await axios.get(`${OKI_API}/teks/count?_where[published_at_null]=false`)
  return response.data
}

export async function jwennAwtisPajinasyon(paj) {
  const start = AWTIS_POU_CHAK_PAJ * (paj - 1)
  const query = qs.stringify({
    populate: ['paroles', 'photo'],
    sort: ['publishedAt:desc'],
    pagination: {
      start,
      limit: AWTIS_POU_CHAK_PAJ
    }
  }, {
    encodeValuesOnly: true
  })

  const {data} = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
  return data
}

export async function jwennTeks() {
  const query = qs.stringify({
    populate: ['artistes', 'couverture', 'user', 'userAdmin', 'commentaires', 'traductions', 'streamVideo', 'streamAudio'],
    sort: ['titre:asc'],
    pagination: {
      pageSize: 100
    }
  }, {
    encodeValuesOnly: true
  })

  const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
  return data.data
}

export async function jwennAwtisSlug() {
  const query = qs.stringify({
    sort: ['publishedAt:desc']
  })

  const response = await readAxiosInstance.get(`${OKI_API}/artistes?${query}`)
  const {data} = response
  return data.data.map(({attributes}) => attributes.slug)
}

export async function jwennTeksSlug() {
  const query = qs.stringify({
    sort: ['publishedAt:desc']
  })

  const response = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
  const {data} = response
  return data.data.map(({attributes}) => attributes.slug)
}

export async function jwennDenyeTeks() {
  const query = qs.stringify({
    populate: ['artistes', 'couverture'],
    sort: ['publishedAt:desc'],
    pagination: {
      limit: 6
    }
  }, {
    encodeValuesOnly: true
  })

  const {data} = await readAxiosInstance.get(`${OKI_API}/paroles?${query}`)
  return data.data
}

export async function jwennAnTeks(teksId, token) {
  const headers = {
    'content-type': 'application/json',
    Authorization: `Bearer ${token}`
  }

  const response = await axios.get(`${OKI_API}/paroles/${teksId}`, {headers})
  return response.data
}

export async function jwennKomanteEpiTeksId(teksId) {
  const query = qs.stringify({
    sort: ['publishedAt:desc'],
    populate: ['paroles', 'user'],
    filters: {
      parole: {
        id: {
          $eq: teksId
        }
      }
    }
  }, {
    encodeValuesOnly: true
  })

  const {data} = await readAxiosInstance.get(`${OKI_API}/commentaires?${query}`)
  return data.data
}

export async function jwennUserEpiToken(userToken) {
  const response = await axios.get(`${OKI_API}/users/me`, {
    headers: {
      Authorization: `Bearer ${userToken}`
    }
  })
  return response.data
}

export async function jwennUserEpiUsername(username) {
  const query = qs.stringify({
    filters: {
      username: {
        $eq: username
      }
    }
  }, {
    encodeValuesOnly: true
  })

  const response = await readAxiosInstance.get(`${OKI_API}/users?${query}`)
  return response.data[0]
}

export async function jwennUserEpiEmail(email) {
  const query = qs.stringify({
    filters: {
      email: {
        $eq: email
      },
      confirmed: {
        $eq: false
      },
      blocked: {
        $eq: false
      }
    }
  }, {
    encodeValuesOnly: true
  })

  const response = await readAxiosInstance.get(`${OKI_API}/users?${query}`)
  return response.data[0]
}

export async function passwordRequest(lyen, email) {
  await axios.post(`${OKI_API}/auth/${lyen}`, {
    email
  })
}
