import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { CompanyPositionEntity } from './company-position.entity';
import { CompanyEmploymentPointEntity } from './company-employment-point.entity';
import { ChangingRoomEntity } from './changing-room.entity';
import { CompanyUniformEntity } from './company-uniform.entity';
import { CharacterEntity } from './character.entity';
import { GovernmentContractEntity } from './government-contract.entity';
import { VehicleEntity } from './vehicle.entity';
import { BankAccountEntity } from './bank-account.entity';
import { PhoneEntity } from './phone.entity';
import { VehicleOrderEntity } from './vehicle-order.entity';

@Entity('companies')
export class CompanyEntity {
    @PrimaryGeneratedColumn() id: number;
    @Column({ type: 'varchar', length: 64 }) name: string;
    @Column({ type: 'int' }) type: number;
    @Column({ type: 'varchar', length: 64, nullable: true }) stamp: string | null;
    @Column({ type: 'int', nullable: true }) owner_character_id: number | null;
    @Column({ type: 'tinyint' }) protected: boolean;
    @Column({ type: 'varchar', length: 6 }) chat_color: string;
    @Column({ type: 'int', default: 3 }) max_on_shift: number;

    // type should be type_id so we can add relations to companytypeentity

    @OneToMany(() => CompanyPositionEntity, (position) => position.company)
    positions: CompanyPositionEntity[];

    @OneToMany(() => CompanyEmploymentPointEntity, (point) => point.company)
    employmentPoints: CompanyEmploymentPointEntity[];

    @OneToMany(() => ChangingRoomEntity, (changingRoom) => changingRoom.company)
    changingRooms: ChangingRoomEntity[];

    @OneToMany(() => CompanyUniformEntity, (uniform) => uniform.company)
    uniforms: CompanyUniformEntity[];

    @ManyToMany(() => CharacterEntity, (character) => character.companies)
    @JoinTable({
        name: 'character_company_positions',
        joinColumn: { name: 'company_id' },
        inverseJoinColumn: { name: 'character_id' },
    })
    employees: CharacterEntity[];

    @OneToMany(() => VehicleEntity, (vehicle) => vehicle.company)
    vehicles: VehicleEntity[];

    @OneToMany(() => BankAccountEntity, (bankAccount) => bankAccount.company)
    bankAccounts: BankAccountEntity[];

    @OneToMany(() => PhoneEntity, (phone) => phone.company)
    phones: PhoneEntity[];

    @ManyToMany(() => GovernmentContractEntity, (contract) => contract.companies)
    @JoinTable({
        name: 'company_contracts',
        joinColumn: { name: 'company_id' },
        inverseJoinColumn: { name: 'contract_id' },
    })
    contracts: GovernmentContractEntity[];

    @ManyToOne(() => CharacterEntity, (character) => character.ownedCompanies)
    @JoinColumn({ name: 'owner_character_id' })
    owner?: CharacterEntity;

    @OneToMany(() => VehicleOrderEntity, (voe) => voe.character)
    vehicleOrders: VehicleOrderEntity[];

    positionPermissionAccess: {
        id: number;
        name: string;
        description: string;
    }[] = [];

    getRegNo() {
        return String(this.id).padStart(6, '0');
    }

    getName() {
        return this.name ?? `Company #${this.getRegNo()}`;
    }

    getEntityType(): 'company' {
        return 'company';
    }

    chatDisabled = false;
}
