import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  ManyToOne,
  JoinColumn,
  OneToMany,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { TenantAwareEntity } from '@/shared/entities/tenant-aware.entity';
import { Brand } from '@/modules/crm/entities/brand.entity';

@Entity('asset_folders', { schema: 'assets' })
@Index(['tenant_id', 'brand_id', 'parent_id'])
@Index(['tenant_id', 'slug'], { unique: true })
export class AssetFolder extends TenantAwareEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('uuid', { nullable: true })
  brand_id: string | null;

  @ManyToOne(() => Brand, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'brand_id' })
  brand: Brand;

  @Column('uuid', { nullable: true })
  parent_id: string | null;

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

  @OneToMany(() => AssetFolder, (folder) => folder.parent)
  children: AssetFolder[];

  @Column({ length: 255 })
  name: string;

  @Column({ length: 255 })
  slug: string;

  @Column({ type: 'text', nullable: true })
  description: string | null;

  @Column({ type: 'text', nullable: true })
  path: string | null; // Full path like /root/subfolder/this

  @Column({ type: 'int', default: 0 })
  level: number; // Depth level in hierarchy

  @Column({ type: 'int', default: 0 })
  sort_order: number;

  @CreateDateColumn({ type: 'timestamptz' })
  created_at: Date;

  @UpdateDateColumn({ type: 'timestamptz' })
  updated_at: Date;

  @Column({ type: 'timestamptz', nullable: true })
  deleted_at: Date | null;
}
