import {
  Column,
  Entity,
  Index,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { DevicesEntity } from './Devices.entity';
import { SCHEMA_NAME } from 'src/constants/auth';

@Index('email', ['email'], { unique: true })
@Entity('vendors', { schema: SCHEMA_NAME })
export class VendorsEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Column('varchar', { name: 'name', length: 150 })
  name: string;

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

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

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

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

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

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

  @Column('enum', {
    name: 'status',
    enum: ['active', 'inactive'],
    default: () => "'active'",
  })
  status: 'active' | 'inactive';

  @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(() => DevicesEntity, (devices) => devices.vendor)
  devices: DevicesEntity[];
}
