import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, Check } from 'typeorm';
import { License } from './license.entity';

@Entity('server_connections')
@Check(`"connection_type" IN ('amp', 'pterodactyl', 'bare_metal')`)
@Check(`"connection_status" IN ('connected', 'degraded', 'offline')`)
export class ServerConnection {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid', unique: true })
  license_id: string;

  @Column({ type: 'varchar', length: 20 })
  connection_type: string;

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

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

  @Column({ type: 'varchar', length: 255, nullable: true })
  panel_server_identifier: string | null;

  @Column({ type: 'varchar', length: 128, nullable: true })
  companion_agent_token: string | null;

  @Column({ type: 'timestamptz', nullable: true })
  companion_last_seen: Date | null;

  @Column({ type: 'timestamptz', nullable: true })
  plugin_last_seen: Date | null;

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

  @Column({ type: 'integer', nullable: true })
  server_port: number | null;

  @Column({ type: 'integer', nullable: true })
  game_port: number | null;

  @Column({ type: 'varchar', length: 20, default: 'offline' })
  connection_status: string;

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  created_at: Date;

  @Column({ type: 'timestamptz', default: () => 'NOW()' })
  updated_at: Date;

  @ManyToOne(() => License, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'license_id' })
  license: License;
}
