import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DEMO_CHARACTER_ID } from '../demo/demo-character.constants';
import { Character } from './entities/character.entity';

@Injectable()
export class CharactersService {
  constructor(
    @InjectRepository(Character)
    private readonly characters: Repository<Character>,
  ) {}

  async getDemoCharacter() {
    const character = await this.characters.findOne({
      where: { id: DEMO_CHARACTER_ID },
      relations: { currentLocation: true },
    });

    if (!character) {
      throw new NotFoundException('Demo character has not been seeded');
    }

    return {
      id: character.id,
      name: character.name,
      level: character.level,
      experience: character.experience,
      currentHp: character.currentHp,
      maxHp: character.baseHp,
      attack: character.baseAttack,
      currentLocation: {
        id: character.currentLocation.id,
        key: character.currentLocation.key,
        name: character.currentLocation.name,
      },
    };
  }
}
