package com.example.gallery.adapters;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.gallery.R;
import com.example.gallery.api.ApiClient;
import com.example.gallery.models.Publication;

import java.util.List;

public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHolder> {

    private final List<Publication> publications;

    public PostsAdapter(List<Publication> publications) {
        this.publications = publications;
    }

    public void updateData(List<Publication> newData) {
        publications.clear();
        publications.addAll(newData);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_post, parent, false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Publication pub = publications.get(position);

        holder.descriptionTV.setText(
                pub.description != null ? pub.description : ""
        );

        holder.mediaTypeBadge.setText(
                pub.content_type != null ? pub.content_type.toUpperCase() : ""
        );

        String mediaPath = null;

        if ("image".equals(pub.content_type)) {
            mediaPath = pub.image;
        } else if ("video".equals(pub.content_type)) {
            mediaPath = pub.video;
        }

        if (mediaPath != null && !mediaPath.isEmpty()) {

            String fullUrl;

            // ✅ CRITICAL FIX: handle both relative & absolute URLs
            if (mediaPath.startsWith("http")) {
                fullUrl = mediaPath;
            } else {
                fullUrl = ApiClient.BASE_URL + mediaPath;
            }

            Log.d("POST_IMAGE_URL", fullUrl);

            Glide.with(holder.itemView.getContext())
                    .load(fullUrl)
                    .placeholder(R.drawable.image_placeholder_background)
                    .error(R.drawable.image_placeholder_background)
                    .centerCrop()
                    .into(holder.mediaIV);

        } else {
            holder.mediaIV.setImageResource(R.drawable.image_placeholder_background);
        }
    }

    @Override
    public int getItemCount() {
        return publications.size();
    }

    static class PostViewHolder extends RecyclerView.ViewHolder {
        TextView descriptionTV;
        TextView mediaTypeBadge;
        ImageView mediaIV;

        public PostViewHolder(@NonNull View itemView) {
            super(itemView);
            descriptionTV = itemView.findViewById(R.id.postDescription);
            mediaIV = itemView.findViewById(R.id.postImage);
            mediaTypeBadge = itemView.findViewById(R.id.mediaTypeBadge);
        }
    }
}
