/**
 * Worker Message Interface for Database Operations
 *
 * Defines the communication protocol between the main thread and the
 * SQL worker thread for TimeSafari web platform.
 *
 * @author Matthew Raymer
 * @version 1.0.0
 * @since 2025-07-02
 */

import type { QueryExecResult } from "./database";

/**
 * Base interface for all worker messages
 */
interface BaseWorkerMessage {
  id: string;
  type: string;
}

/**
 * Database query request message
 */
export interface QueryRequest extends BaseWorkerMessage {
  type: "query";
  sql: string;
  params?: unknown[];
}

/**
 * Database execution request message (INSERT, UPDATE, DELETE, etc.)
 */
export interface ExecRequest extends BaseWorkerMessage {
  type: "exec";
  sql: string;
  params?: unknown[];
}

/**
 * Database get one row request message
 */
export interface GetOneRowRequest extends BaseWorkerMessage {
  type: "getOneRow";
  sql: string;
  params?: unknown[];
}

/**
 * Database initialization request message
 */
export interface InitRequest extends BaseWorkerMessage {
  type: "init";
}

/**
 * Health check request message
 */
export interface PingRequest extends BaseWorkerMessage {
  type: "ping";
}

/**
 * Union type of all possible request messages
 */
export type WorkerRequest =
  | QueryRequest
  | ExecRequest
  | GetOneRowRequest
  | InitRequest
  | PingRequest;

/**
 * Success response from worker
 */
export interface SuccessResponse extends BaseWorkerMessage {
  type: "success";
  data: unknown;
}

/**
 * Error response from worker
 */
export interface ErrorResponse extends BaseWorkerMessage {
  type: "error";
  error: {
    message: string;
    stack?: string;
  };
}

/**
 * Initialization complete response
 */
export interface InitCompleteResponse extends BaseWorkerMessage {
  type: "init-complete";
}

/**
 * Ping response
 */
export interface PongResponse extends BaseWorkerMessage {
  type: "pong";
}

/**
 * Union type of all possible response messages
 */
export type WorkerResponse =
  | SuccessResponse
  | ErrorResponse
  | InitCompleteResponse
  | PongResponse;

/**
 * Query result type specifically for database queries
 */
export interface QueryResult {
  result: QueryExecResult[];
}

/**
 * Execution result type for database modifications
 */
export interface ExecResult {
  changes: number;
  lastId?: number;
}
