import mongoose, { Schema, Document } from 'mongoose';
import jwt from 'jsonwebtoken';

// Interface for the wallet schema
interface IWallet extends Document {
    walletKey: string;
    createdAt: Date;
    isWalletConnect: boolean;
    email: string;
    password: string;
    generateAuthToken(): string;
}

// Define the wallet schema
const walletSchema: Schema = new Schema({
    walletKey: {
        type: String,
        unique: true,
    },
    email: {
        type: String,
        required: true,
        unique: true,
    },
    password: {
        type: String,
        required: true,
        default:null,
    },
    isWalletConnect: {
        type: Boolean,
        default: false,
    },
    createdAt: {
        type: Date,
        default: Date.now,
    },
});

// Method to generate an authentication token
walletSchema.methods.generateAuthToken = function (): string {
    const token = jwt.sign({ _id: this._id }, process.env.JWT_SECRET || 'default_secret', { expiresIn: '1h' });
    return token;
};

// Create the model from the schema
const Wallet = mongoose.model<IWallet>('Wallet', walletSchema);

export default Wallet;
