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

# keila2csv — Convert a Keila contact export to a flat CSV
#
# Expands the "data" JSON column into individual columns, one per custom field.
# The resulting file is easy to open in a spreadsheet or process with other tools.
#
# Usage:
#   keila2csv <keila_export.csv> <output.csv>

require_relative "lib/keila_csv_lib"

if ARGV.length != 2
  warn "Usage: #{File.basename($PROGRAM_NAME)} <keila_export.csv> <output.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.keila_to_csv(input_path, output_path)

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

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