/**
 * StorageFile Entity
 * File metadata with versioning, checksums, and processing status
 * Compatible with erp-core file.entity
 *
 * @module Storage
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
  Unique,
} from 'typeorm';
import { StorageBucket } from './bucket.entity';
import { StorageFolder } from './folder.entity';

export type FileCategory = 'image' | 'document' | 'video' | 'audio' | 'archive' | 'other';
export type FileStatus = 'active' | 'processing' | 'archived' | 'deleted';
export type ProcessingStatus = 'pending' | 'processing' | 'completed' | 'failed';

@Entity({ name: 'files', schema: 'storage' })
@Unique(['tenantId', 'bucketId', 'path', 'version'])
export class StorageFile {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Index()
  @Column({ name: 'bucket_id', type: 'uuid' })
  bucketId: string;

  @Index()
  @Column({ name: 'folder_id', type: 'uuid', nullable: true })
  folderId: string;

  @Column({ name: 'name', type: 'varchar', length: 255 })
  name: string;

  @Column({ name: 'original_name', type: 'varchar', length: 255 })
  originalName: string;

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

  @Index()
  @Column({ name: 'mime_type', type: 'varchar', length: 100 })
  mimeType: string;

  @Column({ name: 'extension', type: 'varchar', length: 20, nullable: true })
  extension: string;

  @Index()
  @Column({ name: 'category', type: 'varchar', length: 30, nullable: true })
  category: FileCategory;

  @Column({ name: 'size_bytes', type: 'bigint' })
  sizeBytes: number;

  @Column({ name: 'checksum_md5', type: 'varchar', length: 32, nullable: true })
  checksumMd5: string;

  @Index()
  @Column({ name: 'checksum_sha256', type: 'varchar', length: 64, nullable: true })
  checksumSha256: string;

  @Column({ name: 'storage_key', type: 'text' })
  storageKey: string;

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

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

  @Column({ name: 'width', type: 'int', nullable: true })
  width: number;

  @Column({ name: 'height', type: 'int', nullable: true })
  height: number;

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

  @Column({ name: 'thumbnails', type: 'jsonb', default: {} })
  thumbnails: Record<string, string>;

  @Column({ name: 'metadata', type: 'jsonb', default: {} })
  metadata: Record<string, any>;

  @Column({ name: 'tags', type: 'text', array: true, default: [] })
  tags: string[];

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

  @Column({ name: 'version', type: 'int', default: 1 })
  version: number;

  @Column({ name: 'parent_version_id', type: 'uuid', nullable: true })
  parentVersionId: string;

  @Column({ name: 'is_latest', type: 'boolean', default: true })
  isLatest: boolean;

  @Index()
  @Column({ name: 'entity_type', type: 'varchar', length: 100, nullable: true })
  entityType: string;

  @Column({ name: 'entity_id', type: 'uuid', nullable: true })
  entityId: string;

  @Column({ name: 'is_public', type: 'boolean', default: false })
  isPublic: boolean;

  @Column({ name: 'access_count', type: 'int', default: 0 })
  accessCount: number;

  @Column({ name: 'last_accessed_at', type: 'timestamptz', nullable: true })
  lastAccessedAt: Date;

  @Index()
  @Column({ name: 'status', type: 'varchar', length: 20, default: 'active' })
  status: FileStatus;

  @Column({ name: 'archived_at', type: 'timestamptz', nullable: true })
  archivedAt: Date;

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

  @Column({ name: 'processing_status', type: 'varchar', length: 20, nullable: true })
  processingStatus: ProcessingStatus;

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

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

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

  @Column({ name: 'uploaded_by', type: 'uuid', nullable: true })
  uploadedBy: string;

  @ManyToOne(() => StorageBucket, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'bucket_id' })
  bucket: StorageBucket;

  @ManyToOne(() => StorageFolder, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'folder_id' })
  folder: StorageFolder;
}
