"""Employee logs data access backed strictly by PostgreSQL operational tables."""

from __future__ import annotations

from app.core.postgres import PostgresClient
from app.models.schemas import (
    EmployeeLogRecord,
    EmployeeLogsResponse,
)


class EmployeeLogsService:
    """Fetch employee log data from PostgreSQL operational tables only."""

    def __init__(self, postgres_client: PostgresClient | None = None) -> None:
        self._postgres_client = postgres_client or PostgresClient()

    def get_employee_logs(self) -> EmployeeLogsResponse:
        """Return only PostgreSQL operational data for employee work logs."""
        if not self._postgres_client.is_enabled():
            return EmployeeLogsResponse(
                items=[],
                source="postgres",
                used_fallback=False,
                message="PostgreSQL chưa được cấu hình nên chưa thể hiển thị nhật ký nhân sự thực tế.",
            )

        try:
            items = self._postgres_client.fetch_employee_log_records()
        except Exception as exc:
            return EmployeeLogsResponse(
                items=[],
                source="postgres",
                used_fallback=False,
                message=f"Không thể đọc dữ liệu nhật ký nhân sự từ PostgreSQL. Chi tiết: {exc}",
            )

        if not items:
            return EmployeeLogsResponse(
                items=[],
                source="postgres",
                used_fallback=False,
                message="PostgreSQL chưa có dữ liệu nhật ký nhân sự thực tế trong các bảng vận hành.",
            )

        return EmployeeLogsResponse(
            items=[EmployeeLogRecord(**item) for item in items],
            source="postgres",
            used_fallback=False,
            message=None,
        )
