/**
 * Abonnement.js
 *
 * @description :: Modèle pour gérer les abonnements des parents
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

const crypto = require('crypto');
const shortId = (prefix) => `${prefix}${crypto.randomBytes(4).toString('hex').toUpperCase()}`;

module.exports = {
    tableName: 'abonnement',

    attributes: {
        codeAbonnement: {
            type: 'string',
            columnName: 'code_abonnement',
            unique: true
        },

        codeParent: {
            type: 'string',
            columnName: 'code_parent',
            required: true
        },

        codeEcole: {
            type: 'string',
            columnName: 'code_ecole',
            required: true
        },

        typeAbonnement: {
            type: 'string',
            columnName: 'type_abonnement',
            required: true,
            isIn: ['gratuit', 'mensuel', 'trimestriel', 'semestriel', 'annuel', 'essai']
        },

        statut: {
            type: 'string',
            columnName: 'statut',
            isIn: ['actif', 'expire', 'suspendu', 'annule', 'en_attente_paiement', 'essai'],
            defaultsTo: 'en_attente_paiement'
        },

        dateDebut: {
            type: 'ref',
            columnName: 'date_debut',
            required: true
        },

        dateFin: {
            type: 'ref',
            columnName: 'date_fin',
            required: true
        },

        montant: {
            type: 'number',
            columnName: 'montant',
            required: true
        },

        devise: {
            type: 'string',
            columnName: 'devise',
            defaultsTo: 'XOF'
        },

        // Association vers l'offre (FK Waterline)
        offre: {
            model: 'offreabonnement',
            columnName: 'offre_id'
        },

        codeOffre: {
            type: 'string',
            columnName: 'code_offre',
            allowNull: true
        },

        // Association vers Promotion (optionnelle)
        promotion: {
            model: 'promotionabonnement',
            columnName: 'promotion_id'
        },

        codePromotion: {
            type: 'string',
            columnName: 'code_promotion',
            allowNull: true
        },

        montantReduction: {
            type: 'number',
            columnName: 'montant_reduction',
            defaultsTo: 0
        },

        montantFinal: {
            type: 'number',
            columnName: 'montant_final',
            required: true
        },

        methodePaiement: {
            type: 'string',
            columnName: 'methode_paiement',
            isIn: ['mobile_money', 'carte_bancaire', 'virement', 'especes', 'gratuit', 'fedapay'],
            allowNull: true
        },

        referencePaiement: {
            type: 'string',
            columnName: 'reference_paiement',
            allowNull: true
        },

        datePaiement: {
            type: 'ref',
            columnName: 'date_paiement',
            // allowNull: true
        },

        renouvellementAuto: {
            type: 'boolean',
            columnName: 'renouvellement_auto',
            defaultsTo: false
        },

        dateRenouvellement: {
            type: 'ref',
            columnName: 'date_renouvellement',
            // allowNull: true
        },

        abonnementPrecedent: {
            type: 'string',
            columnName: 'abonnement_precedent',
            allowNull: true
        },

        commentaires: {
            type: 'string',
            columnName: 'commentaires',
            allowNull: true
        },

        // Relations
        transactions: {
            collection: 'transactionabonnement',
            via: 'abonnement'
        },

        notifications: {
            collection: 'notificationabonnement',
            via: 'abonnement'
        }
    },

    beforeCreate: function (values, cb) {
        // Générer un code court et lisible
        if (!values.codeAbonnement) {
            values.codeAbonnement = shortId('ABO_');
        }

        // Calculer le montant final
        if (values.montant && values.montantReduction) {
            values.montantFinal = values.montant - values.montantReduction;
        } else if (values.montant) {
            values.montantFinal = values.montant;
        }

        cb();
    }
}; 