/**
 * Shared Google Sheets param/result types (browser-safe)
 *
 * These types define the public interface for Google Sheets operations -- used by both
 * the server-side adapter and the SDK typed wrappers. They contain zero Node.js
 * dependencies and are safe to import in any environment.
 *
 * Server-internal types (credentials, API response shapes) remain in
 * server/adapters/google-sheets/google-sheets-adapter.ts
 */

// ============================================================================
// Helper Types
// ============================================================================

/**
 * Sheet information within a spreadsheet
 */
export interface SheetInfo {
  sheetId: number
  title: string
  index: number
}

// ============================================================================
// Read Sheet
// ============================================================================

/**
 * Read sheet parameters
 */
export interface ReadSheetParams {
  spreadsheetId: string
  range: string
}

/**
 * Read sheet result
 */
export interface ReadSheetResult {
  values: string[][]
}

// ============================================================================
// Write Sheet
// ============================================================================

/**
 * Write sheet parameters
 */
export interface WriteSheetParams {
  spreadsheetId: string
  range: string
  values: string[][]
}

/**
 * Write sheet result
 */
export interface WriteSheetResult {
  updatedCells: number
}

// ============================================================================
// Append Rows
// ============================================================================

/**
 * Append rows parameters
 */
export interface AppendRowsParams {
  spreadsheetId: string
  range: string
  values: string[][]
}

/**
 * Append rows result
 */
export interface AppendRowsResult {
  updatedRows: number
}

// ============================================================================
// Clear Range
// ============================================================================

/**
 * Clear range parameters
 */
export interface ClearRangeParams {
  spreadsheetId: string
  range: string
}

/**
 * Clear range result
 */
export interface ClearRangeResult {
  clearedRange: string
}

// ============================================================================
// Get Spreadsheet Metadata
// ============================================================================

/**
 * Get spreadsheet metadata parameters
 */
export interface GetSpreadsheetMetadataParams {
  spreadsheetId: string
}

/**
 * Get spreadsheet metadata result
 */
export interface GetSpreadsheetMetadataResult {
  title: string
  sheets: SheetInfo[]
}

// ============================================================================
// Batch Update
// ============================================================================

/**
 * Batch update parameters
 */
export interface BatchUpdateParams {
  spreadsheetId: string
  data: Array<{ range: string; values: string[][] }>
}

/**
 * Batch update result
 */
export interface BatchUpdateResult {
  totalUpdatedCells: number
  totalUpdatedRows: number
}

// ============================================================================
// Get Headers
// ============================================================================

/**
 * Get headers parameters
 */
export interface GetHeadersParams {
  spreadsheetId: string
  sheetName: string
}

/**
 * Get headers result
 */
export interface GetHeadersResult {
  headers: string[]
  columnCount: number
  columnMap: Record<string, string>
}

// ============================================================================
// Get Last Row
// ============================================================================

/**
 * Get last row parameters
 */
export interface GetLastRowParams {
  spreadsheetId: string
  sheetName: string
  column?: string
}

/**
 * Get last row result
 */
export interface GetLastRowResult {
  lastRow: number
  isEmpty: boolean
}

// ============================================================================
// Get Row By Value
// ============================================================================

/**
 * Get row by value parameters
 */
export interface GetRowByValueParams {
  spreadsheetId: string
  sheetName: string
  searchColumn: string
  searchValue: string
}

/**
 * Get row by value result
 */
export interface GetRowByValueResult {
  found: boolean
  rowNumber: number | null
  rowData: string[] | null
  headers: string[]
}

// ============================================================================
// Update Row By Value
// ============================================================================

/**
 * Update row by value parameters
 */
export interface UpdateRowByValueParams {
  spreadsheetId: string
  sheetName: string
  searchColumn: string
  searchValue: string
  updates: Record<string, string>
}

/**
 * Update row by value result
 */
export interface UpdateRowByValueResult {
  found: boolean
  rowNumber: number | null
  updatedCells: number
}

// ============================================================================
// Upsert Row
// ============================================================================

/**
 * Upsert row parameters
 */
export interface UpsertRowParams {
  spreadsheetId: string
  sheetName: string
  keyColumn: string
  keyValue: string
  rowData: Record<string, string>
}

/**
 * Upsert row result
 */
export interface UpsertRowResult {
  action: 'inserted' | 'updated'
  rowNumber: number
  updatedCells: number
}

// ============================================================================
// Filter Rows
// ============================================================================

/**
 * Filter rows parameters
 */
export interface FilterRowsParams {
  spreadsheetId: string
  sheetName: string
  filterColumn: string
  filterValue: string
  operator?: 'equals' | 'contains' | 'startsWith' | 'endsWith'
}

/**
 * Filter rows result
 */
export interface FilterRowsResult {
  rows: Array<{
    rowNumber: number
    data: string[]
  }>
  matchCount: number
  headers: string[]
}

// ============================================================================
// Delete Row By Value
// ============================================================================

/**
 * Delete row by value parameters
 */
export interface DeleteRowByValueParams {
  spreadsheetId: string
  sheetName: string
  searchColumn: string
  searchValue: string
}

/**
 * Delete row by value result
 */
export interface DeleteRowByValueResult {
  found: boolean
  deletedRowNumber: number | null
}
