import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToOne,
  JoinColumn,
} from 'typeorm';
import { User } from '../../users/entity/user.entity';

@Entity({ name: 'license_orders' })
export class LicenseOrder {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  po_number: string;

  @Column()
  from_account_id: number;

  @Column()
  to_account_id: number;

  @CreateDateColumn()
  ordered_at: Date;

  @Column({ nullable: true })
  issued_at?: Date;

  @Column()
  quantity: number;

  @Column()
  status: string;

  @Column({ nullable: true })
  canceled_at?: Date;

  @Column({ nullable: true })
  created_by: string;

  @CreateDateColumn()
  created_at: Date;

  @Column({ nullable: true })
  updated_by: string;

  @UpdateDateColumn()
  updated_at: Date;
}

@Entity({ name: 'licenses' })
export class License {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: true })
  expiry_date: Date;

  @Column()
  account_id: number;

  @Column()
  type: string;

  @Column()
  status: string;

  @Column({ nullable: true })
  allocated_user_id: number;

  @Column({ nullable: true })
  order_id: number;

  @Column({ nullable: true })
  deleted_at: Date;

  @Column({ nullable: true })
  delete_order_id: number;

  @Column({ nullable: true })
  created_by: string;

  @CreateDateColumn()
  created_at: Date;

  @Column({ nullable: true })
  updated_by: string;

  @UpdateDateColumn()
  updated_at: Date;

  @OneToOne(() => User, (user) => user.license)
  @JoinColumn({ name: 'allocated_user_id' })
  user?: User;
}
@Entity({ name: 'licenses_history' })
export class LicenseHistory {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  user_id: number;

  @Column()
  license_id: number;

  @Column()
  allocated: boolean;

  @Column()
  executed_at: Date;

  @Column()
  exchange_type: string;
}

@Entity({ name: 'card_license_issue' })
export class CardLicenseIssue {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  issued_at: Date;

  @Column({ nullable: true })
  created_by: string;

  @CreateDateColumn()
  created_at: Date;

  @Column({ nullable: true })
  updated_by: string;

  @UpdateDateColumn()
  updated_at: Date;
}

@Entity({ name: 'card_licenses' })
export class CardLicense {
  @PrimaryGeneratedColumn()
  license_id: number;

  @Column()
  issue_id: number;

  @Column()
  card_license_key: string;

  @Column({ nullable: true })
  activated_at: Date;

  @Column({ nullable: true })
  created_by: string;

  @CreateDateColumn()
  created_at: Date;

  @Column({ nullable: true })
  updated_by: string;

  @UpdateDateColumn({})
  updated_at: Date;
}
