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

export type TransportAssignmentDocument = TransportAssignment & Document;

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

    @Prop({ required: true, trim: true }) routeName: string;
    @Prop({ required: true, trim: true }) installment: string;

    @Prop({ required: true, min: 0 }) amount: number;
    @Prop({ default: 0, min: 0 }) fineAmount: number;
    @Prop({ required: true }) dueDate: Date;

    @Prop({ required: true, enum: ['pending', 'paid'], default: 'pending' })
    status: 'pending' | 'paid';

    @Prop() paidAt?: Date;
    @Prop({ enum: ['razorpay', 'cash', 'cheque', 'bank_transfer'] })
    paymentMethod?: 'razorpay' | 'cash' | 'cheque' | 'bank_transfer';
    @Prop({ trim: true }) receiptNumber?: string;
    @Prop() razorpayOrderId?: string;
    @Prop() razorpayPaymentId?: string;
    @Prop() razorpaySignature?: string;
    @Prop() collectedBy?: string;
    @Prop({ trim: true }) notes?: string;
    @Prop({ trim: true }) pickupPoint?: string;
    @Prop({ default: true }) isActive: boolean;
}

export const TransportAssignmentSchema = SchemaFactory.createForClass(TransportAssignment);
TransportAssignmentSchema.index({ studentId: 1, academicYearId: 1 });
TransportAssignmentSchema.index({ routeId: 1 });
TransportAssignmentSchema.index({ academicYearId: 1, status: 1 });
TransportAssignmentSchema.index({ status: 1, dueDate: 1 });
TransportAssignmentSchema.index({ razorpayOrderId: 1 }, { unique: true, sparse: true });
TransportAssignmentSchema.index({ receiptNumber: 1 }, { unique: true, sparse: true });
TransportAssignmentSchema.index({ studentId: 1, routeId: 1, installment: 1 }, { unique: true });
TransportAssignmentSchema.index({ studentId: 1, routeId: 1, isActive: 1 });
TransportAssignmentSchema.set('toJSON', { versionKey: false });
TransportAssignmentSchema.set('toObject', { versionKey: false });
