import { Column, Entity, getRepository, JoinColumn, OneToMany, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
// import { LogSystem, logSystem } from '../core/log';
import { ItemEntity } from './item.entity';
import { FurnitureItemEntity } from './furniture-item.entity';
import { DarknetOrderEntity } from './darknet-order.entity';
import { LogSystem } from '../core/log';

@Entity('inventories')
export class InventoryEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column() game_entity_id: number;
    @Column() game_entity_type_id: number;
    @Column() capacity: number;

    @OneToOne(() => FurnitureItemEntity, { createForeignKeyConstraints: false })
    @JoinColumn({
        name: 'game_entity_id',
        referencedColumnName: 'id',
    })
    furnitureItem: FurnitureItemEntity;

    @OneToOne(() => ItemEntity, { createForeignKeyConstraints: false })
    @JoinColumn({
        name: 'game_entity_id',
        referencedColumnName: 'id',
    })
    parentItem: ItemEntity;

    @OneToMany(() => ItemEntity, (item) => item.inventory, {
        // eager: true,
        cascade: ['update'],
    })
    items: ItemEntity[];

    @OneToMany(() => DarknetOrderEntity, (order) => order.inventory)
    darknetOrders: DarknetOrderEntity[];

    name?: string;

    worldInventory?: boolean;

    overloaded?: boolean;

    async removeItem(item: ItemEntity): Promise<void> {
        try {
            const itemRepository = getRepository(ItemEntity);

            await itemRepository.remove(item);

            this.items = this.items.filter((i) => i !== item);
        } catch (error) {
            LogSystem.Log('error', `${error}`);
        }
    }

    cleanup() {
        this.items = this.items.filter((item) => item.inventory_id === this.id);
    }
}
