package com.example.petstoremobile.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.petstoremobile.R;
import com.example.petstoremobile.dtos.ActivityLogDTO;
import com.example.petstoremobile.utils.DateTimeUtils;

import java.util.List;

public class ActivityLogAdapter extends RecyclerView.Adapter<ActivityLogAdapter.ViewHolder> {
    private final List<ActivityLogDTO> items;

    public ActivityLogAdapter(List<ActivityLogDTO> items) {
        this.items = items;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ActivityLogDTO log = items.get(position);
        holder.tvActivity.setText(log.getActivity());
        holder.tvUser.setText(log.getFullName() + " (" + log.getUsername() + ")");
        holder.tvMeta.setText(log.getStoreName() + " · " + log.getRole());
        String timestamp = log.getLogTimestamp();
        String date = DateTimeUtils.extractDate(timestamp);
        String time = (timestamp != null && timestamp.length() >= 16) ? timestamp.substring(11, 16) : null;
        holder.tvTimestamp.setText(date != null && time != null ? date + " " + time : date);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvActivity, tvUser, tvMeta, tvTimestamp;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvActivity = itemView.findViewById(R.id.tvLogActivity);
            tvUser = itemView.findViewById(R.id.tvLogUser);
            tvMeta = itemView.findViewById(R.id.tvLogMeta);
            tvTimestamp = itemView.findViewById(R.id.tvLogTimestamp);
        }
    }
}
