const mongoose = require('mongoose');

// User Schema
const userSchema = new mongoose.Schema({
    fullName: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    mobile: { type: String, required: true, unique: true },
    referralCode: { type: String, unique: true },
    referredBy: String,
    bankName: String,
    accountNumber: String,
    withdrawalBalance: { type: Number, default: 0 },
    exemptWithdrawalBalance: { type: Number, default: 0 }, // Added to track funds exempt from recommitment
    referralBonus: { type: Number, default: 0 },
    isAdmin: { type: Boolean, default: false },
    suspended: { type: Boolean, default: false },
    registrationDate: { type: Date, default: Date.now },
    highestInvestmentAmount: { type: Number, default: 0 },
    lastInvestmentAmount: { type: Number, default: 0 },
    isEligibleForWithdrawal: { type: Boolean, default: true },
    pendingRecommitmentAmount: { type: Number, default: 0 },
    lastChallengeAttempt: { type: Date, default: null },
    resetPasswordToken: String,
    resetPasswordExpires: Date,
    isVerified: { type: Boolean, default: false },
    verificationToken: String,
    verificationTokenExpires: Date,
    cycleSettlementRequired: { type: Boolean, default: false }
});

// Investment Schema
const investmentSchema = new mongoose.Schema({
    userEmail: String,
    package: String,
    amount: Number,
    profit: Number,
    totalReturn: Number,
    returnRate: Number,
    duration: Number,
    date: { type: Date, default: Date.now },
    maturityDate: Date,
    proofImage: String,
    status: { type: String, default: 'Pending' },
    paymentMethod: { type: String, default: 'Manual' },
    isRecommitment: { type: Boolean, default: false },
    originalInvestmentId: String,
    walletDeduction: { type: Number, default: 0 },
    deleted: { type: Boolean, default: false },
    deletionReason: String,
    deletedAt: Date,
    deletedBy: String,
    withdrawn: { type: Boolean, default: false },
    investmentCycle: { type: Number, default: 1 },
    isRecommitmentCycle: { type: Boolean, default: false },
    settlementAmount: { type: Number, default: 0 },
    cycle2SettlementPaid: { type: Boolean, default: false },
    remainingRecommitmentBalance: { type: Number, default: 0 },
    nextCycleRecommitment: { type: Number, default: 0 },
    totalPaidForCycle: { type: Number, default: 0 },
    settlementDate: Date,
    settlementApprovedAt: Date,
    settlementApprovedBy: String,
    cycleSettlementRequired: { type: Boolean, default: false },
    originalRecommitmentAmount: { type: Number, default: 0 }
});

// Transaction Schema
const transactionSchema = new mongoose.Schema({
    userEmail: String,
    type: String,
    amount: Number,
    description: String,
    transactionType: String,
    status: { type: String, default: 'Completed' },
    date: { type: Date, default: Date.now }
});

// Withdrawal Request Schema
const withdrawalSchema = new mongoose.Schema({
    userEmail: { type: String, required: true },
    amount: { type: Number, required: true },
    bankName: String,
    accountNumber: String,
    status: { type: String, default: 'Pending' },
    isExempt: { type: Boolean, default: false }, // Added to track if withdrawal bypassed recommitment
    requestDate: { type: Date, default: Date.now },
    actionDate: Date,
    note: String
});

// Settings Schema
const settingSchema = new mongoose.Schema({
    type: { type: String, default: 'config' },
    packages: [{
        id: Number,
        name: String,
        returnRate: Number,
        duration: Number,
        enabled: Boolean
    }],
    referralBonus: {
        level1: Number,
        level2: Number
    },
    recommitment: {
        enabled: { type: Boolean, default: true },
        percentage: { type: Number, default: 50 }
    }
});

// Wallet Recommitment Schema
const walletRecommitmentSchema = new mongoose.Schema({
    userEmail: { type: String, required: true },
    originalInvestmentAmount: { type: Number, required: true },
    recommitmentAmount: { type: Number, required: true },
    walletDeduction: { type: Number, required: true },
    originalInvestmentId: { type: mongoose.Schema.Types.ObjectId, ref: 'Investment' },
    recommitmentInvestmentId: { type: mongoose.Schema.Types.ObjectId, ref: 'Investment' },
    status: { type: String, default: 'Pending' },
    date: { type: Date, default: Date.now },
    approvedAt: Date,
    approvedBy: String
});

const User = mongoose.model('User', userSchema);
const Investment = mongoose.model('Investment', investmentSchema);
const Transaction = mongoose.model('Transaction', transactionSchema);
const Setting = mongoose.model('Setting', settingSchema);
const Withdrawal = mongoose.model('Withdrawal', withdrawalSchema);
const WalletRecommitment = mongoose.model('WalletRecommitment', walletRecommitmentSchema);

module.exports = { User, Investment, Transaction, Setting, Withdrawal, WalletRecommitment };