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

@Entity({ name: 'tenant_usage', schema: 'storage' })
@Unique(['tenantId', 'bucketId', 'monthYear'])
export class TenantUsage {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

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

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

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

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

  @Column({ name: 'month_year', type: 'varchar', length: 7 })
  monthYear: string;

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

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