#!/bin/bash

if [ -z "$BW_SESSION" ]; then
    export BW_SESSION=$(bw unlock --raw)
fi

items=$(bw list items)

# Select item with preview showing full details
selected=$(echo "$items" | jq -r '.[] | "\(.name)\t\(.login.username // "no username")"' | \
    column -t -s $'\t' | \
    fzf --preview 'name=$(echo {} | awk "{print \$1}"); bw get item "$name" 2>/dev/null | jq -r "
        \"Name: \" + .name,
        \"Type: \" + (.type | tostring),
        \"---\",
        (if .login then
            \"Username: \" + (.login.username // \"none\"),
            \"Password: \" + (if .login.password then \"********\" else \"none\" end),
            (if .login.uris then \"URIs:\", (.login.uris[] | \"  - \" + .uri) else empty end),
            (if .login.totp then \"TOTP: configured\" else empty end)
        else empty end),
        (if .notes then \"---\", \"Notes:\", .notes else empty end),
        (if .fields then \"---\", \"Custom Fields:\", (.fields[] | \"  \" + .name + \": \" + (if .type == 1 then \"********\" else (.value // \"empty\") end)) else empty end)
    " | cat' --preview-window=right:60%:wrap)

if [ -n "$selected" ]; then
    name=$(echo "$selected" | awk '{print $1}')
    
    # Get full item details
    item=$(bw get item "$name" 2>/dev/null)
    
    if [ -z "$item" ]; then
        notify-send "Bitwarden" "Failed to get item for $name"
        exit 1
    fi
    
    # Build a list of available fields
    fields=()
    
    if echo "$item" | jq -e '.login.password' &>/dev/null; then
        fields+=("Password")
    fi
    
    if echo "$item" | jq -e '.login.username' &>/dev/null; then
        fields+=("Username")
    fi
    
    if echo "$item" | jq -e '.login.uris[]' &>/dev/null; then
        fields+=("URI")
    fi
    
    if echo "$item" | jq -e '.login.totp' &>/dev/null; then
        fields+=("TOTP Code")
    fi
    
    if echo "$item" | jq -e '.notes' &>/dev/null; then
        notes=$(echo "$item" | jq -r '.notes')
        if [ -n "$notes" ] && [ "$notes" != "null" ]; then
            fields+=("Notes")
        fi
    fi
    
    if echo "$item" | jq -e '.fields[]' &>/dev/null; then
        while IFS= read -r field_name; do
            fields+=("Custom: $field_name")
        done < <(echo "$item" | jq -r '.fields[] | .name')
    fi
    
    fields+=("View All (JSON)")
    
    # Let user choose what to copy
    choice=$(printf '%s\n' "${fields[@]}" | fzf --prompt="What to copy? ")
    
    if [ -n "$choice" ]; then
        case "$choice" in
            "Password")
                echo "$item" | jq -r '.login.password' | wl-copy
                notify-send "Bitwarden" "Password copied for $name"
                ;;
            "Username")
                echo "$item" | jq -r '.login.username' | wl-copy
                notify-send "Bitwarden" "Username copied for $name"
                ;;
            "URI")
                echo "$item" | jq -r '.login.uris[0].uri' | wl-copy
                notify-send "Bitwarden" "URI copied for $name"
                ;;
            "TOTP Code")
                totp=$(bw get totp "$name" 2>/dev/null)
                echo -n "$totp" | wl-copy
                notify-send "Bitwarden" "TOTP code copied for $name"
                ;;
            "Notes")
                echo "$item" | jq -r '.notes' | wl-copy
                notify-send "Bitwarden" "Notes copied for $name"
                ;;
            Custom:*)
                field_name="${choice#Custom: }"
                value=$(echo "$item" | jq -r --arg name "$field_name" '.fields[] | select(.name == $name) | .value')
                echo -n "$value" | wl-copy
                notify-send "Bitwarden" "Custom field '$field_name' copied"
                ;;
            "View All (JSON)")
                echo "$item" | jq '.' | wl-copy
                notify-send "Bitwarden" "Full item JSON copied for $name"
                ;;
        esac
    fi
fi
