import { Column, Entity, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { LoanPaymentEntity } from './loan-payment.entity';
import { BankAccountEntity } from './bank-account.entity';

@Entity('loans')
export class LoanEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column({ type: 'varchar', length: 32 }) type: string;
    @Column({ type: 'varchar', length: 128 }) name: string;
    @Column() bank_account_id: number;
    @Column({ type: 'varchar', length: 32, default: 'company' }) creditor_entity: 'faction' | 'company';
    @Column() creditor_id: number;
    @Column() creditor_bank_account_id: number;
    @Column() principal: number;
    @Column() balance: number;
    @Column() interest: number;
    @Column() payment: number;
    @Column() payment_count: number;
    @Column({ type: 'varchar', length: 32 }) collateral_type: string;
    @Column({ type: 'varchar', length: 128 }) collateral_name: string;
    @Column({ type: 'int', nullable: true }) collateral_id: number | null;
    @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' }) created: Date;
    @Column({ type: 'datetime' }) start: Date;
    @Column({ type: 'datetime', nullable: true }) completed: Date | null;
    @Column({ type: 'datetime', nullable: true }) defaulted: Date | null;

    @OneToMany(() => LoanPaymentEntity, (payment) => payment.loan)
    payments: LoanPaymentEntity[];

    @ManyToOne(() => BankAccountEntity, (bae) => bae.loans)
    @JoinColumn({ name: 'bank_account_id' })
    bankAccount: BankAccountEntity;
}
