/**
 * Mapeadores puros para normalizar filas SQLite a DTOs usados por TaskService.
 * Mantienen las mismas formas que consumen comandos, recordatorios y API web.
 */

export function mapTaskBasicRow(
  row: { id?: number | null; description?: string | null; due_date?: string | null; display_code?: number | null }
): {
  id: number;
  description: string;
  due_date: string | null;
  display_code: number | null;
} {
  return {
    id: Number(row.id),
    description: String(row.description || ''),
    due_date: row.due_date ? String(row.due_date) : null,
    display_code: row.display_code != null ? Number(row.display_code) : null,
  };
}

export function mapTaskListItem(
  row: { id: number; description: string; due_date: string | null; group_id: string | null; display_code: number | null },
  assignees: string[]
): {
  id: number;
  description: string;
  due_date: string | null;
  group_id: string | null;
  display_code: number | null;
  assignees: string[];
} {
  return {
    id: Number(row.id),
    description: String(row.description || ''),
    due_date: row.due_date ? String(row.due_date) : null,
    group_id: row.group_id ? String(row.group_id) : null,
    display_code: row.display_code != null ? Number(row.display_code) : null,
    assignees: Array.isArray(assignees) ? assignees.map(a => String(a)) : []
  };
}

export function mapTaskWithGroupNameRow(
  row: { id: number; description: string; due_date: string | null; group_id: string | null; display_code: number | null; group_name: string | null }
): {
  id: number;
  description: string;
  due_date: string | null;
  group_id: string | null;
  group_name: string | null;
  display_code: number | null;
} {
  return {
    id: Number(row.id),
    description: String(row.description || ''),
    due_date: row.due_date ? String(row.due_date) : null,
    group_id: row.group_id ? String(row.group_id) : null,
    group_name: row.group_name ? String(row.group_name) : null,
    display_code: row.display_code != null ? Number(row.display_code) : null
  };
}

export function mapTaskDetailsRow(
  row: { id?: number; description?: string; due_date?: string | null; group_id?: string | null; display_code?: number | null; completed?: number; completed_at?: string | null }
): {
  id: number;
  description: string;
  due_date: string | null;
  group_id: string | null;
  display_code: number | null;
  completed: number;
  completed_at: string | null;
} {
  return {
    id: Number(row.id),
    description: String(row.description || ''),
    due_date: row.due_date ? String(row.due_date) : null,
    group_id: row.group_id ? String(row.group_id) : null,
    display_code: row.display_code != null ? Number(row.display_code) : null,
    completed: Number(row.completed || 0),
    completed_at: row.completed_at ? String(row.completed_at) : null
  };
}
