function parseJsonValue(value, fallback = []) { if (value === null || value === undefined) { return fallback; } if (Array.isArray(value) || typeof value === 'object') { return value; } try { return JSON.parse(value); } catch (_error) { return fallback; } } function titleCase(value) { const rawValue = String(value || '').trim(); if (!rawValue) { return rawValue; } return rawValue.charAt(0).toUpperCase() + rawValue.slice(1).toLowerCase(); } function formatDateForFlutter(dateValue) { const rawValue = String(dateValue || '').trim(); if (/^\d{4}-\d{2}-\d{2}$/.test(rawValue)) { const [year, month, day] = rawValue.split('-'); return `${day}/${month}/${year}`; } return rawValue; } function mapDoctor(row) { return { id: row.id, name: row.name, department: row.department, designation: row.designation, degrees: row.degrees, profileImageUrl: row.profile_image_url || null, roomNumber: row.room_number, consultationFee: Number(row.consultation_fee), consultationDays: parseJsonValue(row.consultation_days), consultationTimes: row.consultation_times, isActive: Boolean(row.is_active), createdAt: row.created_at, updatedAt: row.updated_at, }; } function mapAdministrator(row) { return { id: row.id, name: row.name, email: row.email, profileImageUrl: row.profile_image_url || null, isActive: Boolean(row.is_active), createdAt: row.created_at, updatedAt: row.updated_at, }; } function mapPatient(row) { return { phoneNumber: row.phone_number, email: row.email, name: row.name, profileImageUrl: row.profile_image_url || null, dateOfBirth: formatDateForFlutter(row.date_of_birth), address: row.address, gender: titleCase(row.gender), createdAt: row.created_at, updatedAt: row.updated_at, }; } function mapAppointment(row) { return { id: Number(row.id), doctorId: row.doctor_id, doctorName: row.doctor_name, doctorProfileImageUrl: row.doctor_profile_image_url || null, doctorDepartment: row.doctor_department, doctorDesignation: row.doctor_designation, doctorDegrees: row.doctor_degrees, patientPhone: row.patient_phone, patientName: row.patient_name, patientProfileImageUrl: row.patient_profile_image_url || null, date: row.appointment_date, timeSlot: row.time_slot, serialNumber: Number(row.serial_number), status: titleCase(row.status), notes: row.notes, createdAt: row.created_at, updatedAt: row.updated_at, rescheduleRequest: row.reschedule_request_id ? { id: Number(row.reschedule_request_id), requestedDate: row.reschedule_requested_date, requestedTimeSlot: row.reschedule_requested_time_slot, reason: row.reschedule_reason || null, status: row.reschedule_status, } : null, startedAt: row.started_at || null, endedAt: row.ended_at || null, extraMinutes: Number(row.extra_minutes || 0), }; } function mapPrescription(row, diagnosis = [], medicines = [], tests = [], reminders = []) { return { id: row.id, appointmentId: row.appointment_id ? Number(row.appointment_id) : null, doctorId: row.doctor_id, doctorName: row.doctor_name, doctorProfileImageUrl: row.doctor_profile_image_url || null, doctorDepartment: row.doctor_department, doctorDesignation: row.doctor_designation, doctorDegrees: row.doctor_degrees, patientPhone: row.patient_phone, patientName: row.patient_name, patientProfileImageUrl: row.patient_profile_image_url || null, appointmentDate: row.appointment_date, issuedAt: row.issued_at, diagnosis, medicines, tests, reminders, additionalNotes: row.additional_notes, pdfPath: row.pdf_path, createdAt: row.created_at, }; } module.exports = { mapAdministrator, mapDoctor, mapPatient, mapAppointment, mapPrescription, parseJsonValue, titleCase, };