//
// Created by 34753 on 2025/6/20.
//

#ifndef STUDENTSERVICE_H
#define STUDENTSERVICE_H

#include <QObject>
#include <QSortFilterProxyModel>
#include "core/student.h"
#include "data/dataStorage.h"

class StudentService : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVariantList students READ students NOTIFY studentsChanged)
public:
    explicit StudentService(DataStorage *storage, QObject *parent = nullptr);

    Q_INVOKABLE void addStudent(Student *student);

    Q_INVOKABLE void updateStudent(const QString &id, Student *newInfo);

    Q_INVOKABLE void deleteStudent(const QString &id);

    Q_INVOKABLE Student *getStudentById(const QString &id);

    Q_INVOKABLE QVariantList getCollegeStats() const;

    Q_INVOKABLE QVariantList getHometownStats() const;

    Q_INVOKABLE void sortStudents(const QString &field);

    Q_INVOKABLE QList<Student *> searchStudents(const QString &field, const QString &value);

    Q_INVOKABLE QVariantMap countStatistics();

    Q_INVOKABLE QVariantList students() const;
    Q_INVOKABLE QList<Student*> loadStudents();

    // 统计属性
    int totalStudents() const { return m_totalStudents; }
    int collegeCount() const { return m_collegeCount; }
    int classCount() const { return m_classCount; }
    int maleCount() const { return m_maleCount; }
    int femaleCount() const { return m_femaleCount; }

    // 计算统计信息
    void calculateStatistics();

signals:
    void studentsChanged(); // 当学生列表变化时发出信号
    void statsUpdated();    // 当统计数据更新时发出信号

private:
    DataStorage *m_storage;
    QList<Student *> m_students;

    // 统计信息
    int m_totalStudents = 0;
    int m_collegeCount = 0;
    int m_classCount = 0;
    int m_maleCount = 0;
    int m_femaleCount = 0;
};

#endif //STUDENTSERVICE_H