import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { RefundEntity } from '../../domain/entities/refund.entity';
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { BaseStatusModel } from 'src/core/modules/data/model/base-status.model';
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
import { RefundItemModel } from './refund-item.model';
import { RefundReasonType, RefundType } from '../../constants';

@Entity(TABLE_NAME.REFUND)
export class RefundModel
  extends BaseStatusModel<RefundEntity>
  implements RefundEntity
{
  @Column('enum', {
    name: 'type',
    enum: RefundType,
    default: RefundType.BOOKING,
  })
  type: RefundType;

  @Column('varchar', { name: 'code', nullable: true })
  code: string;

  @Column('date', { name: 'request_date', nullable: true })
  request_date: Date;

  @Column('date', { name: 'refund_date', nullable: true })
  refund_date: Date;

  @Column('decimal', { name: 'refund_sub_total', nullable: true })
  refund_sub_total: number;

  @Column('decimal', { name: 'refund_total', nullable: true })
  refund_total: number;

  @Column('enum', {
    name: 'refund_reason_type',
    enum: RefundReasonType,
    default: RefundReasonType.RIDE_MALFUNCTION,
  })
  refund_reason_type: RefundReasonType;

  @Column('text', { name: 'refund_reason', nullable: true })
  refund_reason: string;

  // bank info
  @Column('varchar', { name: 'bank_name', nullable: true })
  bank_name: string;

  @Column('varchar', { name: 'bank_account_name', nullable: true })
  bank_account_name: string;

  @Column('varchar', { name: 'bank_account_number', nullable: true })
  bank_account_number: string;

  // relations to item
  @OneToMany(() => RefundItemModel, (model) => model.refund, {
    cascade: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  refund_items: RefundItemModel[];

  // relation to transaction
  @Column('varchar', { name: 'transaction_id', nullable: true })
  transaction_id: string;
  @ManyToOne(() => TransactionModel, (model) => model.refunds, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'transaction_id' })
  transaction: TransactionModel;
}
