import java.util.List;

/** Pipeline CI/CD mini: tahapan berurutan dengan perilaku fail-fast. */
public class PipaCi {

    /** Satu pekerjaan dalam satu tahap: nama dan status kelulusannya. */
    record Pekerjaan(String nama, boolean lulus) {}

    /** Satu tahap pipeline memuat beberapa pekerjaan yang boleh berjalan serentak. */
    record Tahap(String nama, List<Pekerjaan> pekerjaan) {}

    /** Jalankan pipeline: tahap berhenti begitu ada pekerjaan yang gagal,
     *  tahap yang belum sempat dieksekusi ditandai DILEWATI. */
    static String jalankan(String judul, List<Tahap> pipeline) {
        System.out.println("== " + judul + " ==");
        String gagalDi = null;
        for (Tahap t : pipeline) {
            if (gagalDi != null) {
                for (Pekerjaan p : t.pekerjaan()) {
                    System.out.printf("  [%-9s] %-26s %s%n", t.nama(), p.nama(), "DILEWATI");
                }
                continue;
            }
            for (Pekerjaan p : t.pekerjaan()) {
                String status = p.lulus() ? "LULUS" : "GAGAL";
                System.out.printf("  [%-9s] %-26s %s%n", t.nama(), p.nama(), status);
                if (!p.lulus() && gagalDi == null) {
                    gagalDi = t.nama();
                }
            }
        }
        String akhir = (gagalDi == null) ? "SUCCEEDED" : "FAILED";
        System.out.println("  status akhir: " + akhir
                + (gagalDi == null ? " (artefak dipublikasikan)" : " (gagal di tahap " + gagalDi + ")"));
        System.out.println();
        return akhir;
    }

    public static void main(String[] args) {
        List<Tahap> hijau = List.of(
                new Tahap("compile", List.of(new Pekerjaan("maven-compile", true))),
                new Tahap("test", List.of(
                        new Pekerjaan("unit-test", true),
                        new Pekerjaan("integration-test", true))),
                new Tahap("package", List.of(new Pekerjaan("maven-package", true))),
                new Tahap("publish", List.of(new Pekerjaan("push-image", true))));
        jalankan("Pipeline 1: seluruh pengujian lulus", hijau);

        List<Tahap> merah = List.of(
                new Tahap("compile", List.of(new Pekerjaan("maven-compile", true))),
                new Tahap("test", List.of(
                        new Pekerjaan("unit-test", true),
                        new Pekerjaan("integration-test", false))),
                new Tahap("package", List.of(new Pekerjaan("maven-package", true))),
                new Tahap("publish", List.of(new Pekerjaan("push-image", true))));
        jalankan("Pipeline 2: integration-test gagal", merah);
    }
}
