/**
 * StorageUpload Entity
 * Chunked upload tracking
 * Compatible with erp-core upload.entity
 *
 * @module Storage
 */

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

export type UploadStatus = 'pending' | 'uploading' | 'processing' | 'completed' | 'failed' | 'cancelled';

@Entity({ name: 'uploads', schema: 'storage' })
export class StorageUpload {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

  @Column({ name: 'total_size_bytes', type: 'bigint', nullable: true })
  totalSizeBytes: number;

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

  @Column({ name: 'uploaded_bytes', type: 'bigint', default: 0 })
  uploadedBytes: number;

  @Column({ name: 'upload_progress', type: 'decimal', precision: 5, scale: 2, default: 0 })
  uploadProgress: number;

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

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

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

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

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

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

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

  @Column({ name: 'started_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  startedAt: Date;

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

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

  @Index()
  @Column({ name: 'expires_at', type: 'timestamptz', nullable: true })
  expiresAt: Date;

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

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

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

  @ManyToOne(() => StorageFolder, { nullable: true })
  @JoinColumn({ name: 'folder_id' })
  folder: StorageFolder;

  @ManyToOne(() => StorageFile, { nullable: true })
  @JoinColumn({ name: 'file_id' })
  file: StorageFile;
}
