import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { FineEntity } from './fine.entity';
import { InventoryEntity } from './inventory.entity';
import { DrugDosage, ItemTypeEntity } from './item-type.entity';
import { WorldItemEntity } from './world-item.entity';

@Entity('items')
export class ItemEntity {
    @PrimaryGeneratedColumn()
    id: number;
    @Column() type_id: number;
    @Column() amount: number;
    @Column({ length: 128, type: 'varchar', nullable: true }) name: string | null;
    @Column({ nullable: true }) inventory_id: number | null;
    @Column({ type: 'json' }) data: {
        loseOnDeath?: boolean;
        radio?: {
            paired: number[];
        };
        clothing?: {
            equipped: boolean;
            state: string;
            outfit?: string;
        };
        drug?: {
            purity: number;
        };
        drug2: {
            contents: {
                itemTypeId: number;
                amount: number; // amount in mg per 1 unit of the item
                active: boolean;
                purity: number | null;
            }[];
            dosage: DrugDosage[];
        };
        imageOverlay?: string;
        imageOverride?: string;
        portableRadio?: {
            state: 'on' | 'off';
            primary: boolean;
            activeSlot: number;
            slots: {
                slot: number;
                frequency: number;
                password?: string;
            }[];
        };
        weapon?: {
            ammoType: number;
            equipped: boolean;
            serial?: string;
            scratched?: boolean;
        };
        component?: {
            equipped: number;
        };
        breathalyser?: number;
        prop?: {
            equipped: boolean;
        };
        fishing?: {
            bait: number;
        };
        spraycan?: {
            equipped: boolean;
            usageCount: number;
        };
        camera?: {
            equipped: boolean;
            lens?: {
                itemId: number;
            };
        };
        cleaning?: {
            equipped: boolean;
        };
        outfit?: {
            customName: string | null;
        };
        ticket?: FineEntity;
        plate?: {
            number: string;
        };
        restraints?: {
            attachedTo: number;
            attachedBy: number;
            destroyOnRemove: boolean;
        };
        money?: {
            createdBy: {
                account: number;
                character: number;
                rgscId: string;
                serial: string;
            };
        };
        scratchoff?: {
            prize: number;
            cells: {
                amount: string;
                revealed: boolean;
            }[];
            symbols: {
                text: string;
                revealed: boolean;
            }[];
            word: {
                text: string;
                revealed: boolean;
            };
        };
        lockpick?: {
            equipped: boolean;
            uses: number;
        };
        blankNote?: {
            blank: boolean;
            imageLink: string | null;
        };
        postalDelivery?: {
            propertyId: number;
            address: string;
        };
        jerryCan?: {
            amount: number;
            equipped: boolean;
        };
        shovel?: {
            equipped: boolean;
        };
        phone?: {
            id: number;
            number: string;
            main: boolean;
            inUse: boolean;
            power: boolean;
            color: string;
        };
        armour?: {
            equipped: boolean;
            points: number;
        };
    };

    @Column({ type: 'tinyint', width: 1 }) held_by_prison: boolean;

    @ManyToOne(() => InventoryEntity, (inventory) => inventory.items, {
        cascade: ['update'],
    })
    @JoinColumn({ name: 'inventory_id' })
    inventory: InventoryEntity | null;

    @OneToOne(() => InventoryEntity, { createForeignKeyConstraints: false })
    @JoinColumn({
        name: 'id',
        referencedColumnName: 'game_entity_id',
    })
    childInventory?: InventoryEntity;

    @ManyToOne(() => ItemTypeEntity, (type) => type.items, {
        // eager: true,
    })
    @JoinColumn({ name: 'type_id' })
    type: ItemTypeEntity;

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

    public getName() {
        return this.name ?? this.type?.name ?? 'Unknown';
    }
}
