import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    CreateDateColumn,
    UpdateDateColumn,
    ManyToOne,
    JoinColumn,
    OneToMany,
} from 'typeorm';
import { User } from '../user/user.entity';
import { Tenant } from '../tenant/tenant.entity';
import { Note } from './note.entity';

@Entity('note_categories')
export class NoteCategory {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    name: string;

    @Column({ name: 'user_id' })
    userId: string;

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

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

    @Column({ default: 1 })
    level: number;

    @ManyToOne(() => NoteCategory, (category) => category.children, { onDelete: 'CASCADE' })
    @JoinColumn({ name: 'parent_id' })
    parent: NoteCategory;

    @OneToMany(() => NoteCategory, (category) => category.parent)
    children: NoteCategory[];

    @ManyToOne(() => Tenant, { nullable: true, onDelete: 'CASCADE' })
    @JoinColumn({ name: 'tenant_id' })
    tenant: Tenant;

    @ManyToOne(() => User)
    @JoinColumn({ name: 'user_id' })
    user: User;

    @OneToMany(() => Note, (note) => note.category)
    notes: Note[];

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

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