import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
} from 'typeorm';

export type LoginStatus = 'success' | 'failed' | 'blocked' | 'mfa_required' | 'mfa_failed';
export type AuthMethod = 'password' | 'sso' | 'oauth' | 'mfa' | 'magic_link' | 'biometric';
export type MfaMethod = 'totp' | 'sms' | 'email' | 'push';

@Entity({ name: 'login_history', schema: 'audit' })
export class LoginHistory {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid', nullable: true })
  tenantId: string;

  @Index()
  @Column({ name: 'user_id', type: 'uuid', nullable: true })
  userId: string;

  @Column({ name: 'email', type: 'varchar', length: 255, nullable: true })
  email: string;

  @Column({ name: 'username', type: 'varchar', length: 100, nullable: true })
  username: string;

  @Index()
  @Column({ name: 'status', type: 'varchar', length: 20 })
  status: LoginStatus;

  @Column({ name: 'auth_method', type: 'varchar', length: 30, nullable: true })
  authMethod: AuthMethod;

  @Column({ name: 'oauth_provider', type: 'varchar', length: 30, nullable: true })
  oauthProvider: string;

  @Column({ name: 'mfa_method', type: 'varchar', length: 20, nullable: true })
  mfaMethod: MfaMethod;

  @Column({ name: 'mfa_verified', type: 'boolean', nullable: true })
  mfaVerified: boolean;

  @Column({ name: 'device_id', type: 'uuid', nullable: true })
  deviceId: string;

  @Column({ name: 'device_fingerprint', type: 'varchar', length: 255, nullable: true })
  deviceFingerprint: string;

  @Column({ name: 'device_type', type: 'varchar', length: 30, nullable: true })
  deviceType: string;

  @Column({ name: 'device_os', type: 'varchar', length: 50, nullable: true })
  deviceOs: string;

  @Column({ name: 'device_browser', type: 'varchar', length: 50, nullable: true })
  deviceBrowser: string;

  @Index()
  @Column({ name: 'ip_address', type: 'inet', nullable: true })
  ipAddress: string;

  @Column({ name: 'user_agent', type: 'text', nullable: true })
  userAgent: string;

  @Column({ name: 'country_code', type: 'varchar', length: 2, nullable: true })
  countryCode: string;

  @Column({ name: 'city', type: 'varchar', length: 100, nullable: true })
  city: string;

  @Column({ name: 'latitude', type: 'decimal', precision: 10, scale: 8, nullable: true })
  latitude: number;

  @Column({ name: 'longitude', type: 'decimal', precision: 11, scale: 8, nullable: true })
  longitude: number;

  @Column({ name: 'risk_score', type: 'int', nullable: true })
  riskScore: number;

  @Column({ name: 'risk_factors', type: 'jsonb', default: [] })
  riskFactors: string[];

  @Index()
  @Column({ name: 'is_suspicious', type: 'boolean', default: false })
  isSuspicious: boolean;

  @Column({ name: 'is_new_device', type: 'boolean', default: false })
  isNewDevice: boolean;

  @Column({ name: 'is_new_location', type: 'boolean', default: false })
  isNewLocation: boolean;

  @Column({ name: 'failure_reason', type: 'varchar', length: 100, nullable: true })
  failureReason: string;

  @Column({ name: 'failure_count', type: 'int', nullable: true })
  failureCount: number;

  @Index()
  @Column({ name: 'attempted_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  attemptedAt: Date;
}
