using Adw;
using Gtk;

public class ConversationRow : Adw.ActionRow {
    public Conversation conversation;
    private Image unread_badge;
    
    public ConversationRow(Conversation conversation) {
        this.conversation = conversation;
        this.activatable = true;
        
        title = conversation.display_name.strip();
        title_lines = 1;

        var preview = conversation.last_message_preview
            .strip()
            .replace("\n", " ")
            .replace("<", "\\<")
            .replace(">", "\\>")
            .replace("&", "&amp;");

        subtitle = preview.length > 100 ? preview.substring(0, 100) : preview; 
        subtitle_lines = 1;

        add_css_class("conversation-row");

        unread_badge = new Image.from_icon_name ("media-record-symbolic");
        unread_badge.add_css_class("badge");
        unread_badge.add_css_class("accent");
        add_prefix(unread_badge);

        if (conversation.is_unread) {
            unread_badge.opacity = 1.0;
        } else {
            unread_badge.opacity = 0.0;
        }
        
        // Add timestamp if available
        if (conversation.date > 0) {
            var datetime = new DateTime.from_unix_local(conversation.date);
            if (datetime != null) {
                var now = new DateTime.now_local();
                
                string time_str;
                if (datetime.get_year() == now.get_year() && 
                    datetime.get_day_of_year() == now.get_day_of_year()) {
                    // Today - show time
                    time_str = datetime.format("%H:%M");
                } else {
                    // Not today - show date
                    time_str = datetime.format("%b %d");
                }
                
                var time_label = new Label(time_str);
                time_label.add_css_class("dim-label");
                time_label.margin_start = 8;
                add_suffix(time_label);
            }
        }
    }
}
