import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { TruckingLocationEntity } from './trucking-location.entity';
import { ItemTypeEntity } from './item-type.entity';
import { ItemCategoryEntity } from './item-category.entity';

@Entity('trucking_location_item_types')
export class TruckingLocationItemTypeEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    trucking_location_id: number;

    /** Either item_type_id or item_category_id is set — not both */
    @Column({ type: 'int', nullable: true })
    item_type_id: number | null;

    @Column({ type: 'int', nullable: true })
    item_category_id: number | null;

    @Column({ type: 'int', default: 0 })
    min_cdl_level: number;

    @ManyToOne(() => TruckingLocationEntity)
    @JoinColumn({ name: 'trucking_location_id' })
    location: TruckingLocationEntity;

    @ManyToOne(() => ItemTypeEntity, { nullable: true })
    @JoinColumn({ name: 'item_type_id' })
    itemType: ItemTypeEntity | null;

    @ManyToOne(() => ItemCategoryEntity, { nullable: true })
    @JoinColumn({ name: 'item_category_id' })
    itemCategory: ItemCategoryEntity | null;

    /** Human-readable description of what this mapping covers */
    getLabel(): string {
        if (this.itemType) return this.itemType.name;
        if (this.itemCategory) return `[Category] ${this.itemCategory.name}`;
        return 'Unknown';
    }
}
