use serde::{Deserialize, Serialize};
use ozra::types::{DataSet, FieldValue};
use crate::ApplicationError;
use crate::RusaintError;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct LectureSyllabus {
pub course_name: String,
pub professor: String,
pub course_code: String,
pub year: String,
pub semester: String,
pub credits: String,
pub abstract_text: String,
pub teaching_method: String,
pub main_textbook: String,
pub sub_textbook: String,
pub professor_phone: String,
pub professor_email: String,
pub office_hours: String,
pub target_students: String,
pub designation: String,
pub absence_policy: String,
pub grading_items: Vec<SyllabusGradingItem>,
pub learning_objectives: Vec<String>,
pub weekly_schedule: Vec<SyllabusWeeklyPlan>,
pub competencies: Vec<SyllabusCompetency>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct SyllabusGradingItem {
pub name: String,
pub rate: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct SyllabusWeeklyPlan {
pub week: String,
pub topic: String,
pub details: String,
pub teaching_method: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct SyllabusCompetency {
pub name: String,
pub rate: String,
}
fn get_string_field(row: &[(String, FieldValue)], field_name: &str) -> String {
row.iter()
.find(|(name, _)| name == field_name)
.map(|(_, val)| val.to_string_repr())
.unwrap_or_default()
}
fn find_dataset<'a>(datasets: &'a [DataSet], name: &str) -> &'a [Vec<(String, FieldValue)>] {
datasets
.iter()
.find(|(n, _)| n == name)
.map(|(_, rows)| rows.as_slice())
.unwrap_or(&[])
}
impl LectureSyllabus {
pub fn from_datasets(datasets: &[DataSet]) -> Result<Self, RusaintError> {
let plan_rows = find_dataset(datasets, "ET_PLAN");
let plan = plan_rows.first().ok_or_else(|| {
ApplicationError::OzDataFetchError(
"ET_PLAN dataset is empty or missing in OZ response".to_string(),
)
})?;
let course_name = get_string_field(plan, "SMTEXT");
let professor = get_string_field(plan, "PROF_NM");
let course_code = get_string_field(plan, "SMOBJID");
let year = get_string_field(plan, "PERYR");
let semester = get_string_field(plan, "PERID");
let credits = get_string_field(plan, "PTPLAN");
let abstract_text = get_string_field(plan, "ABSTRACT");
let teaching_method = get_string_field(plan, "CLSWY_TEXT");
let main_textbook = get_string_field(plan, "TXTREFER_M");
let sub_textbook = get_string_field(plan, "TXTREFER_S");
let professor_phone = get_string_field(plan, "PROF_TELNR");
let professor_email = get_string_field(plan, "SMTPADR");
let office_hours = get_string_field(plan, "COUNSELTM");
let target_students = get_string_field(plan, "BOOK_TARGET");
let designation = get_string_field(plan, "DESIGNATION");
let absence_policy = get_string_field(plan, "SMABSENT");
let grading_items: Vec<SyllabusGradingItem> = find_dataset(datasets, "ET_APP")
.iter()
.map(|row| SyllabusGradingItem {
name: get_string_field(row, "AGRDESC"),
rate: get_string_field(row, "RATE"),
})
.collect();
let learning_objectives: Vec<String> = find_dataset(datasets, "ET_GOAL")
.iter()
.map(|row| get_string_field(row, "ZGOAL"))
.collect();
let weekly_schedule: Vec<SyllabusWeeklyPlan> = find_dataset(datasets, "ET_WEEK")
.iter()
.map(|row| SyllabusWeeklyPlan {
week: get_string_field(row, "WEEKLY"),
topic: get_string_field(row, "COREWORD"),
details: get_string_field(row, "DETAILS"),
teaching_method: get_string_field(row, "REMARKT"),
})
.collect();
let competencies: Vec<SyllabusCompetency> = find_dataset(datasets, "ET_PRO_ABLI")
.iter()
.map(|row| SyllabusCompetency {
name: get_string_field(row, "PRO_ABLIT"),
rate: get_string_field(row, "REL_RATE"),
})
.collect();
Ok(Self {
course_name,
professor,
course_code,
year,
semester,
credits,
abstract_text,
teaching_method,
main_textbook,
sub_textbook,
professor_phone,
professor_email,
office_hours,
target_students,
designation,
absence_policy,
grading_items,
learning_objectives,
weekly_schedule,
competencies,
})
}
}