import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { License } from './license.entity';
import { MapLibrary } from './map-library.entity';

@Entity('server_config')
export class ServerConfig {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ type: 'varchar', length: 255, default: '' })
  server_name: string;

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

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

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

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

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

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

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

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

  @Column({ type: 'boolean', default: false })
  auto_restart_enabled: boolean;

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

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

  @Column({ type: 'boolean', default: true })
  crash_recovery_enabled: boolean;

  @Column({ type: 'integer', default: 3 })
  crash_recovery_max_attempts: number;

  @Column({ type: 'integer', default: 10 })
  crash_recovery_cooldown_minutes: number;

  @Column({ type: 'boolean', default: true })
  force_wipe_eligible: boolean;

  @Column({ type: 'boolean', default: true })
  auto_update_on_force_wipe: boolean;

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

  @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;

  @ManyToOne(() => MapLibrary, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'current_map_id' })
  current_map: MapLibrary | null;
}
