from __future__ import annotations

import uuid
from datetime import date, datetime

from sqlalchemy import Boolean, Column, Date, DateTime, ForeignKey, String, Table, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.base_class import Base

employee_role_links = Table(
    "employee_role_links",
    Base.metadata,
    Column("employee_id", String(36), ForeignKey("employees.id"), primary_key=True),
    Column("role_id", String(36), ForeignKey("roles.id"), primary_key=True),
)


class Employee(Base):
    __tablename__ = "employees"

    id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    employee_no: Mapped[str] = mapped_column(String(50), unique=True, index=True)
    name: Mapped[str] = mapped_column(String(100), index=True)
    email: Mapped[str] = mapped_column(String(255), unique=True, index=True)
    gender: Mapped[str | None] = mapped_column(String(20), nullable=True)
    birth_date: Mapped[date | None] = mapped_column(Date(), nullable=True)
    phone: Mapped[str | None] = mapped_column(String(30), nullable=True)
    join_date: Mapped[date | None] = mapped_column(Date(), nullable=True)
    location: Mapped[str | None] = mapped_column(String(100), nullable=True)
    position: Mapped[str] = mapped_column(String(100), default="员工")
    grade: Mapped[str] = mapped_column(String(20), default="P3", index=True)
    cost_center: Mapped[str | None] = mapped_column(String(50), nullable=True)
    finance_owner_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    password_hash: Mapped[str | None] = mapped_column(String(255), nullable=True)
    employment_status: Mapped[str] = mapped_column(String(30), default="在职", index=True)
    sync_state: Mapped[str] = mapped_column(String(30), default="已同步")
    spotlight: Mapped[bool] = mapped_column(Boolean, default=False)
    last_sync_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
    organization_unit_id: Mapped[str | None] = mapped_column(
        ForeignKey("organization_units.id"), nullable=True, index=True
    )
    manager_id: Mapped[str | None] = mapped_column(ForeignKey("employees.id"), nullable=True, index=True)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
    )

    organization_unit = relationship("OrganizationUnit", back_populates="employees")
    manager = relationship("Employee", remote_side=[id], back_populates="reports")
    reports = relationship("Employee", back_populates="manager")
    roles = relationship("Role", secondary=employee_role_links, back_populates="employees")
    change_logs = relationship(
        "EmployeeChangeLog",
        back_populates="employee",
        cascade="all, delete-orphan",
        order_by="desc(EmployeeChangeLog.occurred_at)",
    )
    reimbursement_requests = relationship("ReimbursementRequest", back_populates="employee")
