001package com.studentgui.tools;
002
003import java.sql.Connection;
004import java.sql.DriverManager;
005import java.sql.PreparedStatement;
006import java.sql.ResultSet;
007import java.util.List;
008
009import com.studentgui.apphelpers.Database;
010import com.studentgui.apphelpers.Helpers;
011
012/**
013 * Command-line inspection tool for viewing student database contents and schema statistics.
014 *
015 * <p>Provides a quick diagnostic view of database state without launching the GUI.
016 * Useful for:</p>
017 * <ul>
018 *   <li>Verifying student records exist in the database</li>
019 *   <li>Inspecting available progress types and their assessment part counts</li>
020 *   <li>Checking session data row sizes for debugging schema migrations</li>
021 *   <li>Quick manual data verification during development or troubleshooting</li>
022 * </ul>
023 *
024 * <p><b>Usage:</b></p>
025 * <pre>{@code
026 * # List all students and progress types with counts
027 * java -cp StudentDataGUI.jar com.studentgui.tools.QueryStudentData
028 *
029 * # Inspect specific student's progress types
030 * java -cp StudentDataGUI.jar com.studentgui.tools.QueryStudentData "Aaron A Aaronsson"
031 * }</pre>
032 *
033 * <p><b>Output Format:</b></p>
034 * <pre>
035 * Inspecting student: Aaron A Aaronsson
036 * ProgressType 'Braille' (id=1) parts=64 sessions=3
037 *  Sample row sizes: 64 values: [2, 3, 2, 3, 4, ...]
038 * ProgressType 'Abacus' (id=2) parts=22 sessions=1
039 *  Sample row sizes: 22 values: [0, 1, 2, 1, 3, ...]
040 * </pre>
041 *
042 * <p><b>Workflow:</b></p>
043 * <ol>
044 *   <li>Lists all known students via {@link Helpers#getStudents()}</li>
045 *   <li>Selects first student or uses command-line argument</li>
046 *   <li>Queries {@code ProgressType} table for all progress types</li>
047 *   <li>For each progress type: counts assessment parts and fetches sample session rows</li>
048 *   <li>Prints progress type name, ID, part count, session count, and sample row to stdout</li>
049 * </ol>
050 *
051 * @see com.studentgui.apphelpers.Database#fetchLatestAssessmentResults
052 * @see com.studentgui.apphelpers.Helpers#getStudents()
053 */
054public class QueryStudentData {
055    /**
056     * Command-line entry point. Prints progress types and a sample row for
057     * the specified or first-known student.
058     *
059     * @param args optional first argument is student display name
060     * @throws Exception on database errors
061     */
062    public static void main(final String[] args) throws Exception {
063        Helpers.createFolderHierarchy();
064        List<String> students = Helpers.getStudents();
065        String student = null;
066        if (args.length > 0) {
067            student = args[0];
068        }
069        if (student == null) {
070            System.out.println("Known students:");
071            for (String s : students) {
072                System.out.println(" - " + s);
073            }
074            if (!students.isEmpty()) {
075                student = students.get(0);
076            } else {
077                System.out.println("No students found in DB. Exiting.");
078                return;
079            }
080        }
081        System.out.println("Inspecting student: " + student);
082        // list progress types
083        try (Connection c = DriverManager.getConnection("jdbc:sqlite:" + Helpers.DATABASE_PATH.toString())) {
084            try (PreparedStatement ps = c.prepareStatement("SELECT id, name FROM ProgressType")) {
085                try (ResultSet rs = ps.executeQuery()) {
086                    while (rs.next()) {
087                        int ptId = rs.getInt("id");
088                        String ptName = rs.getString("name");
089                        // count parts
090                            int partCount = 0;
091                        try (PreparedStatement ps2 = c.prepareStatement("SELECT COUNT(*) FROM AssessmentPart WHERE progress_type_id = ?")) {
092                            ps2.setInt(1, ptId);
093                            try (ResultSet rs2 = ps2.executeQuery()) {
094                                if (rs2.next()) {
095                                    partCount = rs2.getInt(1);
096                                }
097                            }
098                        }
099                        List<List<Integer>> rows = Database.fetchLatestAssessmentResults(student, ptName, 5);
100                        System.out.println(String.format("ProgressType '%s' (id=%d) parts=%d sessions=%d", ptName, ptId, partCount, rows.size()));
101                        if (!rows.isEmpty()) {
102                            System.out.println(" Sample row sizes: " + rows.get(0).size() + " values: " + rows.get(0));
103                        }
104                    }
105                }
106            }
107        }
108    }
109    /**
110     * No-op public constructor to document this class as a small utility.
111     */
112    public QueryStudentData() {
113        // utility class; no state
114    }
115}