import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    CreateDateColumn,
    UpdateDateColumn,
    ManyToOne,
    JoinColumn,
} from 'typeorm';
import { User } from '../user/user.entity';
import { KnowledgeGroup } from '../knowledge-group/knowledge-group.entity';
import { Tenant } from '../tenant/tenant.entity';

@Entity('notes')
export class Note {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    title: string;

    @Column('text')
    content: string;

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

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

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

    @Column({ name: 'group_id', nullable: true })
    groupId: string; // Corresponds to Notebook/KnowledgeGroup ID

    @Column({
        type: 'simple-enum',
        enum: ['PRIVATE', 'TENANT', 'GLOBAL_PENDING', 'GLOBAL_APPROVED'],
        default: 'PRIVATE',
        name: 'sharing_status'
    })
    sharingStatus: string;

    @Column({ name: 'screenshot_path', nullable: true })
    screenshotPath: string; // Path to screenshot image for PDF selections

    @Column({ name: 'source_file_id', nullable: true })
    sourceFileId: string; // ID of the source PDF file

    @Column({ name: 'source_page_number', nullable: true })
    sourcePageNumber: number; // Page number in the source PDF

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

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

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

    @ManyToOne(() => KnowledgeGroup)
    @JoinColumn({ name: 'group_id' })
    group: KnowledgeGroup;

    @Column({ name: 'category_id', nullable: true })
    categoryId: string;

    @ManyToOne('NoteCategory', 'notes', { nullable: true, onDelete: 'SET NULL' })
    @JoinColumn({ name: 'category_id' })
    category: any;
}
