import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { CharacterEntity } from './character.entity';
import { CompanyEntity } from './company.entity';
import { FactionEntity } from './faction.entity';
import { VehicleDealershipModelEntity } from './vehicle-dealership-model.entity';
import { VehicleColorEntity } from './vehicle-color.entity';
import { VehicleWheelsEntity } from './vehicle-wheels.entity';
import { VehicleUpgradeEntity } from './vehicle-upgrade.entity';
import { InvoiceEntity } from './invoice.entity';
import { VehicleEngineEntity } from './vehicle-engine.entity';
import { VehicleDealershipEntity } from './vehicle-dealership.entity';

@Entity('vehicle_orders')
export class VehicleOrderEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column() dealership_id: number;
    @Column({ type: 'int', nullable: true }) character_id: number | null;
    @Column({ type: 'int', nullable: true }) company_id: number | null;
    @Column({ type: 'int', nullable: true }) faction_id: number | null;
    @Column({ type: 'int', nullable: true }) vehicle_id: number | null;
    @Column({ type: 'int', nullable: true }) invoice_id: number | null;
    @Column({ type: 'int', nullable: true }) loan_id: number | null;
    @Column() model_id: number;
    @Column() color_id: number;
    @Column({ type: 'int', nullable: false }) engine_level: number;
    @Column() wheel_id: number;
    @Column() created_by_character_id: number;
    @Column({ type: 'datetime' }) created: Date;
    @Column({ type: 'datetime', nullable: true }) claimed: Date | null;
    @Column({ type: 'datetime' }) ready: Date;
    @Column({ type: 'boolean' }) sent_text: boolean;

    @OneToOne(() => VehicleDealershipModelEntity, {
        eager: true,
    })
    @JoinColumn({ name: 'model_id', referencedColumnName: 'id' })
    model: VehicleDealershipModelEntity;

    @OneToOne(() => VehicleColorEntity, {
        eager: true,
    })
    @JoinColumn({ name: 'color_id', referencedColumnName: 'id' })
    color: VehicleColorEntity;

    @OneToOne(() => VehicleWheelsEntity, {
        eager: true,
    })
    @JoinColumn({ name: 'wheel_id', referencedColumnName: 'id' })
    wheel: VehicleWheelsEntity;

    @ManyToMany(() => VehicleUpgradeEntity, {
        eager: true,
    })
    @JoinTable({
        name: 'vehicle_order_upgrades',
        joinColumn: { name: 'vehicle_order_id' },
        inverseJoinColumn: { name: 'vehicle_upgrade_id' },
    })
    upgrades: VehicleUpgradeEntity[];

    @OneToOne(() => VehicleEngineEntity, {
        eager: true,
    })
    @JoinColumn({ name: 'engine_level', referencedColumnName: 'level' })
    engine: VehicleEngineEntity;

    @ManyToOne(() => InvoiceEntity)
    @JoinColumn({ name: 'invoice_id', referencedColumnName: 'id' })
    invoice?: InvoiceEntity;

    @ManyToOne(() => CharacterEntity, (character) => character.vehicleOrders)
    @JoinColumn({ name: 'character_id' })
    character?: CharacterEntity;

    @ManyToOne(() => CompanyEntity, (company) => company.vehicleOrders)
    @JoinColumn({ name: 'company_id' })
    company?: CompanyEntity;

    @ManyToOne(() => FactionEntity, (faction) => faction.vehicleOrders)
    @JoinColumn({ name: 'faction_id' })
    faction?: FactionEntity;

    @ManyToOne(() => VehicleDealershipEntity)
    @JoinColumn({ name: 'dealership_id' })
    dealership?: VehicleDealershipEntity;

    @ManyToOne(() => CharacterEntity)
    @JoinColumn({ name: 'created_by_character_id' })
    createdByCharacter?: CharacterEntity;

    validate(): boolean {
        return !!(this.model && this.color && this.wheel && this.upgrades);
    }
}
