import {
    Column,
    CreateDateColumn,
    Entity,
    OneToMany,
    PrimaryGeneratedColumn,
    UpdateDateColumn,
} from 'typeorm';
import { User } from '../user/user.entity';
import { TenantMember } from './tenant-member.entity';
import { JoinColumn, ManyToOne } from 'typeorm';

@Entity('tenants')
export class Tenant {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({ type: 'text', unique: true })
    name: string;

    @Column({ type: 'text', unique: true, nullable: true })
    domain: string;

    @Column({ type: 'boolean', default: false })
    isSystem: boolean;

    @Column({ name: 'parent_id', type: 'text', nullable: true })
    parentId: string;

    @ManyToOne(() => Tenant, (tenant) => tenant.children, { onDelete: 'SET NULL' })
    @JoinColumn({ name: 'parent_id' })
    parent: Tenant;

    @OneToMany(() => Tenant, (tenant) => tenant.parent)
    children: Tenant[];

    @OneToMany(() => TenantMember, (member) => member.tenant)
    members: TenantMember[];

    @CreateDateColumn({ name: 'created_at' })
    createdAt: Date;

    @UpdateDateColumn({ name: 'updated_at' })
    updatedAt: Date;
}
