from sqlalchemy import (
    String,
    ForeignKey,
    Index,
    Numeric,
)
from sqlalchemy.orm import mapped_column, Mapped
from databases.sql_models.core_mixin import CrudCollection

from api_validations.validations_request import InsertCompanyEmployees


class Staff(CrudCollection):

    __tablename__ = "staff"
    __exclude__fields__ = []

    staff_description: Mapped[str] = mapped_column(
        String, server_default="", comment="Staff Description"
    )
    staff_name: Mapped[str] = mapped_column(
        String, nullable=False, comment="Staff Name"
    )
    staff_code: Mapped[str] = mapped_column(
        String, nullable=False, comment="Staff Code"
    )

    duties_id: Mapped[int] = mapped_column(ForeignKey("duties.id"), nullable=False)
    duties_uu_id: Mapped[str] = mapped_column(
        String, nullable=False, comment="Duty UUID"
    )

    # people: Mapped["People"] = relationship(
    #     "People", back_populates="employees", foreign_keys=[people_id], uselist=True
    # )
    # duty: Mapped["CompanyDuty"] = relationship(
    #     "CompanyDuty", back_populates="employees", foreign_keys=[duty_id]
    # )

    @classmethod
    def create_action(cls, data: InsertCompanyEmployees):
        from databases import Duties

        data_dict = data.model_dump()
        if duty := Duties.find_one(uu_id=data.duty_uu_id):
            data_dict["duty_id"] = duty.id
        # if person := People.find_one(uu_id=data.person_uu_id):
        #     data_dict["people_id"] = person.id
        if data.start_date:
            data_dict["expiry_starts"] = data.start_date
        if data.stop_date:
            data_dict["expiry_ends"] = data.stop_date
        # del data_dict["duty_uu_id"], data_dict["person_uu_id"]
        del data_dict["start_date"], data_dict["stop_date"], data_dict["duty_uu_id"]
        return cls.find_or_create(**data_dict)

    __table_args__ = ({"comment": "Staff Information"},)


class Employees(CrudCollection):

    __tablename__ = "employees"
    __exclude__fields__ = []

    staff_id: Mapped[int] = mapped_column(ForeignKey("staff.id"))
    staff_uu_id: Mapped[str] = mapped_column(
        String, nullable=False, comment="Staff UUID"
    )
    people_id: Mapped[int] = mapped_column(ForeignKey("people.id"), nullable=True)
    people_uu_id: Mapped[str] = mapped_column(
        String, nullable=True, comment="People UUID"
    )

    __table_args__ = (
        Index("employees_ndx_00", people_id, staff_id, unique=True),
        {"comment": "Employee Person Information"},
    )


class EmployeeHistory(CrudCollection):

    __tablename__ = "employee_history"
    __exclude__fields__ = []

    staff_id: Mapped[int] = mapped_column(
        ForeignKey("staff.id"), nullable=False, comment="Staff ID"
    )
    staff_uu_id: Mapped[str] = mapped_column(
        String, nullable=False, comment="Staff UUID"
    )
    people_id: Mapped[int] = mapped_column(
        ForeignKey("people.id"), nullable=False, comment="People ID"
    )
    people_uu_id: Mapped[str] = mapped_column(
        String, nullable=False, comment="People UUID"
    )

    __table_args__ = (
        Index("_employee_history_ndx_00", people_id, staff_id),
        {"comment": "Employee History Information"},
    )


class EmployeesSalaries(CrudCollection):

    __tablename__ = "employee_salaries"
    __exclude__fields__ = []

    gross_salary: Mapped[float] = mapped_column(
        Numeric(20, 6), nullable=False, comment="Gross Salary"
    )
    net_salary: Mapped[float] = mapped_column(
        Numeric(20, 6), nullable=False, comment="Net Salary"
    )

    people_id: Mapped[int] = mapped_column(ForeignKey("people.id"), nullable=False)
    people_uu_id: Mapped[str] = mapped_column(
        String, nullable=False, comment="People UUID"
    )

    # people: Mapped["People"] = relationship(
    #     "People", back_populates="employee_salaries", foreign_keys=[people_id]
    # )

    __table_args__ = (
        Index("_employee_salaries_ndx_00", people_id, "expiry_starts"),
        {"comment": "Employee Salaries Information"},
    )


# class Events2Employees(CrudCollection):
#
#     __tablename__ = "events2employees"
#     __exclude__fields__ = []
#
#     event_id = mapped_column(ForeignKey("events.id"), nullable=False)
#     employees_id = mapped_column(ForeignKey("employees.id"), nullable=False)
#
#     __table_args__ = (
#         Index("_events2employees_ndx_00", event_id, employees_id),
#         {"comment": "Events2Employees Information"},
#     )
