# How Auto-Generate Schedule Works

A plain-language guide to what the system does when you press **Generate Schedule**, the rules it follows, and what to expect afterward.

---

## 1. What it does, in one paragraph

Auto-Generate looks at the sections you pick, works out which subjects still need class time, and places those subjects into the empty spots of each section's weekly timetable. It picks a teacher and a room for each one. It only ever makes placements that you could also have made by hand. It never creates a double-booking. If a subject cannot be placed without causing a conflict, the system leaves it empty and tells you, instead of forcing it in.

---

## 2. Before you start

- A **School Year** and a **Term** must be active in Admin Settings. Without them the Generate button is disabled.
- Each section needs subjects assigned to its **Track** for its grade level.
- Each subject needs at least one **active teacher** assigned to it.
- The **Readiness** panel on the Scheduling page gives you an early warning:
  - **Subject Coverage** – flags any subject that has no qualified teacher.
  - **Specialized Rooms** – flags when labs and other special rooms cannot cover the hours the subjects need.
  - **Grid Capacity** – tries to fit every subject assigned to each section into its timetable with no teacher conflict. A section that is **blocked** cannot be completed: a subject has no qualified teacher, its teachers are all busy elsewhere at the open times, or the timetable is simply too small. The report lists which subject is short and why.

---

## 3. The flow, step by step

1. **Choose what to generate.** Tick one or more year levels, then tick the sections you want. You can press **View** on a section to see each of its subjects and whether they are ready.
2. **The system checks each section.** A subject is marked:
   - **Teacher available** – it can be placed.
   - **No free slot** – it has teachers, but all of them are busy at every open time.
   - **No teacher** – nobody is assigned to teach it.
   - **Complete** – it already has all the hours it needs.

   A section with nothing placeable is **skipped**. The other sections still generate.
3. **The system works out what is missing.** For every section, it lists each subject that is still short of its weekly hours.
4. **The hardest subjects go first.** Subjects with the fewest available teachers are placed first, then subjects in sections with the fewest open time slots. This keeps the scarce teachers and time slots from being used up on easy placements.
5. **Each subject is placed one period at a time**, following the rules below, until it has its full weekly hours or nothing more can be placed.
6. **A summary is shown**: how many entries were placed, how many sections are now Complete, and which subjects still need attention.

The whole run is **all-or-nothing**. If something goes wrong partway, nothing is saved.

---

## 4. The placement rules

### What is never touched
- **Existing entries are never changed or removed** during a normal run. Auto-Generate only fills empty spots.
- A subject that is already **Complete** or **Exceeded** is left alone.
- **Breaks** are never used, and neither are very short time slots (15 minutes or less).
- Only a **completely empty** cell is used. It never squeezes a subject into a cell that already has something in it.
- A section only uses the time slots of **its own shift**.

### Which subjects count
A section's subjects are the active subjects that:
- match the section's **grade level**, and
- are assigned to the section's **Track** for the current term.

Activity-type subjects are not scheduled by Auto-Generate.

If a section's Track has no subjects assigned for its grade level and term, the section has no required subjects. Auto-Generate skips it. Assign subjects to the Track in Academic Setup first.

### Days and times
- **Same time every day.** A subject prefers to meet at the same time each day. For example, Monday 8:00, Tuesday 8:00, Wednesday 8:00, and so on. This is how a real school day runs.
- **Monday to Thursday first, Friday last.** Friday is only used once the other days are used up or blocked.
- **One period of a subject per day, when possible.** Days where the section does not yet have the subject are used before days where it does. A second period on the same day is a last resort.
- **A time row belongs to one subject.** Once a section has put a subject in a time slot, that slot is not handed to a different subject as a new row. The exception is when a subject cannot find a same-time run anywhere. Then it may use any empty cell, because a scattered period is better than a missing one.
- **Bigger time slots are used first.**
- **The last period is trimmed.** If a subject only needs 1 hour more and the slot is 1.5 hours, it takes 1 hour. The remaining time stays open for you to use.

### Teachers
- Only **active teachers assigned to that subject** can be chosen.
- **One teacher per subject per section.** The first teacher chosen is locked in and teaches every period of that subject for that section. The system never splits a subject between two teachers. If the locked-in teacher is busy at a particular time, that time is skipped. It does not switch to another teacher.
- **No double-booking.** A teacher cannot be in two places at overlapping times, in any section.
- **Choosing the first teacher.** Among the free teachers, the system prefers:
  1. a teacher from the **same grade level** as the subject, then
  2. the teacher with the **lightest current load**, so work is spread across staff.
- **There is no weekly load limit.** A teacher is only unavailable when they have an actual time conflict.

### Rooms
- **Regular subjects** use the section's own home room. If that room is somehow busy, the entry is placed without a room.
- **Specialized subjects** (labs and so on) need a free, active, non-general room. The first free one (alphabetically) is used. If none is free at that time, the subject is not placed at that time.
- **A room cannot be double-booked** at overlapping times.

### Conflicts
The system runs the **same checks as manual scheduling**: teacher assigned to the subject, teacher conflict, room conflict, and cell availability. Nothing the generator creates would be rejected if you had entered it by hand.

---

## 5. What you get afterward

- **Placed entries** appear in the timetable like any other entry and can be edited, moved, copied, or deleted as usual.
- **Sections marked Complete** have every subject at its weekly hours.
- **Leftovers.** If a subject could not be fully placed, for example because its teacher is fully booked or no lab is free, the missing hours are reported. Those subjects need manual attention.
- **A "Last generated" notice** shows what happened, with a **Redo Generation** button.

---

## 6. Redo, regenerate, undo, and remove

| Action | What it does | What is kept |
|---|---|---|
| **Redo Generation** | Removes what the last run created, then runs the same selection again. | Anything you edited by hand since the run. |
| **Generate on a section marked "Generated"** | Pressing **Continue** opens a review step in the same window, listing each already-generated section with its scheduled subjects. You choose per section (see below). | Depends on the choice. |
| **Remove Schedules** | Erases **every** entry (generated **and** manual) for a chosen year level or section. This cannot be undone. | Nothing in that selection. |

The review step offers three choices for each already-generated section:

- **Skip** – the section is left exactly as it is.
- **Lock & fill remaining** – every existing entry stays locked in place and only the missing subjects or hours are generated around them. Locked entries still count for teacher, room and weekly-hour checks. This is the default for a section that still has gaps.
- **Regenerate all** – the section's auto-generated entries are cleared and generated fresh. Entries you added or edited by hand are kept.

A section that is already complete defaults to Skip and cannot be filled. The clear and the new generation happen together, so if the run fails nothing is lost.

Once you edit an auto-generated entry by hand, the system treats it as yours. Redo and Regenerate will not remove it.

---

## 7. Good to know

- Auto-Generate is **"best effort"**. It places things one at a time in a sensible order but does not rearrange existing entries to make room. A tightly packed school may still end up with a few gaps to fix by hand.
- Results can differ between runs if teachers, rooms, or existing entries changed in between.
- The generator never breaks a rule to make a schedule "complete". A gap is always preferred over a conflict.
- Generating for one year level or a few sections does not affect the others.
