package com.example.pap_teste;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pap_teste.models.Reserva;
import com.google.android.material.chip.Chip;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class ReservaAdapter extends RecyclerView.Adapter<ReservaAdapter.ViewHolder> {

    public interface OnReservaActionListener {
        void onCheckIn(Reserva reserva);
        void onCancel(Reserva reserva);
    }

    private final List<Reserva> reservas;
    private final OnReservaActionListener listener;
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());

    public ReservaAdapter(List<Reserva> reservas, OnReservaActionListener listener) {
        this.reservas = reservas;
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Reserva reserva = reservas.get(position);
        
        holder.txtRestaurante.setText(reserva.getRestaurantName());
        
        String dateStr = reserva.getDate() != null ? dateFormat.format(reserva.getDate().toDate()) : "Data N/A";
        holder.txtDataHora.setText(String.format("%s às %s • %d pessoas", dateStr, reserva.getTimeSlot(), reserva.getGuests()));
        
        String status = reserva.getStatus() != null ? reserva.getStatus() : "pending";
        holder.chipStatus.setText(status.toUpperCase());
        
        // Status Colors
        int colorBg, colorText;
        android.content.Context ctx = holder.itemView.getContext();
        if ("confirmed".equals(status)) {
            colorBg = ctx.getColor(R.color.colorChipConfirmed);
            colorText = ctx.getColor(R.color.colorChipConfirmedText);
            holder.btnCheckIn.setVisibility(View.VISIBLE);
        } else if ("cancelled".equals(status) || "rejected".equals(status)) {
            colorBg = ctx.getColor(R.color.colorChipCancelled);
            colorText = ctx.getColor(R.color.colorChipCancelledText);
            holder.btnCheckIn.setVisibility(View.GONE);
        } else {
            colorBg = ctx.getColor(R.color.colorChipPending);
            colorText = ctx.getColor(R.color.colorChipPendingText);
            holder.btnCheckIn.setVisibility(View.GONE);
        }
        
        holder.chipStatus.setChipBackgroundColor(android.content.res.ColorStateList.valueOf(colorBg));
        holder.chipStatus.setTextColor(colorText);

        boolean canCancel = "pending".equals(status) || "confirmed".equals(status);
        holder.btnCancelar.setVisibility(canCancel ? View.VISIBLE : View.GONE);

        holder.btnCheckIn.setOnClickListener(v -> listener.onCheckIn(reserva));
        holder.btnCancelar.setOnClickListener(v -> listener.onCancel(reserva));
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtRestaurante, txtDataHora;
        Button btnCheckIn, btnCancelar;
        Chip chipStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            txtRestaurante = itemView.findViewById(R.id.txtReservaRestaurante);
            txtDataHora = itemView.findViewById(R.id.txtReservaDataHora);
            btnCheckIn = itemView.findViewById(R.id.btnCheckIn);
            btnCancelar = itemView.findViewById(R.id.btnCancelar);
            chipStatus = itemView.findViewById(R.id.chipReservaStatus);
        }
    }
}

