1.  **Database Schema Update:**
    *   Create a new table `sub_groups` to store sub-group information:
        ```sql
        CREATE TABLE sub_groups (
            sub_group_id INT AUTO_INCREMENT PRIMARY KEY,
            club_id INT NOT NULL,
            name VARCHAR(255) NOT NULL,
            description TEXT,
            category VARCHAR(255),
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (club_id) REFERENCES clubs(club_id) -- Assuming a clubs table exists
        );
        ```
    *   Create a junction table `sub_group_members` to manage the many-to-many relationship between athletes and sub-groups:
        ```sql
        CREATE TABLE sub_group_members (
            sub_group_member_id INT AUTO_INCREMENT PRIMARY KEY,
            athlete_id INT NOT NULL,
            sub_group_id INT NOT NULL,
            FOREIGN KEY (athlete_id) REFERENCES athletes(athlete_id), -- Assuming an athletes table exists
            FOREIGN KEY (sub_group_id) REFERENCES sub_groups(sub_group_id),
            UNIQUE KEY unique_athlete_sub_group (athlete_id, sub_group_id) -- Prevent duplicate assignments
        );
        ```
    *   Add a `sub_group_id` column to the `timetable` table (if sessions can be linked to sub-groups). If not, create a junction table between `timetable` and `sub_groups`:

        ```sql
        --Option 1: Add directly to timetable table
        ALTER TABLE timetable ADD COLUMN sub_group_id INT NULL;
        ALTER TABLE timetable ADD FOREIGN KEY (sub_group_id) REFERENCES sub_groups(sub_group_id);

        --Option 2: Create junction table between timetable and sub_groups
        CREATE TABLE timetable_sub_groups (
            timetable_sub_group_id INT AUTO_INCREMENT PRIMARY KEY,
            timetable_id INT NOT NULL,
            sub_group_id INT NOT NULL,
            FOREIGN KEY (timetable_id) REFERENCES timetable(timetable_id),
            FOREIGN KEY (sub_group_id) REFERENCES sub_groups(sub_group_id),
            UNIQUE KEY unique_timetable_sub_group (timetable_id, sub_group_id)
        );
        ```

2.  **admin.php Modifications:**
    *   **Sub-group Management UI:** Create a new section in `admin.php` for managing sub-groups.  This should include:
        *   A form to create new sub-groups (name, description, category).
        *   A list of existing sub-groups with options to edit or delete them.
    *   **Athlete Assignment UI:**  In the athlete management section, add a multi-select dropdown or similar UI element to allow assigning athletes to multiple sub-groups. Use AJAX to update the `sub_group_members` table when assignments are changed.

3.  **coachs-site.php Modifications:**
    *   Display sub-groups:  Modify the coach's site to display the sub-groups that each athlete belongs to. This could be a simple list of sub-group names next to the athlete's name.
    *   Session filtering: Add a filter to the timetable on `coachs-site.php` to allow coaches to view only sessions associated with a specific sub-group.

4.  **timetable.php Modifications:**
    *   Display Sub-group association: If a session is associated with a sub-group, display this information on the timetable.
    *   Modify Find POD logic: If a session is associated with a sub-group, filter the Find POD results to include only athletes who are members of that sub-group. This will require retrieving the session's `sub_group_id` (or `timetable_sub_groups`) and querying the `sub_group_members` table.

5.  **dashboard.php Modifications:**
    *   Display Sub-groups: Display the sub-groups an athlete belongs to on their dashboard.

6.  **API adjustments**
    *   Update endpoints: Modify API endpoints related to athlete and timetable data to include sub-group information where relevant. Ensure data structures sent to and from API account for the addition of sub-groups.

7.  **CSV Upload/Export Modifications:**
    *   Update CSV upload logic to handle a new "Sub-groups" column. The column should contain a comma-separated list of sub-group names or IDs. The upload process should create the sub-groups if they don't exist and add the athletes to the specified sub-groups.
    *   Update CSV export logic to include a "Sub-groups" column containing a comma-separated list of sub-group names or IDs for each athlete.

8.  **Automated Sub-group Creation:**
    *   Modify the code that creates new competitions to automatically create a corresponding sub-group with the same name as the competition. The category could be set to "Competition" or similar.

9. **Update How-To-Use Landing Page:**
    *   Edit the `index.php` How-To-Use page to clearly explain the difference between main groups and sub-groups and how they are used in the system.

10. **Validation and Error Handling:**
    *   Implement server-side validation in `admin.php` when assigning athletes to sub-groups:
        *   Check that the `sub_group_id` exists in the `sub_groups` table.
        *   Prevent duplicate assignments by checking if the athlete is already a member of the sub-group.
        *   Handle the case where a sub-group is deleted but still referenced in a timetable entry gracefully (e.g., display an error message or ignore the sub-group).

11. **Code Examples:**

    *   **PHP (admin.php - Adding an athlete to a sub-group):**
        ```php
        // Assuming $athlete_id and $sub_group_ids are obtained from the form
        foreach ($sub_group_ids as $sub_group_id) {
            // Validate sub_group_id
            $sql = "SELECT sub_group_id FROM sub_groups WHERE sub_group_id = ?";
            $stmt = $pdo->prepare($sql); // Assuming $pdo is your database connection
            $stmt->execute([$sub_group_id]);
            $sub_group = $stmt->fetch();

            if (!$sub_group) {
                echo "Error: Invalid sub-group ID: " . $sub_group_id;
                continue;
            }

            // Check for duplicate assignment
            $sql = "SELECT sub_group_member_id FROM sub_group_members WHERE athlete_id = ? AND sub_group_id = ?";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$athlete_id, $sub_group_id]);
            $existing_assignment = $stmt->fetch();

            if ($existing_assignment) {
                echo "Error: Athlete already assigned to sub-group: " . $sub_group_id;
                continue;
            }

            // Add to sub_group_members
            $sql = "INSERT INTO sub_group_members (athlete_id, sub_group_id) VALUES (?, ?)";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([$athlete_id, $sub_group_id]);
        }
        ```

    *   **PHP (timetable.php - Modifying Find POD logic):**
        ```php
        // Assuming $session_id is the ID of the current session
        // Get the sub_group_id for the session
        $sql = "SELECT sub_group_id FROM timetable WHERE timetable_id = ?";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$session_id]);
        $session = $stmt->fetch();

        if ($session && $session['sub_group_id']) {
            $sub_group_id = $session['sub_group_id'];

            // Modify the SQL query to filter by sub-group
            $sql = "SELECT a.* FROM athletes a
                    INNER JOIN sub_group_members sgm ON a.athlete_id = sgm.athlete_id
                    WHERE sgm.sub_group_id = ?";

            $stmt = $pdo->prepare($sql);
            $stmt->execute([$sub_group_id]);
            $eligible_athletes = $stmt->fetchAll();
        } else {
            // If no sub-group is associated with the session, use the existing logic
            $sql = "SELECT * FROM athletes";
            $stmt = $pdo->prepare($sql);
            $stmt->execute();
            $eligible_athletes = $stmt->fetchAll();
        }
        ```

12. **Unit Tests:**
    *   Create unit tests to verify the following:
        *   Adding an athlete to 'Tuesday Gym' does not remove them from 'Main Group A'.
        *   Athletes can be successfully added to and removed from sub-groups.
        *   The Find POD logic correctly filters athletes based on sub-group membership.
        *   CSV upload and export correctly handle sub-group data.
