package br.gov.sigefp.org.domain;

import br.gov.sigefp.common.domain.BaseEntity;
import jakarta.persistence.*;
import lombok.*;

/**
 * Entidade que representa uma unidade organizacional.
 * Suporta hierarquia através de parentUnit (auto-relacionamento).
 */
@Entity
@Table(name = "org_unit", indexes = {
    @Index(name = "idx_org_unit_code", columnList = "code"),
    @Index(name = "idx_org_unit_ministry", columnList = "ministry_id"),
    @Index(name = "idx_org_unit_parent", columnList = "parent_unit_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrgUnit extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ministry_id", nullable = false)
    private Ministry ministry;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_unit_id")
    private OrgUnit parentUnit;

    @Column(nullable = false, unique = true, length = 50)
    private String code;

    @Column(nullable = false, length = 200)
    private String name;

    @Column(nullable = false, length = 50, name = "unit_type")
    private String unitType; // DEPARTMENT, DIVISION, SECTION, etc.

    @Column(length = 20)
    private String acronym;

    @Column(length = 255)
    private String address;

    @Column(length = 100)
    private String email;

    @Column(length = 20)
    private String phone;

    @Column(nullable = false, name = "is_active")
    @Builder.Default
    private Boolean isActive = true;
}

