import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { TransactionData } from '../../../domain/value-objects/transaction-data';

@Entity('transactions')
export class TransactionEntity {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Column()
  hash!: string;

  @Column({ type: 'enum', enum: ['pending', 'confirmed', 'failed', 'dropped'] })
  status!: 'pending' | 'confirmed' | 'failed' | 'dropped';

  @Column({
    type: 'bigint',
    nullable: true,
    transformer: {
      from: (value: string | null): number | null =>
        value === null ? null : Number(value),
      to: (value: number | null): string | null =>
        value === null ? null : String(value),
    },
  })
  block!: number | null;

  @Column({ name: 'network_id' })
  networkId!: string;

  @Column()
  timestamp!: Date;

  @Column({ name: 'from_address_id', type: 'varchar', length: 255, nullable: true })
  fromAddressId!: string | null;

  @Column({ name: 'to_address_id', type: 'varchar', length: 255, nullable: true })
  toAddressId!: string | null;

  @Column({ name: 'interacted_with', type: 'varchar', length: 255, nullable: true })
  interactedWith!: string | null;

  @Column({ type: 'decimal', precision: 65, scale: 0 })
  value!: string;

  @Column({ type: 'decimal', precision: 65, scale: 0, nullable: true })
  fee!: string | null;

  @Column({ name: 'gas_price', type: 'decimal', precision: 65, scale: 0, nullable: true })
  gasPrice!: string | null;

  @Column({ name: 'gas_limit', type: 'int', nullable: true })
  gasLimit!: number | null;

  @Column()
  method!: string;

  @Column('json')
  data!: TransactionData[];

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

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt!: Date;
}
