import {
  Entity, Column, PrimaryGeneratedColumn, CreateDateColumn,
  UpdateDateColumn, VersionColumn, Index,
} from 'typeorm';
import { Platform } from '../enums/platform.enum';
import { AppType } from '../enums/app-type.enum';

@Entity('app_versions')
@Index('idx_app_versions_app_platform', ['appType', 'platform', 'isEnabled'])
@Index('idx_app_versions_code', ['appType', 'platform', 'versionCode'])
export class AppVersion {
  @PrimaryGeneratedColumn('uuid') id: string;

  @Column({ name: 'app_type', type: 'varchar', length: 20, default: 'GENEX_MOBILE' })
  appType: AppType;

  @Column({ type: 'varchar', length: 10 })
  platform: Platform;

  @Column({ name: 'version_code', type: 'int' })
  versionCode: number;

  @Column({ name: 'version_name', length: 32 })
  versionName: string;

  @Column({ name: 'build_number', length: 64 })
  buildNumber: string;

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

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

  @Column({ name: 'file_size', type: 'bigint', default: 0 })
  fileSize: string; // bigint as string in TypeORM

  @Column({ name: 'file_sha256', length: 64 })
  fileSha256: string;

  @Column({ name: 'min_os_version', length: 16, nullable: true })
  minOsVersion: string | null;

  @Column({ type: 'text', default: '' })
  changelog: string;

  @Column({ name: 'is_force_update', type: 'boolean', default: false })
  isForceUpdate: boolean;

  @Column({ name: 'is_enabled', type: 'boolean', default: true })
  isEnabled: boolean;

  @Column({ name: 'release_date', type: 'timestamptz', nullable: true })
  releaseDate: Date | null;

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

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

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedBy: string | null;

  @VersionColumn({ default: 1 }) version: number;
}
