/**
 * FileAccessToken Entity
 * Temporary access tokens for file downloads
 * Compatible with erp-core file-access-token.entity
 *
 * @module Storage
 */

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

@Entity({ name: 'file_access_tokens', schema: 'storage' })
export class FileAccessToken {
  @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: 'token', type: 'varchar', length: 255, unique: true })
  token: string;

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

  @Column({ name: 'allowed_ips', type: 'inet', array: true, nullable: true })
  allowedIps: string[];

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

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

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

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

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

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

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

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

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