import { Account } from '../../../repositories/accounts/entity/account.entity';
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToOne,
  OneToMany,
  PrimaryColumn,
} from 'typeorm';
import { License } from '../../licenses/entity/license.entity';
import { UserGroupMember } from '../../user_groups/entity/user_group_member.entity';

@Entity({ name: 'users' })
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  external_id: string;

  @Column()
  account_id: number;

  @Column()
  role: string;

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

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

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

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

  @Column({ default: false })
  email_verified: boolean;

  @Column({ default: true })
  auto_renew: boolean;

  @Column({ default: true })
  notification: boolean;

  @Column({ default: false })
  encryption: boolean;

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

  @Column({ default: false })
  prompt: boolean;

  @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',
  }) // defaultはSQLite用設定値.本番用は別途migrationで設定
  created_at: Date;

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

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  }) // defaultはSQLite用設定値.本番用は別途migrationで設定
  updated_at: Date;

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

  @OneToOne(() => License, (license) => license.user)
  license: License | null;

  @OneToMany(() => UserGroupMember, (userGroupMember) => userGroupMember.user)
  userGroupMembers: UserGroupMember[] | null;
}

@Entity({ name: 'users_archive' })
export class UserArchive {
  @PrimaryColumn()
  id: number;

  @Column()
  external_id: string;

  @Column()
  account_id: number;

  @Column()
  role: string;

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

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

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

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

  @Column()
  email_verified: boolean;

  @Column()
  auto_renew: boolean;

  @Column()
  notification: boolean;

  @Column()
  encryption: boolean;

  @Column()
  prompt: boolean;

  @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',
  }) // defaultはSQLite用設定値.本番用は別途migrationで設定
  archived_at: Date;
}

export type newUser = Omit<
  User,
  | 'id'
  | 'deleted_at'
  | 'created_at'
  | 'updated_at'
  | 'updated_by'
  | 'created_by'
  | 'account'
  | 'license'
  | 'userGroupMembers'
  | 'email_verified'
>;
