package com.example.musicapp.Data.Repository

import com.example.musicapp.Data.Album
import com.example.musicapp.Data.Artist
import com.example.musicapp.Data.ArtistDetail
import com.example.musicapp.Data.Category
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path

interface DeezerApiService {
    @GET("genre?output=json")  // json endpoint instead of “/”
    suspend fun getGenres(): GenreResponse

    @GET("genre/{genre_id}/artists")
    suspend fun getArtists(@Path("genre_id") genreId: Int): ArtistResponse

    @GET("artist/{artist_id}")
    suspend fun getArtistDetail(@Path("artist_id") artistId: Int): ArtistDetailResponse

    @GET("artist/{artist_id}/albums")
    suspend fun getArtistAlbums(@Path("artist_id") artistId: Int): AlbumResponse

    data class AlbumResponse(val data: List<Album>)

    // Create a new class for storing response fields.
    data class GenreResponse(val data: List<Category>)
    data class ArtistResponse(val data: List<Artist>)
    data class ArtistDetailResponse(val id: Int, val name: String, val picture_big: String, val albums: List<Album>?)


    companion object {
        private const val BASE_URL = "https://api.deezer.com/"

        fun create(): DeezerApiService {
            val client = OkHttpClient.Builder()
                .addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
                .build()

            return Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(DeezerApiService::class.java)
        }
    }


},package com.example.musicapp.Data.Repository

import com.example.musicapp.Data.Album
import com.example.musicapp.Data.Artist
import com.example.musicapp.Data.ArtistDetail
import com.example.musicapp.Data.Category

class DeezerRepository {
    private val deezerApiService = DeezerApiService.create()

    suspend fun getCategories(): List<Category> {
        val response = deezerApiService.getGenres()
        return response.data.map { category ->
            Category(category.id, category.name, category.picture_medium)
        }
    }

    suspend fun getArtists(genreId: Int): List<Artist> {
        val response = deezerApiService.getArtists(genreId)
        return response.data
    }

    suspend fun getArtistDetail(artistId: Int): ArtistDetail {
        val response = deezerApiService.getArtistDetail(artistId)

        return ArtistDetail(
            id = response.id,
            name = response.name,
            pictureBig = response.picture_big,
            albums = response.albums
        )
    }

    suspend fun getArtistAlbums(artistId: Int): List<Album> {
        val response = deezerApiService.getArtistAlbums(artistId)
        return response.data
    }
},package com.example.musicapp.Data

data class Album(
    val id: Int,
    val title: String,
    val link: String,
    val cover: String,
    val coverSmall: String,
    val coverMedium: String,
    val coverBig: String,
    val coverXl: String,
    val releaseDate: String,
    val tracklist: String,
    val type: String
),package com.example.musicapp.Data

data class Artist(
    val id: Int,
    val name: String,
    val picture_medium: String
),package com.example.musicapp.Data

data class ArtistDetail(
    val id: Int,
    val name: String,
    val pictureBig: String,
    val albums: List<Album>?,
),package com.example.musicapp.Interface

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberAsyncImagePainter
import com.example.musicapp.Data.Album
import com.example.musicapp.ViewModel.ArtistsViewModel

@Composable
fun ArtistDetailScreen(artistId: Int) {
    val artistsViewModel: ArtistsViewModel = viewModel()
    artistsViewModel.fetchArtistDetails(artistId)
    val artistDetails by artistsViewModel.artistDetails.collectAsState()

    artistDetails?.let { details ->
        val artistDetail = details.firstOrNull()
        artistDetail?.let {
            Column {
                Text(
                    text = it.name,
                    style = MaterialTheme.typography.h6,
                    modifier = Modifier.padding(start = 16.dp, end = 16.dp, top = 16.dp, bottom = 8.dp)
                )

                val painter = rememberAsyncImagePainter(model = it.pictureBig)
                Image(
                    painter = painter,
                    contentDescription = it.name,
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .align(Alignment.CenterHorizontally)
                        .size(200.dp)
                )

                if (it.albums.isNullOrEmpty()) {
                    Log.d("ArtistDetailScreen", "No albums found for artist ${it.name}")
                }

                LazyColumn {
                    items(it.albums ?: emptyList()) { album ->
                        ArtistDetailItem(album)
                    }
                }
            }
        }
    }
}

@Composable
fun ArtistDetailItem(album: Album) {
    Row(modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)) {
        val painter = rememberAsyncImagePainter(model = album.coverMedium)
        Image(
            painter = painter,
            contentDescription = album.title,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(100.dp)
                .align(Alignment.CenterVertically)
        )

        Column(
            modifier = Modifier
                .weight(1f) // 2/3 of the width
                .padding(start = 16.dp)
                .align(Alignment.CenterVertically)
        ) {
            Text(text = album.title ?: "Unknown title", style = MaterialTheme.typography.subtitle1)
            Text(text = album.releaseDate ?: "Unknown release date", style = MaterialTheme.typography.caption)
        }
    }
} -> these are some of my classes and when i ran the app , i can observe album titles belongs to the artist when i select an artist . But  release date of the album comes empty, also album cover image is not visible on the screen either. Is there a problem with api ? Why  these data comes empty help me and show me fix please. Read about deezer api and tell me if there is an issue with them