--- SQLite storage backend for screw.nvim
---
--- This module implements the StorageBackend interface using SQLite database.
--- Note: This is a placeholder implementation - actual SQLite integration
--- would require additional dependencies.
---

local utils = require("screw.utils")

local M = {}

--- Create a new SQLite storage backend
---@param config table Storage configuration
---@return StorageBackend
function M.new(config)
  local backend = {}

  backend.db_path = nil
  backend.notes = {}
  backend.config = config

  --- Initialize storage
  function backend:setup()
    -- Build full database path from directory and filename
    local storage_dir = self.config.path
    local filename = self.config.filename or "screw_notes_" .. os.date("%Y%m%d_%H%M%S") .. ".db"
    -- For SQLite, ensure .db extension
    if not filename:match("%.db$") then
      filename = filename:gsub("%.json$", ".db")
      if not filename:match("%.db$") then
        filename = filename .. ".db"
      end
    end
    self.db_path = storage_dir .. "/" .. filename

    utils.ensure_dir(storage_dir)

    -- For now, fall back to in-memory storage
    -- TODO: Implement actual SQLite integration
    utils.warn("SQLite backend not fully implemented, using in-memory storage")
    self.notes = {}
  end

  --- Load notes from storage
  function backend:load_notes()
    -- TODO: Implement SQLite loading
    -- For now, notes remain empty
    self.notes = {}
  end

  --- Save notes to storage
  ---@return boolean
  function backend:save_notes()
    -- TODO: Implement SQLite saving
    -- For now, always return true
    return true
  end

  --- Get all notes
  ---@return ScrewNote[]
  function backend:get_all_notes()
    return self.notes
  end

  --- Get note by ID
  ---@param id string
  ---@return ScrewNote?
  function backend:get_note(id)
    for _, note in ipairs(self.notes) do
      if note.id == id then
        return utils.deep_copy(note)
      end
    end
    return nil
  end

  --- Save a note (create or update)
  ---@param note ScrewNote
  ---@return boolean
  function backend:save_note(note)
    if not note or not note.id then
      return false
    end

    -- Find existing note
    local found_index = nil
    for i, existing_note in ipairs(self.notes) do
      if existing_note.id == note.id then
        found_index = i
        break
      end
    end

    if found_index then
      -- Update existing note
      self.notes[found_index] = utils.deep_copy(note)
    else
      -- Add new note
      table.insert(self.notes, utils.deep_copy(note))
    end

    return true
  end

  --- Delete a note
  ---@param id string
  ---@return boolean
  function backend:delete_note(id)
    local found_index = nil
    for i, note in ipairs(self.notes) do
      if note.id == id then
        found_index = i
        break
      end
    end

    if found_index then
      table.remove(self.notes, found_index)
      return true
    end

    return false
  end

  --- Get notes for a specific file
  ---@param file_path string
  ---@return ScrewNote[]
  function backend:get_notes_for_file(file_path)
    local file_notes = {}
    for _, note in ipairs(self.notes) do
      if note.file_path == file_path then
        table.insert(file_notes, utils.deep_copy(note))
      end
    end
    return file_notes
  end

  --- Get notes for a specific line
  ---@param file_path string
  ---@param line_number number
  ---@return ScrewNote[]
  function backend:get_notes_for_line(file_path, line_number)
    local line_notes = {}
    for _, note in ipairs(self.notes) do
      if note.file_path == file_path and note.line_number == line_number then
        table.insert(line_notes, utils.deep_copy(note))
      end
    end
    return line_notes
  end

  --- Clear all notes (for testing)
  function backend:clear_notes()
    self.notes = {}
  end

  --- Force save notes
  ---@return boolean
  function backend:force_save()
    return self:save_notes()
  end

  --- Get storage statistics
  ---@return table
  function backend:get_storage_stats()
    local stats = {
      total_notes = #self.notes,
      storage_path = self.db_path,
      file_exists = utils.file_exists(self.db_path or ""),
      auto_save = self.config.auto_save,
      backend_type = "sqlite",
    }

    return stats
  end

  return backend
end

return M
