#!/usr/bin/env ruby
# frozen_string_literal: true

# csv2keila — Convert a flat CSV to a Keila-importable contact file
#
# Collects any non-standard columns back into Keila's "data" JSON column.
# Standard Keila fields (email, first_name, last_name, external_id, tags)
# are passed through as-is. Empty cells are omitted from the JSON.
#
# Usage:
#   csv2keila <flat_contacts.csv> <keila_import.csv>

require_relative "lib/keila_csv_lib"

if ARGV.length != 2
  warn "Usage: #{File.basename($PROGRAM_NAME)} <flat_contacts.csv> <keila_import.csv>"
  exit 1
end

input_path, output_path = ARGV

unless File.exist?(input_path)
  warn "Error: Input file not found: #{input_path}"
  exit 1
end

result = KeilaCsv.csv_to_keila(input_path, output_path)

fields_info = result[:custom_fields].empty? \
  ? "no custom fields found" \
  : "packed fields: #{result[:custom_fields].join(', ')}"

puts "Converted #{result[:contacts]} contact(s) — #{fields_info}"
puts "Written to: #{output_path}"
