import { Account } from '../../../repositories/accounts/entity/account.entity';
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToOne,
  OneToMany,
} 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 })
  author_id?: string;

  @Column({ nullable: true })
  accepted_terms_version?: string;

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

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

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

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

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

  @Column({ nullable: true })
  encryption_password?: string;

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

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

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

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

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

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

  @ManyToOne(() => Account, (account) => account.user)
  @JoinColumn({ name: 'account_id' })
  account?: Account;

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

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

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