import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { ItemEntity } from './item.entity';
import { CharacterEntity } from './character.entity';
import { InventoryEntity } from './inventory.entity';

export interface VehicleSyncedWorldItem {
    item: {
        id: number;
        type: {
            name: string;
            properties: {
                gameObject?: string;
                weaponHash?: number;
                childInventory?: boolean;
            };
        };
    };
    lock?: boolean;
    childInventory?: {
        id: number;
        name: string | undefined;
    };
    position: number[];
    placementType: 'trunk' | 'interior';
}

@Entity('world_items')
export class WorldItemEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column() item_id: number;
    @Column() inventory_id: number;
    @Column({ type: 'float' }) x: number;
    @Column({ type: 'float' }) y: number;
    @Column({ type: 'float' }) z: number;
    @Column({ type: 'float' }) rx: number;
    @Column({ type: 'float' }) ry: number;
    @Column({ type: 'float' }) rz: number;
    @Column() dimension: number;
    @Column() created_by: number;
    @Column({ type: 'int', nullable: true, default: null }) vehicle_id: number | null;
    @Column({ type: 'varchar', length: 10, nullable: true, default: null }) vehicle_placement_type: 'interior' | 'trunk' | null;
    @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' }) created: Date;

    @OneToOne(() => ItemEntity, (item) => item.worldItem)
    @JoinColumn({
        name: 'item_id',
        referencedColumnName: 'id',
    })
    item: ItemEntity;

    @ManyToOne(() => CharacterEntity)
    @JoinColumn({
        name: 'created_by',
        referencedColumnName: 'id',
    })
    character: {
        name: string;
    };

    @ManyToOne(() => InventoryEntity)
    @JoinColumn({
        name: 'inventory_id',
        referencedColumnName: 'id',
    })
    inventory?: InventoryEntity;
}
