import { DataSource } from 'typeorm';
import { DEMO_CHARACTER_ID } from '../../demo/demo-character.constants';
import { Character } from '../../characters/entities/character.entity';
import { LocationConnection } from '../../world/entities/location-connection.entity';
import { LocationDefinition } from '../../world/entities/location-definition.entity';
import { BURNED_ROAD_ID, SOUTH_GATE_ID } from './vertical-slice.constants';

export async function seedVisibleVerticalSlice(
  dataSource: DataSource,
): Promise<void> {
  const locationRepository = dataSource.getRepository(LocationDefinition);
  const connectionRepository = dataSource.getRepository(LocationConnection);
  const characterRepository = dataSource.getRepository(Character);

  const locations = [
    {
      id: SOUTH_GATE_ID,
      key: 'south-gate',
      name: 'Südtor von Graufurt',
      description:
        'Am schwarzen Südtor endet der Schutz Graufurts. Hinter den Wachtfeuern beginnt die stille Weite der Aschenfelder.',
      regionKey: 'ashen-fields',
      minRecommendedLevel: 1,
      maxRecommendedLevel: 1,
      dangerLevel: 0,
      isSafe: true,
      huntingEnabled: false,
      artworkPath: '/images/backgrounds/Suedtor.png',
    },
    {
      id: BURNED_ROAD_ID,
      key: 'burned-road',
      name: 'Verbrannte Straße',
      description:
        'Die alte Handelsstraße führt durch verkohlte Felder. Zwischen Asche und zerbrochenen Wagen warten die ersten Gefahren.',
      regionKey: 'ashen-fields',
      minRecommendedLevel: 1,
      maxRecommendedLevel: 2,
      dangerLevel: 1,
      isSafe: false,
      huntingEnabled: true,
      artworkPath: '/images/backgrounds/Aschestrasse.png',
    },
  ];
  let southGateId = SOUTH_GATE_ID;
  let burnedRoadId = BURNED_ROAD_ID;

  for (const location of locations) {
    const existing = await locationRepository.findOneBy({
      key: location.key,
    });
    const { id, key, ...definition } = location;
    const persistedId = existing?.id ?? id;

    if (existing) {
      await locationRepository.update(existing.id, definition);
    } else {
      await locationRepository.insert(location);
    }

    if (key === 'south-gate') {
      southGateId = persistedId;
    } else {
      burnedRoadId = persistedId;
    }
  }

  await connectionRepository.upsert(
    [
      {
        fromLocationId: southGateId,
        toLocationId: burnedRoadId,
        travelDurationSeconds: 10,
        ambushChance: '0.0500',
        enabled: true,
      },
      {
        fromLocationId: burnedRoadId,
        toLocationId: southGateId,
        travelDurationSeconds: 10,
        ambushChance: '0.0500',
        enabled: true,
      },
    ],
    ['fromLocationId', 'toLocationId'],
  );

  const existing = await characterRepository.findOneBy({
    id: DEMO_CHARACTER_ID,
  });

  if (!existing) {
    await characterRepository.insert({
      id: DEMO_CHARACTER_ID,
      name: 'Aric Duskwalker',
      level: 1,
      experience: 0,
      baseHp: 100,
      baseAttack: 6,
      currentHp: 100,
      currentLocationId: southGateId,
    });
  }
}
