set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim

let g:ycm_auto_trigger = 1

filetype plugin indent on    " required

set clipboard=unnamedplus

function! GitBranch()
  let l:branch =  system("bash -c 'uptime'")
  return l:branch
endfunction

call plug#begin()
   " List your plugins here


" Install fzf and fzf.vim plugins
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'


Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'
  Plug 'tpope/vim-sensible'
  Plug 'Exafunction/codeium.vim', { 'branch': 'main' }
  Plug 'prabirshrestha/vim-lsp'
    Plug 'mattn/vim-lsp-settings'
Plug 'Exafunction/windsurf.vim', { 'branch': 'main' }

    set statusline=\{…\}%3{codeium#GetStatusString()}\%h%m%r\ [%l,%c]\ %p%%
highlight StatusLine guifg=#ffffff guibg=#005f87
highlight StatusLineNC guifg=#aaaaaa guibg=#303030
highlight ErrorMsg ctermfg=Red ctermbg=NONE guifg=#FF0000 guibg=NONE



call plug#end()

let g:codeium_idle_delay = 75

call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'ycm-core/YouCompleteMe'
call vundle#end()

set mouse=a
set backspace=indent,eol,start
syntax on
filetype plugin indent on

set showtabline=2
set enc=utf-8
set fenc=utf-8
set termencoding=utf-8
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
set number
set showmatch
set comments=sl:/*,mb:\ *,elx:\ */

let mapleader = ","

function! ShowInTerminal(content)
  " Open a new terminal split
  belowright split | terminal
  " Wait for terminal to initialize
  call feedkeys("i")  " go into insert mode to send keys
  call chansend(b:terminal_job_id, a:content . "\n")
endfunction

function! AIR() 
    let l:ai_prompt = input("Enter AI instructions: ")
    let $AI_PROMPT = l:ai_prompt
    let l:result = system('r ' . l:ai_prompt)
    call ShowInTerminal(l:result)

endfunction

function! AIPromptSelection()
    " Save cursor position and the unnamed register
    let l:pos          = getpos('.')
    let l:save_reg     = @"
    let l:save_regtype = getregtype('"')

    " Yank the visual selection into the unnamed register
    normal! gvy
    let l:selected_text = @"

    " Restore the unnamed register
    call setreg('"', l:save_reg, l:save_regtype)

    " Prompt the user for AI instructions
    let l:ai_prompt = input('Enter AI instructions: ')
    let $AI_PROMPT  = l:ai_prompt

    " Call your external AI formatter (replace `c` with your command)
    let l:formatted = system('c', l:selected_text)

    " Handle errors or replace the selection with the AI response
    if v:shell_error
        echohl ErrorMsg | echom 'Formatting failed' | echohl None
    else
        " Delete the original selection
        normal! gvd
        " Insert the formatted text *before* the cursor (same spot)
        put! =l:formatted
        " Restore the original cursor position
        call setpos('.', l:pos)
    endif

    " Restore the unnamed register again (good measure)
    call setreg('"', l:save_reg, l:save_regtype)
endfunction



" New function to handle AI prompt for selected text
function! AiEditSelection()
    " (1) Get user prompt
    let l:instruction = input("AI instruction: ")
    if empty(l:instruction)
        echo "No instruction given, cancelled."
        return
    endif

    " (2) Get visual selection
    let l:origText = GetVisualSelection()
    if empty(l:origText)
        echo "No selection."
        return
    endif

    " (3) Compose the API prompt
    let l:prompt = l:instruction . "\n\nHere is the text:\n" . l:origText . "\n\nOutput only the transformed text. Do not include explanations, markdown, or code blocks."

    " (4) Send to API -- replace this with your favorite endpoint
    let l:api_key = $OPENAI_API_KEY  " or set it here
    let l:json = '{"model":"gpt-4o-mini","messages":[{"role":"user","content":'.json_encode(l:prompt).'}]}'
    let l:cmd = 'curl -sS -X POST "https://api.openai.com/v1/chat/completions" -H "Authorization: Bearer ' . l:api_key . '" -H "Content-Type: application/json" -d ' . shellescape(l:json)
    let l:reply = system(l:cmd)
    " --- Extract output (simple JSON parsing) ---
    let l:text = matchstr(l:reply, '"content":\s*"\zs\(.\{-}\)\ze"\s*}')
    let l:text = substitute(l:text, '\\n', "\n", 'g')
    let l:text = substitute(l:text, '\\"', '"', 'g')

    " (5) Replace selected text
    normal! gv
    " Use c to change or d (delete and then 'put') depending on visual mode
    normal! c
    call feedkeys(l:text, 'n')
endfunction


" Map the AI prompt function to a key combination
vnoremap <Leader>f :<C-u>call AIPromptSelection()<CR>
nnoremap <Leader>r :call AIR()<CR>

" Existing keymappings and other configurations...
inoremap <C-n> <ESC>:tabnext<CR>
inoremap <C-p> <ESC>:tabnext<CR>
nnoremap <C-n> :tabnext<CR>
nnoremap <C-p> :tabnext<CR>
nnoremap <Tab> :tabnext<CR>
nnoremap <C-Tab> :tabprevious<CR>
inoremap <C-t> <ESC>:terminal<CR><CR><C-w>r<CR><C-w>-<C-w>-<C-w>-<C-w>-<C-w>-<C-w>-<C-w>-
nnoremap <C-e> :tabnew<Space>
inoremap <C-e> <ESC>:tabnew<Space>
nnoremap e :tabnew<Space>
nnoremap q <ESC>:q<CR>

set undofile
set undodir=~/.vim/undo

" Existing commands and other configurations...
command! GPT py3file ~/bin/gpt
command! Refactor py3file /home/retoor/.vim/plugin/refactor.py
command! HelloVim py3file ~/.vim/plugin/hello.py | py3 say_hello()

if has("autocmd")
    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

execute pathogen#infect()
execute pathogen#helptags()

" Helper: get visually selected text
function! GetVisualSelection()
    let [line_start, col_start] = [line("'<"), col("'<")]
    let [line_end, col_end] = [line("'>"), col("'>")]
    let lines = getline(line_start, line_end)
    if len(lines) == 0
        return ''
    endif
    " Trim col for first/last line if selection is characterwise
    let lines[0] = lines[0][col_start - 1 :]
    let lines[-1] = lines[-1][: col_end - (line_start == line_end ? 1 : 2)]
    return join(lines, "\n")
endfunction

function! AiEditSelection()
    " (1) Get user prompt
    let l:instruction = input("AI instruction: ")
    if empty(l:instruction)
        echo "No instruction given, cancelled."
        return
    endif

    " (2) Get visual selection
    let l:origText = GetVisualSelection()
    if empty(l:origText)
        echo "No selection."
        return
    endif

    " (3) Compose the API prompt
    let l:prompt = l:instruction . "\n\nHere is the text:\n" . l:origText . "\n\nOutput only the transformed text. Do not include explanations, markdown, or code blocks."

    " (4) Send to API -- replace this with your favorite endpoint
    let l:api_key = $OPENAI_API_KEY  " or set it here
    let l:json = '{"model":"gpt-4o-mini","messages":[{"role":"user","content":'.json_encode(l:prompt).'}]}'
    let l:cmd = 'curl -sS -X POST "https://api.openai.com/v1/chat/completions" -H "Authorization: Bearer ' . l:api_key . '" -H "Content-Type: application/json" -d ' . shellescape(l:json)
    let l:reply = system(l:cmd)
    " --- Extract output (simple JSON parsing) ---
    let l:text = matchstr(l:reply, '"content":\s*"\zs\(.\{-}\)\ze"\s*}')
    let l:text = substitute(l:text, '\\n', "\n", 'g')
    let l:text = substitute(l:text, '\\"', '"', 'g')

    " (5) Replace selected text
    normal! gv
    " Use c to change or d (delete and then 'put') depending on visual mode
    normal! c
    call feedkeys(l:text, 'n')
endfunction

" Visual mode mapping
xnoremap <silent> <Leader>a :<C-u>call AiEditSelection()<CR>

command! GreetPython py3 greet()
vnoremap <leader>gr :GPTRefactor<CR>
vnoremap <leader>gd :GPTRefactorDefault<CR>


if executable('pylsp')
    " pip install python-lsp-server
    au User lsp_setup call lsp#register_server({
        \ 'name': 'pylsp',
        \ 'cmd': {server_info->['pylsp']},
        \ 'allowlist': ['python'],
        \ })
endif

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    setlocal signcolumn=yes
    if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gs <plug>(lsp-document-symbol-search)
    nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
    nmap <buffer> gr <plug>(lsp-references)
    nmap <buffer> gi <plug>(lsp-implementation)
    nmap <buffer> gt <plug>(lsp-type-definition)
    nmap <buffer> <leader>rn <plug>(lsp-rename)
    nmap <buffer> [g <plug>(lsp-previous-diagnostic)
    nmap <buffer> ]g <plug>(lsp-next-diagnostic)
    nmap <buffer> K <plug>(lsp-hover)
    nnoremap <buffer> <expr><c-f> lsp#scroll(+4)
    nnoremap <buffer> <expr><c-d> lsp#scroll(-4)

    let g:lsp_format_sync_timeout = 1000
    autocmd! BufWritePre *.rs,*.go call execute('LspDocumentFormatSync')

    " refer to doc to add more commands
endfunction

augroup lsp_install
    au!
    " call s:on_lsp_buffer_enabled only for languages that has the server registered.
    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END

let g:lsp_document_highlight_enabled = 1
let g:lsp_diagnostics_enabled = 0         " disable diagnostics support


" Optional: Map keys for quick access
nnoremap <leader>f :Files<CR>
nnoremap <leader>b :Buffers<CR>
nnoremap <leader>l :Lines<CR>
nnoremap <leader>g :GFiles<CR>



