import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryColumn,
} from 'typeorm';
import { App } from './app.entity';

@Entity('builds')
export class Build {
  @PrimaryColumn('uuid')
  id: string;

  @Column({ name: 'app_id' })
  appId: string;

  @Column()
  version: string;

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

  @Column({ name: 'min_os', type: 'varchar', nullable: true })
  minOs: string | null;

  @Column({ name: 'drive_file_id' })
  driveFileId: string;

  @Column({ name: 'plist_file_id', type: 'varchar', nullable: true })
  plistFileId: string | null;

  @Column({ type: 'bigint', default: 0 })
  size: number;

  @Column({ name: 'short_code', type: 'varchar', length: 8, unique: true, nullable: true })
  shortCode: string | null;

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

  @Column({ name: 'icon_file_id', type: 'varchar', nullable: true })
  iconFileId: string | null;

  /** iOS only: device UDIDs from the provisioning profile (comma-separated in DB) */
  @Column({ type: 'simple-array', nullable: true })
  udids: string[] | null;

  /** iOS only: provisioning profile type (Development, AdHoc, Enterprise, AppStore) */
  @Column({ name: 'provisioning_profile', type: 'varchar', length: 50, nullable: true })
  provisioningProfile: string | null;

  /** Build type: for iOS (Development/AdHoc/Enterprise/AppStore), for Android (Debug/Release) */
  @Column({ name: 'build_type', type: 'varchar', length: 50, nullable: true })
  buildType: string | null;

  /** Password protection: hashed password if the build is password-protected */
  @Column({ name: 'password_hash', type: 'varchar', nullable: true })
  passwordHash: string | null;

  /** Plain text password (only visible to uploader/admin) */
  @Column({ name: 'password_text', type: 'varchar', nullable: true })
  passwordText: string | null;

  /** Whether this build is password protected */
  @Column({ name: 'is_password_protected', type: 'boolean', default: false })
  isPasswordProtected: boolean;

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

  @ManyToOne(() => App, (app) => app.builds, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'app_id' })
  app: App;
}
