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

export type AuditAction = 'create' | 'read' | 'update' | 'delete' | 'login' | 'logout' | 'export';
export type AuditCategory = 'data' | 'auth' | 'system' | 'config' | 'billing';
export type AuditStatus = 'success' | 'failure' | 'partial';

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

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

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

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

  @Column({ name: 'user_name', type: 'varchar', length: 200, nullable: true })
  userName: string;

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

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

  @Index()
  @Column({ name: 'action', type: 'varchar', length: 50 })
  action: AuditAction;

  @Index()
  @Column({ name: 'action_category', type: 'varchar', length: 50, nullable: true })
  actionCategory: AuditCategory;

  @Index()
  @Column({ name: 'resource_type', type: 'varchar', length: 100 })
  resourceType: string;

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

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

  @Column({ name: 'old_values', type: 'jsonb', nullable: true })
  oldValues: Record<string, any>;

  @Column({ name: 'new_values', type: 'jsonb', nullable: true })
  newValues: Record<string, any>;

  @Column({ name: 'changed_fields', type: 'text', array: true, nullable: true })
  changedFields: string[];

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

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

  @Column({ name: 'device_info', type: 'jsonb', default: {} })
  deviceInfo: Record<string, any>;

  @Column({ name: 'location', type: 'jsonb', default: {} })
  location: Record<string, any>;

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

  @Column({ name: 'request_method', type: 'varchar', length: 10, nullable: true })
  requestMethod: string;

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

  @Column({ name: 'request_params', type: 'jsonb', default: {} })
  requestParams: Record<string, any>;

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

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

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

  @Column({ name: 'metadata', type: 'jsonb', default: {} })
  metadata: Record<string, any>;

  @Column({ name: 'tags', type: 'text', array: true, default: [] })
  tags: string[];

  @Index()
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;
}
