import { BaseEntity } from "@edr/api-common";
import {
  Check,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
} from "typeorm";

import { FileUploadSetting } from "./file-upload-setting.entity";

@Entity({schema:"freight", name: "file_upload_fields" })
@Index(["settingId", "fileKey"], { unique: true })
@Check(`"max_files" > 0`)
@Check(`"max_size_mb" > 0`)
export class FileUploadField extends BaseEntity {
  @ManyToOne(() => FileUploadSetting, (setting) => setting.fields, {
    onDelete: "CASCADE",
  })
  @JoinColumn({ name: "setting_id" })
  setting!: FileUploadSetting;

  @Column({ name: "setting_id", type: "uuid" })
  settingId!: string;

  @Column({ name: "file_key", type: "varchar", length: 128 })
  fileKey!: string;

  @Column({ name: "file_label", type: "varchar", length: 256 })
  fileLabel!: string;

  @Column({ name: "help_text", type: "text", nullable: true })
  helpText?: string | null;

  @Column({ name: "is_required", type: "boolean", default: false })
  isRequired!: boolean;

  @Column({ name: "is_multiple", type: "boolean", default: false })
  isMultiple!: boolean;

  @Column({ name: "max_files", type: "integer", default: 1 })
  maxFiles!: number;

  @Column({
    name: "allowed_extensions",
    type: "text",
    array: true,
    default: () => "'{}'::text[]",
  })
  allowedExtensions!: string[];

  @Column({ name: "max_size_mb", type: "integer", default: 10 })
  maxSizeMb!: number;

  @Column({ name: "display_order", type: "integer", default: 0 })
  displayOrder!: number;
}