import {
    Column,
    CreateDateColumn,
    Entity,
    Index,
    ManyToOne,
    OneToMany,
    PrimaryGeneratedColumn,
    UpdateDateColumn,
  } from 'typeorm';
  import { Branch } from './branch.entity';

  export type OfficeType = 'head' | 'branch' | 'sub_branch' | 'service_center';

  @Entity({ name: 'offices' })
  export class Office {
    @PrimaryGeneratedColumn('uuid')
    id!: string;

    @Index({ unique: true })
    @Column({ type: 'varchar', length: 50 })
    code!: string;

    @Column({ type: 'varchar', length: 200 })
    name!: string;

    @Column({ type: 'varchar', length: 50, default: 'branch' })
    type!: OfficeType;

    @Column({ type: 'varchar', name: 'parent_office_id', nullable: true })
    parentOfficeId?: string;

    @ManyToOne(() => Office, { nullable: true, onDelete: 'SET NULL' })
    parentOffice?: Office;

    @Column({ type: 'varchar', length: 50, nullable: true })
    phone?: string;

    @Column({ type: 'varchar', length: 200, nullable: true })
    email?: string;

    @Column({ type: 'text', nullable: true })
    address?: string;

    @Column({ type: 'decimal', precision: 10, scale: 7, nullable: true })
    latitude?: string;

    @Column({ type: 'decimal', precision: 10, scale: 7, nullable: true })
    longitude?: string;

    @Column({ default: true })
    active!: boolean;

    @OneToMany(() => Branch, (b) => b.office)
    branches!: Branch[];

    @CreateDateColumn()
    createdAt!: Date;

    @UpdateDateColumn()
    updatedAt!: Date;
  }
  