import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { CompanyEntity } from './company.entity';
import { CharacterEntity } from './character.entity';
import { FactionEntity } from './faction.entity';
import { preloadSystem } from '../core/preload';
import { companySystem } from '../features/company';
import { factionSystem } from '../features/faction';
import { playerSystem } from '../features/player';

@Entity('property_renter')
export class PropertyRenterEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column() property_id: number;
    @Column() entity_type_id: number;
    @Column() entity_id: number;
    @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP()' }) rent_start: Date;
    @Column() rent_price: number;
    @Column() rent_debt: number;
    @Column() rent_period: number; // in weeks
    @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP()' }) rent_last_paid: Date;

    async getEntity(): Promise<CharacterEntity | FactionEntity | CompanyEntity | undefined> {
        const type = preloadSystem.getGameEntityTypeById(this.entity_type_id);
        if (type === 'company') {
            return companySystem.getCompanyById(this.entity_id);
        }
        if (type === 'faction') {
            return factionSystem.getFactionFromId(this.entity_id);
        }
        if (type === 'character') {
            return playerSystem.getCharacterFromId(this.entity_id);
        }
    }
}
