#compdef pastebin_client.sh

_pastebin_client() {
  local context state line
  typeset -A opt_args
  API_URL="https://paste.priet.us"

  # List of main subcommands
  local -a subcommands
  subcommands=(
    'login:Log in with username and password'
    'create:Create a paste from STDIN'
    'upload:Upload a file'
    'list:List all pastes with optional filters'
    'view:Show the content of a paste'
    'view_raw:Show raw content of a paste'
    'delete:Delete a paste'
    'register:Register a new user'
    'details:Show details about the logged-in user'
    'search:Search for pastes by a search term'
    'favorite:Add a paste to favorites'
    'unfavorite:Remove a paste from favorites'
    'download:Download the content of a paste'
    'download_all:Download all pastes'
    'download_favorites:Download all favorite pastes'
    'list_favorites:List all favorite pastes'
    'list_pastes:List all pastes of current user'
    'shared_with_others:List pastes shared with other users'
    'shared_with_me:List pastes others shared with me'
    'share:Share a paste with another user'
    'unshare:Unshare a paste with a user'
    'edit:Edit paste from command line and upload changes to the server'
    'remove_gps: remove gps metadata from pastte id'
  )

  # Define the autocompletion logic
  _arguments -C \
    '1:subcommand:->cmd' \
    '*::argument:->args'

  case $state in
    cmd)
      # Display available subcommands
      _describe -t subcommands 'subcommand' subcommands
      ;;
    args)
      case $words[1] in
        login)
          # login <username> <password>
          _arguments \
            '1:Username:_default' \
            '2:Password:_default'
          ;;
        create)
          _arguments \
            '1:Expire after 1 day:_expire_options' \
            '2:Private:_private_options'  # ✅ Referencia a listas de valores
          ;;
        remove_gps)
          # remove_gps <paste_id>
          _dynamic_paste_ids
          ;;
          # upload <file> <language>
        upload)
          _arguments \
            '1:File:_files' \
            '2:Expire after 1 day:_expire_options' \
            '3:Private:_private_options'  # ✅ Referencia a listas de valores
          ;;
        list)
          # list options: --type, --from, --to
          _arguments \
            '--type=[Filter by paste type]:type:(Text Image Video)' \
            '--from=[Start date (YYYY-MM-DD)]' \
            '--to=[End date (YYYY-MM-DD)]'
          ;;
        view|view_raw|delete|favorite|unfavorite|download|edit)
          # view <paste_id>, view_raw <paste_id>, delete <paste_id>, favorite <paste_id>, unfavorite <paste_id>, download <paste_id>, edit <paste_id>
          _dynamic_paste_ids
          ;;
        download_all|download_favorites|list_favorites)
          # No additional arguments
          ;;
        register)
          # register <username> <password>
          _arguments \
            '1:Username:_default' \
            '2:Password:_default'
          ;;
        details)
          # details (no arguments)
          ;;
        search)
          # search <query>
          _arguments '1:Search query:_default'
          ;;
        share|unshare)
          # share <paste_id> <username> [can_edit]
          if [[ $words[1] == "share" ]]; then
            _arguments \
              '1:Paste ID:_dynamic_paste_ids' \
              '2:Username:_dynamic_usernames' \
              '3:Can Edit:(true false)'
          else
            # unshare <paste_id> <username>
            _arguments \
              '1:Paste ID:_dynamic_paste_ids' \
              '2:Username:_dynamic_usernames'
          fi
          ;;
      esac
      ;;
  esac
}

_expire_options() {
  _values "Expiration option" \
    "yes[The paste will expire after 1 day]" \
    "no[The paste will not expire]"
}

_private_options() {
  _values "Privacy option" \
    "yes[The paste will be private]" \
    "no[The paste will be public]"
}

# Function to dynamically fetch paste IDs from the server
_dynamic_paste_ids() {
  local token_file="$HOME/.pastebin_token"
  local token
  local response
  local paste_ids=()

  # Check if the token file exists
  if [[ -f "$token_file" ]]; then
    token=$(cat "$token_file")
  else
    _message "No token found. Please log in first."
    return
  fi

  # Fetch paste IDs using the API
  response=$(curl -s -H "Authorization: Bearer $token" "$API_URL/pastes")

  # Parse the response to extract paste IDs
  if [[ $? -eq 0 ]]; then
    paste_ids=($(echo "$response" | jq -r '.[].id'))
  fi

  if (( ${#paste_ids[@]} )); then
    _values "paste_id" "${paste_ids[@]}"
  else
    _message "No pastes found."
  fi
}

# Function to dynamically fetch usernames from the server
_dynamic_usernames() {
  local token_file="$HOME/.pastebin_token"
  local token
  local response
  local usernames=()

  # Check if the token file exists
  if [[ -f "$token_file" ]]; then
    token=$(cat "$token_file")
  else
    _message "No token found. Please log in first."
    return
  fi

  # Fetch usernames using the API
  response=$(curl -s -H "Authorization: Bearer $token" "$API_URL/api/users")

  # Parse the response to extract usernames
  if [[ $? -eq 0 ]]; then
    usernames=($(echo "$response" | jq -r '.users[]'))
  fi

  if (( ${#usernames[@]} )); then
    _values "username" "${usernames[@]}"
  else
    _message "No users found."
  fi
}

# Associate the autocompletion function with the script name
compdef _pastebin_client pastebin_client.sh

