package com.example.schoolmanagementservice;

import io.teaql.core.UserContext;
import java.time.LocalDate;

/** Application-owned fixture, called by the generated console's composition root. */
public final class SchoolCheck {
    public static void run(String phase, UserContext context) {
        if (!java.util.List.of("bootstrap", "create", "read").contains(phase))
            throw new IllegalArgumentException("expected bootstrap, create or read");
        if (phase.equals("create")) {
            var roots = Q.platformsWithMinimalFields().withIdIs(1L).limit(2)
                .comment("Load the School fixture root").purpose("Verify School persistence")
                .executeForList(context);
            if (roots.size() != 1) throw new IllegalStateException("Missing domain root");
            var school = Q.schools().comment("Initialize Riverside Primary School")
                .purpose("Verify School persistence").newEntity(context);
            school.updatePlatform(roots.get(0));
            school.updateSchoolTypeToPrimary();
            school.updateName("Riverside Primary School");
            school.updateAddress("12 River Road, Springfield");
            school.updateEstablishedDate(LocalDate.of(1995, 9, 1));
            school.updateStudentCapacity(800);
            school.updateActive(true);
            school.auditAs("Create the documented School fixture").save(context);
        }

        var rows = Q.schoolsWithMinimalFields().selectName()
            .withNameIs("Riverside Primary School").orderByIdAscending().limit(2)
            .comment("Read the persisted School fixture").purpose("Verify School persistence")
            .executeForList(context);
        if (rows.size() != (phase.equals("bootstrap") ? 0 : 1))
            throw new IllegalStateException("Unexpected School row count");
        System.out.println("SCHOOL_OK phase=" + phase);
    }
}
