import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';

import { GameEntity } from '../game/game.entity';
import { FactionEntity } from '../faction/faction.entity';

@Entity('MapDrawing')
export class MapDrawingEntity {
  @PrimaryGeneratedColumn('uuid') mapDrawingId: string;
  @Column({ type: 'bool', nullable: true }) drawingIsActive: boolean;
  @Column({ type: 'time', nullable: true }) drawingValidTill: string;

  @Column({ type: 'json', nullable: true }) data: JSON;

  @ManyToOne(type => FactionEntity, faction => faction.mapDrawings, {
    onDelete: 'CASCADE',
  })
  faction: FactionEntity;
  @ManyToOne(type => GameEntity, gameEntity => gameEntity.id, {
    onDelete: 'CASCADE',
  })
  gameId: GameEntity;

  async ownershipCheck(factionEntity, role) {
    if (role === 'admin') {
      return factionEntity == this.faction;
    } else {
      return this.faction && factionEntity.factionId === this.faction.factionId
        ? true
        : false;
    }
  }
}