import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class RepositoryDemo {
    public static void main(String[] args) throws IOException {
        Path file = Path.of("build", "demo", "canciones.csv");
        Files.createDirectories(file.getParent());
        SongRepository repo = new LoggingSongRepository(new CsvSongRepository(file),
                msg -> IO.println("[log] " + msg));

        List<Song> songs = List.of(
                new Song("Contigo Perú", "Arturo \"Zambo\" Cavero", 254),
                new Song("Cariñito", "Los Hijos del Sol", 187),
                new Song("Lima, de noche", "Los Ensayos", 205));
        repo.save(songs);
        IO.print(Files.readString(file));

        List<Song> back = repo.load();
        IO.println("¿iguales? " + back.equals(songs));

        // Otro archivo, con una línea mala.
        Path bad = file.resolveSibling("malo.csv");
        Files.writeString(bad, "title,artist,seconds\nValicha,Miguel Ángel Hurtado,192\nHuayno,Anónimo,tres\n");
        try {
            new LoggingSongRepository(new CsvSongRepository(bad), msg -> IO.println("[log] " + msg)).load();
        } catch (MalformedCsvException e) {
            IO.println("no se pudo cargar: " + e.getMessage() + " (e.line() = " + e.line() + ")");
        }
    }
}
