import { Column, Entity, Index, JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { VehicleDealershipModelEntity } from './vehicle-dealership-model.entity';
import { VehicleDealerBayEntity } from './vehicle-dealer-bay.entity';
import { CompanyEntity } from './company.entity';
import * as dayjs from 'dayjs';

export interface DealershipParkingSpot {
    x: number;
    y: number;
    z: number;
    heading: number;
}

export interface DealershipShowroomPosition {
    x: number;
    y: number;
    z: number;
    heading: number;
    category: string;
}

export interface DealershipShowroomModel {
    position: DealershipShowroomPosition;
    vehicle?: VehicleMp;
    label: TextLabelMp;
    price: number;
    discount: number;
    dealership: VehicleDealershipEntity;
    created: dayjs.Dayjs;
    player?: PlayerMpLoggedIn;
    model?: VehicleDealershipModelEntity;
}

export interface DealershipDisplayModel {
    position: DealershipParkingSpot;
    vehicle: VehicleMp;
    dealership: VehicleDealershipEntity;
    created: dayjs.Dayjs;
}

@Entity('vehicle_dealerships')
export class VehicleDealershipEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column({ type: 'varchar', length: 64 }) name: string;
    @Column() x: number;
    @Column() y: number;
    @Column() z: number;
    @Column() marker_type: number;
    @Column() blip_sprite: number;
    @Index()
    @Column()
    company_id: number;
    @Column({ type: 'varchar', length: 64, nullable: true }) website?: string;
    @Column({ type: 'json' }) showroom_positions: DealershipShowroomPosition[];
    @Column({ type: 'json' }) parking_spots: DealershipParkingSpot[];
    @Column({ type: 'json' }) display_spots: DealershipParkingSpot[];
    @Column({ type: 'tinyint', default: 0 }) buys_everything: boolean;

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

    @OneToMany(() => VehicleDealershipModelEntity, (model) => model.dealership)
    models!: VehicleDealershipModelEntity[];

    @OneToMany(() => VehicleDealerBayEntity, (bay) => bay.dealership)
    bays!: VehicleDealerBayEntity[];
    showroomModels: DealershipShowroomModel[] = [];
    displayModels: DealershipDisplayModel[] = [];

    marker: MarkerMp;
    blip: BlipMp;

    welcomeColShape: ColshapeMp;

    getRandomParkingSpot(): DealershipParkingSpot {
        return this.parking_spots[Math.floor(this.parking_spots.length * Math.random())];
    }
}
