import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { Student } from './student.entity';
import { DepositInstallment } from './deposit-installment.entity';

@Entity('deposits')
export class Deposit {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'student_id' })
  studentId: number;

  @Column({ type: 'decimal', precision: 10, scale: 2, default: 500 })
  amount: number;

  // paid: 有可用余额 | refunded: 余额已全部退还 | depleted: 余额已被账单扣完
  @Column({ type: 'varchar', length: 20, default: 'paid' })
  status: string;

  @Column({ name: 'paid_date', type: 'date' })
  paidDate: string;

  @Column({ name: 'refund_date', type: 'date', nullable: true })
  refundDate: string;

  @Column({ name: 'refund_amount', type: 'decimal', precision: 10, scale: 2, nullable: true })
  refundAmount: number;

  @Column({ name: 'deduction_amount', type: 'decimal', precision: 10, scale: 2, default: 0 })
  deductionAmount: number;

  @Column({ name: 'deduction_reason', type: 'text', nullable: true })
  deductionReason: string;

  @Column({ type: 'text', nullable: true })
  notes: string;

  @Column({ name: 'recorded_by', type: 'integer', nullable: true })
  recordedBy: number | null;

  @Column({ name: 'refunded_by', type: 'integer', nullable: true })
  refundedBy: number | null;

  @Column({ name: 'refunded_at', type: 'datetime', nullable: true })
  refundedAt: Date | null;

  @OneToMany(() => DepositInstallment, (i) => i.deposit, { cascade: true, eager: true })
  installments: DepositInstallment[];

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @ManyToOne(() => Student, { eager: true })
  @JoinColumn({ name: 'student_id' })
  student: Student;

}
