// models/MacPolicy.js
import { DataTypes } from 'sequelize';

export const registerModels = (sequelize) => {
    const Group = sequelize.define('Group', {
        id: {
            type: DataTypes.INTEGER(11),
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(64),
            allowNull: false,
            unique: true
        },
        gidnumber: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            unique: true
        }
    }, {
        tableName: 'ldapgroups',
        timestamps: false,
        hooks: {
            afterCreate: (instance, options) => {
                console.log(`✓ saved Group: ${instance.name}`);
            },
            afterUpdate: (instance, options) => {
                console.log(`✓ updated Group: ${instance.name}`);
            },
            afterDestroy: (instance, options) => {
                console.log(`✗ deleted Group: ${instance.name}`);
            }
        }
    });

    const User = sequelize.define('User', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(64),
            allowNull: false,
            unique: true
        },
        uidnumber: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        primarygroup: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        othergroups: {
            type: DataTypes.STRING(1024),
            allowNull: true,
            defaultValue: ''
        },
        givenname: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        sn: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        mail: {
            type: DataTypes.STRING(254),
            allowNull: true,
            defaultValue: ''
        },
        loginshell: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        homedirectory: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        disabled: {
            type: DataTypes.SMALLINT,
            allowNull: true,
            defaultValue: 0
        },
        passsha256: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        passbcrypt: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        otpsecret: {
            type: DataTypes.STRING(64),
            allowNull: true,
            defaultValue: ''
        },
        yubikey: {
            type: DataTypes.STRING(128),
            allowNull: true,
            defaultValue: ''
        },
        sshkeys: {
            type: DataTypes.TEXT,
            allowNull: true,
            defaultValue: ''
        },
        custattr: {
            type: DataTypes.TEXT,
            allowNull: true,
            defaultValue: '{}',
            get() {
                try {
                    return JSON.parse(this.getDataValue('custattr'));
                } catch (e) {
                    return {}; // Fallback, falls DB-Daten fehlerhaft sind
                }
            },
            set(value) {
                this.setDataValue('custattr', JSON.stringify(value ?? {}));
            }
        },
    }, {
        tableName: 'users',
        timestamps: false,
        hooks: {
            afterCreate: (instance, options) => {
                console.log(`✓ saved User: ${instance.name}`);
            },
            afterUpdate: (instance, options) => {
                console.log(`✓ updated User: ${instance.name}`);
            },
            afterDestroy: (instance, options) => {
                console.log(`✗ deleted User: ${instance.name}`);
            }
        }
    });

    // Group.hasMany(User, {
    //     targetKey: 'uidnumber',
    //     foreignKey: 'primarygroup',
    //     as: 'users'
    // });

    User.belongsTo(Group, {
        targetKey: 'gidnumber',
        foreignKey: 'primarygroup',
        as: 'primarygroupObject'
    });

    return {
        Group,
        User
    };
};