import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { BaseEntity } from '../../common/entities/base.entity';

export type FeePaymentDocument = FeePayment & Document;

@Schema({ timestamps: true })
export class FeePayment extends BaseEntity {
    @Prop({ required: true }) assignmentId: string;
    @Prop({ required: true }) studentId: string;
    @Prop({ required: true }) academicYearId: string;

    @Prop({ required: true, min: 1 }) amount: number;
    @Prop({ default: 0, min: 0 }) fineComponent: number;

    @Prop({ required: true, enum: ['razorpay', 'cash', 'cheque', 'bank_transfer', 'upi', 'online'] })
    paymentMethod: string;

    @Prop({ trim: true }) receiptNumber?: string;
    @Prop() razorpayOrderId?: string;
    @Prop() razorpayPaymentId?: string;
    @Prop() razorpaySignature?: string;
    @Prop({ trim: true, maxlength: 200 }) transactionId?: string;
    @Prop() collectedBy?: string;
    @Prop({ trim: true, maxlength: 200 }) collectedByName?: string;
    @Prop({ trim: true }) receiptUrl?: string;
    @Prop({ trim: true, maxlength: 1000 }) notes?: string;
    @Prop({ required: true }) paidAt: Date;
}

export const FeePaymentSchema = SchemaFactory.createForClass(FeePayment);
FeePaymentSchema.index({ assignmentId: 1, paidAt: 1 });
FeePaymentSchema.index({ studentId: 1, academicYearId: 1 });
FeePaymentSchema.index({ razorpayOrderId: 1 }, { unique: true, sparse: true });
FeePaymentSchema.index({ receiptNumber: 1 }, { unique: true, sparse: true });
FeePaymentSchema.set('toJSON', { versionKey: false });
FeePaymentSchema.set('toObject', { versionKey: false });
