/**
 * Carrier Activity ORM Entity (Infrastructure Layer)
 *
 * TypeORM entity for carrier activity logging
 * Tracks all actions performed by carriers: login, booking actions, document downloads, etc.
 */

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { CarrierProfileOrmEntity } from './carrier-profile.orm-entity';
import { CsvBookingOrmEntity } from './csv-booking.orm-entity';

/**
 * Enum for carrier activity types
 */
export enum CarrierActivityType {
  BOOKING_ACCEPTED = 'BOOKING_ACCEPTED',
  BOOKING_REJECTED = 'BOOKING_REJECTED',
  DOCUMENT_DOWNLOADED = 'DOCUMENT_DOWNLOADED',
  PROFILE_UPDATED = 'PROFILE_UPDATED',
  LOGIN = 'LOGIN',
  PASSWORD_CHANGED = 'PASSWORD_CHANGED',
}

@Entity('carrier_activities')
@Index('idx_carrier_activities_carrier_id', ['carrierId'])
@Index('idx_carrier_activities_booking_id', ['bookingId'])
@Index('idx_carrier_activities_type', ['activityType'])
@Index('idx_carrier_activities_created_at', ['createdAt'])
@Index('idx_carrier_activities_carrier_created', ['carrierId', 'createdAt'])
export class CarrierActivityOrmEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'carrier_id', type: 'uuid' })
  carrierId: string;

  @ManyToOne(() => CarrierProfileOrmEntity, carrier => carrier.activities, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'carrier_id' })
  carrierProfile: CarrierProfileOrmEntity;

  @Column({ name: 'booking_id', type: 'uuid', nullable: true })
  bookingId: string | null;

  @ManyToOne(() => CsvBookingOrmEntity, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'booking_id' })
  booking: CsvBookingOrmEntity | null;

  @Column({
    name: 'activity_type',
    type: 'enum',
    enum: CarrierActivityType,
  })
  activityType: CarrierActivityType;

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

  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, any> | null;

  @Column({ name: 'ip_address', type: 'varchar', length: 45, nullable: true })
  ipAddress: string | null;

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

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;
}
