#ifndef PROJECTDAO_H
#define PROJECTDAO_H

#include <QObject>
#include <QString>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QVector>
#include <QPair>
#include <QDate>

struct Project {
    int id = 0;
    QString code;
    QString title;
    QString customerCode;
    QString customerName;
    QDate startDate;
    QDate duration;
    QString state;
    QString contact;
    QString address1;
    QString address2;
    QString postcode;
    QString city;
    QString phone;
    QString mobile;
    QString email;
};

struct ProjectDocument {
    int id = 0;
    QString projectCode;
    QString docType; // PR, FI, FC, OT
    QString docCode;
};

struct ProjectBudget {
    int id = 0;
    QString projectCode;
    QString concept;
    double amount = 0.0;
};

class ProjectDAO : public QObject
{
    Q_OBJECT

public:
    explicit ProjectDAO(QObject *parent = nullptr) : QObject(parent) {}

    // Project CRUD
    static bool create(Project &proj);
    static bool update(const Project &proj);
    static bool remove(const QString &code);
    static Project getByCode(const QString &code);
    static Project getById(int id);
    static QVector<Project> getAll();
    static QStringList getAllCodes();
    static bool exists(const QString &code);

    // Project status management
    static bool setState(const QString &code, const QString &state);
    static QStringList getStates();

    // Project documents
    static bool attachDocument(const QString &projectCode, const QString &docType, const QString &docCode);
    static bool removeDocument(int docId);
    static QVector<ProjectDocument> getDocuments(const QString &projectCode);

    // Project budget
    static bool addBudgetLine(const QString &projectCode, const QString &concept, double amount);
    static bool removeBudgetLine(int budgetId);
    static QVector<ProjectBudget> getBudget(const QString &projectCode);
    static double getTotalBudget(const QString &projectCode);

    // Financial summary
    static double getTotalCost(const QString &projectCode);
    static double getTotalInvoiced(const QString &projectCode);
    static QPair<double, double> getFinancialSummary(const QString &projectCode);
};

#endif // PROJECTDAO_H