import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { LocationDefinition } from './location-definition.entity';

@Entity({ name: 'location_connections' })
@Index(
  'IDX_location_connection_direction',
  ['fromLocationId', 'toLocationId'],
  {
    unique: true,
  },
)
export class LocationConnection {
  @PrimaryGeneratedColumn('uuid', { name: 'id' })
  id!: string;

  @Column({ name: 'from_location_id', type: 'uuid' })
  fromLocationId!: string;

  @Column({ name: 'to_location_id', type: 'uuid' })
  toLocationId!: string;

  @Column({ name: 'travel_duration_seconds', type: 'integer' })
  travelDurationSeconds!: number;

  @Column({ name: 'ambush_chance', type: 'numeric', precision: 5, scale: 4 })
  ambushChance!: string;

  @Column({ name: 'enabled', type: 'boolean' })
  enabled!: boolean;

  @ManyToOne(
    () => LocationDefinition,
    (location) => location.outgoingConnections,
    { onDelete: 'RESTRICT' },
  )
  @JoinColumn({ name: 'from_location_id' })
  fromLocation!: LocationDefinition;

  @ManyToOne(
    () => LocationDefinition,
    (location) => location.incomingConnections,
    { onDelete: 'RESTRICT' },
  )
  @JoinColumn({ name: 'to_location_id' })
  toLocation!: LocationDefinition;
}
