"""
員工履歷資料 Model (人員基本檔)
記錄員工的個人資料、教育背景等（與任用無關的基本資料）
"""
from datetime import datetime, date
from sqlalchemy import Column, Integer, String, Boolean, Date, DateTime, Text, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import relationship

from app.db.base import Base


class EmpResume(Base):
    """員工履歷表（人員基本檔）"""

    __tablename__ = "tenant_emp_resumes"
    __table_args__ = (
        UniqueConstraint("tenant_id", "seq_no", name="uq_tenant_resume_seq"),
        UniqueConstraint("tenant_id", "id_number", name="uq_tenant_id_number"),
        Index("idx_emp_resume_tenant", "tenant_id"),
    )

    id = Column(Integer, primary_key=True, index=True)
    tenant_id = Column(Integer, ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True,
                       comment="租戶 ID")
    seq_no = Column(Integer, nullable=False, comment="租戶內序號 (觸發器自動生成)")

    # 個人基本資料
    legal_name = Column(String(100), nullable=False, comment="法定姓名")
    english_name = Column(String(100), nullable=True, comment="英文名稱")
    id_number = Column(String(20), nullable=False, comment="身分證字號/護照號碼")
    birth_date = Column(Date, nullable=True, comment="出生日期")
    gender = Column(String(10), nullable=True, comment="性別: M/F/Other")
    marital_status = Column(String(20), nullable=True, comment="婚姻狀況: single/married/divorced/widowed")
    nationality = Column(String(50), nullable=True, comment="國籍")

    # 聯絡資訊
    phone = Column(String(20), nullable=True, comment="聯絡電話")
    mobile = Column(String(20), nullable=True, comment="手機")
    personal_email = Column(String(255), nullable=True, comment="個人郵箱")
    address = Column(Text, nullable=True, comment="通訊地址")
    emergency_contact = Column(String(100), nullable=True, comment="緊急聯絡人")
    emergency_phone = Column(String(20), nullable=True, comment="緊急聯絡電話")

    # 教育背景
    education_level = Column(String(50), nullable=True, comment="學歷: high_school/bachelor/master/phd")
    school_name = Column(String(200), nullable=True, comment="畢業學校")
    major = Column(String(100), nullable=True, comment="主修科系")
    graduation_year = Column(Integer, nullable=True, comment="畢業年份")

    # 備註
    notes = Column(Text, nullable=True, comment="備註")

    # 通用欄位
    is_active = Column(Boolean, default=True, nullable=False, comment="是否啟用")
    edit_by = Column(String(100), nullable=True, comment="最後編輯者")
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False, comment="建立時間")
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新時間")

    # 關聯
    tenant = relationship("Tenant")
    employment_setting = relationship(
        "EmpSetting",
        back_populates="resume",
        uselist=False,
        cascade="all, delete-orphan"
    )

    def __repr__(self):
        return f"<EmpResume {self.legal_name} ({self.id_number})>"
