const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
    const Udks = sequelize.define(
        "udk",
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            creation: { type: DataTypes.DATE, allowNull: false },
            modified: { type: DataTypes.DATE, allowNull: false },
            modified_by: { type: DataTypes.INTEGER, allowNull: false },
            owner: { type: DataTypes.INTEGER, allowNull: false },
            enabled: { type: DataTypes.BOOLEAN, allowNull: false },

            name: {
                type: DataTypes.STRING(100),
                allowNull: false,
                unique: true,
            },
            date: { type: DataTypes.DATE, allowNull: false },
            customer: { type: DataTypes.STRING(100), allowNull: false },
            address: { type: DataTypes.STRING(100), allowNull: true },
            pic: { type: DataTypes.STRING(100), allowNull: true },
            email: { type: DataTypes.STRING(100), allowNull: true },
            phone_number: { type: DataTypes.STRING(100), allowNull: true },
            distribution: { type: DataTypes.STRING(100), allowNull: true },
            top: { type: DataTypes.INTEGER, allowNull: true },
            periode: { type: DataTypes.INTEGER, allowNull: true },
            is_tax: { type: DataTypes.BOOLEAN, allowNull: true },
            sub_total: { type: DataTypes.INTEGER, allowNull: true },
            ppn: { type: DataTypes.INTEGER, allowNull: true },
            price_total: { type: DataTypes.INTEGER, allowNull: true },
            price_condition: { type: DataTypes.TEXT, allowNull: true },
            payment: { type: DataTypes.TEXT, allowNull: true },
            validity_offer: { type: DataTypes.TEXT, allowNull: true },
            description: { type: DataTypes.TEXT, allowNull: true },
            shipping: { type: DataTypes.TEXT, allowNull: true },
            other_discount: { type: DataTypes.INTEGER, allowNull: true },
            other_price: { type: DataTypes.INTEGER, allowNull: true },
            note: { type: DataTypes.TEXT, allowNull: true },


            //   fk
            company_id: {
                type: DataTypes.INTEGER,
                allowNull: false,
                references: {
                    model: "companies",
                    key: "id",
                },
            },
        },
        {
            tableName: "udks",
            freezeTableName: true,
        }
    );

    /**
     * Associates the Customer List model with other models.
     * @param {object} models - The models to associate with.
     */
    Udks.associate = (models) => {
        /**
         * Associates the Udks model with the UdkProduct model.
         * A Udk has many UdkProducts.
         * @param {object} models - The models to associate with.
         */
        Udks.hasMany(models.UdkProduct, {
            foreignKey: "udk_id",
            as: "udkproducts",
            onDelete: "CASCADE",
        });

        /**
         * Associates the Udks model with the Company model.
         * A Udk belongs to a Company.
         * @param {object} models - The models to associate with.
         */
        Udks.belongsTo(models.Company, {
            foreignKey: "company_id",
            as: "company",
        });

        Udks.belongsTo(models.User, { foreignKey: 'owner', as: 'user' });

    };

    return Udks;
};
