import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { PetTypeEntity } from './pet-types.entity';
import { CharacterEntity } from './character.entity';
import { FactionEntity } from './faction.entity';
import { PetState } from '../features/pets/types';

export interface PetSpawnData {
    x: number;
    y: number;
    z: number;
    dimension: number;
}

@Entity('pets')
export class PetEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column({ type: 'int', nullable: false }) pet_type_id: number;
    @Column({ type: 'int', default: null }) character_id: number | null;
    @Column({ type: 'int', default: null }) faction_id: number | null;
    @Column({ type: 'varchar', length: 64, default: null }) name: string | null;
    @Column({ type: 'json', default: null }) spawn: PetSpawnData | null;
    @Column({ type: 'datetime', default: null }) last_spawn: Date | null;

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

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

    @OneToOne(() => PetTypeEntity)
    @JoinColumn({
        name: 'pet_type_id',
        referencedColumnName: 'id',
    })
    type: PetTypeEntity;

    state: PetState;
    spawned_by?: PlayerMp;
    entity?: PedMp;
}
