import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { DeviceAssignmentsEntity } from './DeviceAssignments.entity';
import { VendorsEntity } from './Vendors.entity';
import { SCHEMA_NAME } from 'src/constants/auth';

// @Index('asset_tag', ['asset_tag'], { unique: true })
@Index('serial_number', ['serial_number'], { unique: true })
@Index('fk_devices_vendor', ['vendor_id'], {})
@Entity('devices', { schema: SCHEMA_NAME })
export class DevicesEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Column('varchar', { name: 'asset_tag', unique: false, length: 100 })
  asset_tag: string;

  @Column('varchar', {
    name: 'serial_number',
    nullable: true,
    unique: true,
    length: 150,
  })
  serial_number: string | null;

  @Column('enum', {
    name: 'device_type',
    enum: ['laptop', 'monitor', 'phone', 'tablet', 'printer', 'other'],
  })
  device_type: 'laptop' | 'monitor' | 'phone' | 'tablet' | 'printer' | 'other';

  @Column('varchar', { name: 'brand', nullable: true, length: 100 })
  brand: string | null;

  @Column('varchar', { name: 'model', nullable: true, length: 100 })
  model: string | null;

  @Column('longtext', { name: 'specifications', nullable: true })
  specifications: string | null;

  @Column('date', { name: 'lease_start_date', nullable: true })
  lease_start_date: string | null;

  @Column('date', { name: 'lease_end_date', nullable: true })
  lease_end_date: string | null;

  @Column('int', { name: 'vendor_id', nullable: true })
  vendor_id: number | string | null;

  @Column('enum', {
    name: 'status',
    enum: ['available', 'assigned', 'maintenance', 'retired'],
    default: () => "'available'",
  })
  status: 'available' | 'assigned' | 'maintenance' | 'retired';

  @Column('varchar', { name: 'device_condition', nullable: true, length: 100 })
  device_condition: string | null;

  @Column('decimal', {
    name: 'purchase_value',
    nullable: true,
    precision: 12,
    scale: 2,
  })
  purchase_value: string | null;

  @Column('decimal', {
    name: 'current_value',
    nullable: true,
    precision: 12,
    scale: 2,
  })
  current_value: string | null;

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

  @Column('json', {
    name: 'attachment_file',
    nullable: true,
  })
  attachment_file: object | null;

  @Column('timestamp', {
    name: 'created_at',
    default: () => 'CURRENT_TIMESTAMP',
  })
  created_at: Date;

  @Column('timestamp', {
    name: 'updated_at',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updated_at: Date;

  @OneToMany(
    () => DeviceAssignmentsEntity,
    (deviceAssignments) => deviceAssignments.device,
  )
  deviceAssignments: DeviceAssignmentsEntity[];

  @ManyToOne(() => VendorsEntity, (vendors) => vendors.devices, {
    onDelete: 'SET NULL',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'vendor_id', referencedColumnName: 'id' }])
  vendor: VendorsEntity;
}
