import { Column, CreateDateColumn, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { ItemTypeEntity } from './item-type.entity';
import { CompanyEntity } from './company.entity';

export type TruckingContractStatus = 'open' | 'claimed' | 'completed' | 'cancelled';
export type TruckingContractSourceType = 'npc' | 'business';

@Entity('trucking_contracts')
export class TruckingContractEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ type: 'enum', enum: ['npc', 'business'], default: 'npc' })
    source_type: TruckingContractSourceType;

    /** ID of NPC industry or property, depending on source_type */
    @Column({ type: 'int', nullable: true })
    source_id: number | null;

    /** Display name of the requesting entity — never reveals NPC source */
    @Column({ type: 'varchar', length: 256 })
    source_name: string;

    @Column()
    item_type_id: number;

    @Column()
    quantity: number;

    @Column()
    quantity_remaining: number;

    @Index()
    @Column({ type: 'enum', enum: ['open', 'claimed', 'completed', 'cancelled'], default: 'open' })
    status: TruckingContractStatus;

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

    @Column({ type: 'timestamp', nullable: true })
    claimed_at: Date | null;

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

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

    @CreateDateColumn()
    created_at: Date;

    @ManyToOne(() => ItemTypeEntity)
    @JoinColumn({ name: 'item_type_id' })
    itemType: ItemTypeEntity;

    @ManyToOne(() => CompanyEntity, { nullable: true })
    @JoinColumn({ name: 'claimed_by_company_id' })
    claimedByCompany: CompanyEntity | null;
}
