import { DataTypes } from "sequelize";
import { sequelize } from "@/configs/sequelize";
import { User } from "./user";

export const Symptom = sequelize.define(
    "Symptom",
    {
        id: {
            type: DataTypes.UUID,
            primaryKey: true,
            defaultValue: DataTypes.UUIDV4,
        },
        name: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false,
        },
        index: {
            type: DataTypes.INTEGER,
            defaultValue: 0,
        },
        description: {
            type: DataTypes.STRING,
            defaultValue: "",
        },
        category: {
            type: DataTypes.ENUM(
                "danger",
                "respiratory",
                "digestive",
                "general",
                "skin",
                "specific",
            ),
            defaultValue: "general",
        },
        question: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        created_by_id: {
            type: DataTypes.UUID,
            allowNull: false,
        },
    },
    {
        tableName: "symptoms",
        timestamps: true,
        createdAt: "created_at",
        updatedAt: "updated_at",
    },
);

export const FollowUpQuestion = sequelize.define(
    "FollowUpQuestion",
    {
        id: {
            type: DataTypes.UUID,
            primaryKey: true,
            defaultValue: DataTypes.UUIDV4,
        },
        symptom_id: {
            type: DataTypes.UUID,
            allowNull: false,
        },
        question: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        type: {
            type: DataTypes.ENUM(
                "generic",
                "duration",
                "severity",
                "frequency",
                "yes/no",
            ),
            allowNull: false,
        },
        options: {
            type: DataTypes.JSON,
            defaultValue: [],
        },
    },
    {
        tableName: "follow_up_questions",
        timestamps: true,
        createdAt: "created_at",
        updatedAt: "updated_at",
    },
);

Symptom.hasMany(FollowUpQuestion, {
    foreignKey: "symptom_id",
    as: "follow_up_questions",
    onUpdate: "cascade",
    onDelete: "cascade",
});

Symptom.belongsTo(User, {
    foreignKey: "created_by_id",
    as: "created_by",
    onUpdate: "cascade",
    onDelete: "cascade",
});
