CREATE TABLE classroom_compliance_class (
	id BIGSERIAL PRIMARY KEY,
	number INT NOT NULL
		CONSTRAINT class_number_check CHECK (number > 0),
	school_year VARCHAR(9) NOT NULL,
	user_id BIGINT NOT NULL
		REFERENCES auth_user(id)
			ON UPDATE CASCADE ON DELETE CASCADE,
	score_expression VARCHAR(255) NOT NULL DEFAULT '0.5 * classroom_readiness + 0.5 * behavior',
	score_period VARCHAR(64) NOT NULL DEFAULT 'week',
	archived BOOLEAN NOT NULL DEFAULT FALSE,
	CONSTRAINT unique_class_numbers_per_school_year
		UNIQUE(number, school_year, user_id)
);

CREATE TABLE classroom_compliance_student (
	id BIGSERIAL PRIMARY KEY,
	name VARCHAR(255) NOT NULL,
	class_id BIGINT NOT NULL
		REFERENCES classroom_compliance_class(id)
			ON UPDATE CASCADE ON DELETE CASCADE,
	desk_number INT NOT NULL DEFAULT 0,
	removed BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE classroom_compliance_student_label (
	student_id BIGINT NOT NULL
		REFERENCES classroom_compliance_student(id)
			ON UPDATE CASCADE ON DELETE CASCADE,
	label VARCHAR(255) NOT NULL,
	PRIMARY KEY (student_id, label)
);

CREATE TABLE classroom_compliance_entry (
	class_id BIGINT NOT NULL
		REFERENCES classroom_compliance_class(id)
			ON UPDATE CASCADE ON DELETE CASCADE,
	student_id BIGINT NOT NULL
		REFERENCES classroom_compliance_student(id)
			ON UPDATE CASCADE ON DELETE CASCADE,
	date DATE NOT NULL,
	created_at BIGINT NOT NULL
		DEFAULT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000,
	absent BOOLEAN NOT NULL DEFAULT FALSE,
	comment VARCHAR(2000) NOT NULL DEFAULT '',
	PRIMARY KEY (class_id, student_id, date)
);

CREATE TABLE classroom_compliance_entry_checklist_item (
	class_id BIGINT NOT NULL,
	student_id BIGINT NOT NULL,
	date DATE NOT NULL,
	item VARCHAR(2000) NOT NULL,
	checked BOOLEAN NOT NULL DEFAULT FALSE,
	category VARCHAR(255) NOT NULL,
	PRIMARY KEY (class_id, student_id, date, item, category),
	FOREIGN KEY (class_id, student_id, date)
		REFERENCES classroom_compliance_entry(class_id, student_id, date)
			ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE classroom_compliance_class_note (
	id BIGSERIAL PRIMARY KEY,
	class_id BIGINT NOT NULL
		REFERENCES classroom_compliance_class(id)
			ON UPDATE CASCADE ON DELETE CASCADE,
	created_at BIGINT NOT NULL
		DEFAULT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000,
	content VARCHAR(2000) NOT NULL
);
