import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import { encryptData, decryptData } from '../utils/encrypt.js';

const ExternalTournamentParticipant = sequelize.define('ExternalTournamentParticipant', {
    tournamentId: {
        type: DataTypes.INTEGER,
        allowNull: false,
    },
    groupId: {
        type: DataTypes.INTEGER,
        autoIncrement: false,
        allowNull: true
    },
    firstName: {
        type: DataTypes.STRING,
        allowNull: false,
        set(value) {
            const encryptedValue = encryptData(value);
            this.setDataValue('firstName', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('firstName');
            return decryptData(encryptedValue);
        }
    },
    lastName: {
        type: DataTypes.STRING,
        allowNull: false,
        set(value) {
            const encryptedValue = encryptData(value);
            this.setDataValue('lastName', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('lastName');
            return decryptData(encryptedValue);
        }
    },
    club: {
        type: DataTypes.STRING,
        allowNull: true,
        set(value) {
            if (!value) {
                this.setDataValue('club', null);
                return;
            }
            const encryptedValue = encryptData(value);
            this.setDataValue('club', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('club');
            if (!encryptedValue) return null;
            return decryptData(encryptedValue);
        }
    },
    email: {
        type: DataTypes.STRING(500),
        allowNull: true,
        set(value) {
            if (!value) {
                this.setDataValue('email', null);
                return;
            }
            const encryptedValue = encryptData(value);
            this.setDataValue('email', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('email');
            if (!encryptedValue) return null;
            return decryptData(encryptedValue);
        }
    },
    address: {
        type: DataTypes.TEXT,
        allowNull: true,
        set(value) {
            if (!value) {
                this.setDataValue('address', null);
                return;
            }
            const encryptedValue = encryptData(value);
            this.setDataValue('address', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('address');
            if (!encryptedValue) return null;
            return decryptData(encryptedValue);
        }
    },
    birthDate: {
        type: DataTypes.STRING,
        allowNull: true,
        set(value) {
            if (!value) {
                this.setDataValue('birthDate', null);
                return;
            }
            const encryptedValue = encryptData(value || '');
            this.setDataValue('birthDate', encryptedValue);
        },
        get() {
            const encryptedValue = this.getDataValue('birthDate');
            if (!encryptedValue) return null;
            return decryptData(encryptedValue);
        }
    },
    gender: {
        type: DataTypes.ENUM('male', 'female', 'diverse', 'unknown'),
        allowNull: true,
        defaultValue: 'unknown'
    },
    seeded: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
    },
    classId: {
        type: DataTypes.INTEGER,
        allowNull: true
    },
    outOfCompetition: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
    },
    gaveUp: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        field: 'gave_up'
    }
}, {
    underscored: true,
    tableName: 'external_tournament_participant',
    timestamps: true,
});

export default ExternalTournamentParticipant;

