import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToOne,
  JoinColumn,
  ManyToOne,
  PrimaryColumn,
} from 'typeorm';
import { User } from '../../users/entity/user.entity';
import { bigintTransformer } from '../../../common/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({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  ordered_at: Date;

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

  @Column()
  quantity: number;

  @Column()
  status: string;

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

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  updated_at: Date;
}

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

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

  @Column()
  account_id: number;

  @Column()
  type: string;

  @Column()
  status: string;

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  allocated_user_id: number | null;

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  order_id: number | null;

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

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  delete_order_id: number | null;

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  updated_at: Date;

  @OneToOne(() => User, (user) => user.license, {
    createForeignKeyConstraints: false,
  }) // createForeignKeyConstraintsはSQLite用設定値.本番用は別途migrationで設定
  @JoinColumn({ name: 'allocated_user_id' })
  user: User | null;
}

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

  @Column()
  issued_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  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, type: 'datetime' })
  activated_at: Date | null;

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  updated_at: Date;
}

@Entity({ name: 'license_allocation_history' })
export class LicenseAllocationHistory {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  user_id: number;

  @Column()
  license_id: number;

  @Column()
  is_allocated: boolean;

  @Column()
  account_id: number;

  @Column()
  executed_at: Date;

  @Column()
  switch_from_type: string;

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

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  updated_at: Date;

  @ManyToOne(() => License, (licenses) => licenses.id, {
    createForeignKeyConstraints: false,
  }) // createForeignKeyConstraintsはSQLite用設定値.本番用は別途migrationで設定
  @JoinColumn({ name: 'license_id' })
  license: License | null;
}

@Entity({ name: 'licenses_archive' })
export class LicenseArchive {
  @PrimaryColumn()
  id: number;

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

  @Column()
  account_id: number;

  @Column()
  type: string;

  @Column()
  status: string;

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  allocated_user_id: number | null;

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  order_id: number | null;

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

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  delete_order_id: number | null;

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @Column()
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @Column()
  updated_at: Date;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  archived_at: Date;
}

@Entity({ name: 'license_allocation_history_archive' })
export class LicenseAllocationHistoryArchive {
  @PrimaryColumn()
  id: number;

  @Column()
  user_id: number;

  @Column()
  license_id: number;

  @Column()
  is_allocated: boolean;

  @Column()
  account_id: number;

  @Column()
  executed_at: Date;

  @Column()
  switch_from_type: string;

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

  @Column({ nullable: true, type: 'datetime' })
  created_by: string | null;

  @Column()
  created_at: Date;

  @Column({ nullable: true, type: 'datetime' })
  updated_by: string | null;

  @Column()
  updated_at: Date;

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  })
  archived_at: Date;
}
