/**
 * FileShare Entity
 * File sharing with granular permissions
 * Compatible with erp-core file-share.entity
 *
 * @module Storage
 */

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

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

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

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

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

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

  @Column({ name: 'shared_with_role', type: 'varchar', length: 50, nullable: true })
  sharedWithRole: string;

  @Column({ name: 'can_view', type: 'boolean', default: true })
  canView: boolean;

  @Column({ name: 'can_download', type: 'boolean', default: true })
  canDownload: boolean;

  @Column({ name: 'can_edit', type: 'boolean', default: false })
  canEdit: boolean;

  @Column({ name: 'can_delete', type: 'boolean', default: false })
  canDelete: boolean;

  @Column({ name: 'can_share', type: 'boolean', default: false })
  canShare: boolean;

  @Index()
  @Column({ name: 'public_link', type: 'varchar', length: 255, unique: true, nullable: true })
  publicLink: string;

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

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

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

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

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

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

  @Column({ name: 'notify_on_access', type: 'boolean', default: false })
  notifyOnAccess: boolean;

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

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

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

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