"""
퇴근 후 표시되는 "오늘 요약" 카드 위젯.

다음 출근 시 자동 숨김. 메인 화면 상단에 conditional하게 표시.
"""
from __future__ import annotations
from PyQt5.QtWidgets import QFrame, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
from PyQt5.QtCore import Qt

from core.i18n import tr


class TodaySummaryCard(QFrame):
    """퇴근 처리 직후 표시되는 요약 카드."""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("today_summary_card")
        self.setStyleSheet("""
            QFrame#today_summary_card {
                background-color: rgba(81, 207, 102, 0.08);
                border: 1px solid rgba(81, 207, 102, 0.40);
                border-radius: 8px;
                padding: 6px;
            }
            QLabel { padding: 1px; background: transparent; border: none; }
        """)
        self.setVisible(False)

        layout = QVBoxLayout()
        layout.setContentsMargins(10, 6, 10, 6)
        layout.setSpacing(2)

        header = QHBoxLayout()
        title = QLabel(tr('today.title'))
        title.setStyleSheet("font-weight: bold; font-size: 13px;")
        header.addWidget(title)
        header.addStretch()
        close_btn = QPushButton("✕")
        close_btn.setFixedSize(20, 20)
        close_btn.setStyleSheet("border: none; font-weight: bold;")
        close_btn.clicked.connect(self.hide)
        header.addWidget(close_btn)
        layout.addLayout(header)

        self.total_label = QLabel("")
        self.detail_label = QLabel("")
        self.detail_label.setStyleSheet("color: #909296; font-size: 11px;")
        self.salary_label = QLabel("")
        self.salary_label.setStyleSheet("color: #51CF66; font-weight: bold;")

        layout.addWidget(self.total_label)
        layout.addWidget(self.detail_label)
        layout.addWidget(self.salary_label)

        self.setLayout(layout)

    def show_summary(self, total_hours: float, lunch_minutes: int,
                     break_minutes: int, overtime_actual: int,
                     overtime_earned: int, salary_text: str = "",
                     dinner_minutes: int = 0) -> None:
        """카드 내용 채우고 표시.

        Args:
            total_hours: 총 근무시간(시간)
            lunch_minutes: 점심 시간(분)
            break_minutes: 외출 시간(분, 식사 제외)
            overtime_actual: 실제 연장근무(분)
            overtime_earned: 적립 연장근무(분)
            salary_text: 추정 급여 표시 문자열 (옵션 활성 시)
            dinner_minutes: 저녁 시간(분), 0이면 표시 안 함
        """
        h = int(total_hours)
        m = int((total_hours - h) * 60)
        self.total_label.setText(tr('today.total_work', hours=h, minutes=m))

        details = []
        if lunch_minutes > 0:
            details.append(tr('today.detail_lunch', minutes=lunch_minutes))
        if dinner_minutes > 0:
            details.append(tr('today.detail_dinner', minutes=dinner_minutes))
        if break_minutes > 0:
            details.append(tr('today.detail_break', minutes=break_minutes))
        if overtime_actual > 0:
            details.append(tr('today.detail_overtime', actual=overtime_actual, earned=overtime_earned))
        self.detail_label.setText(" · ".join(details) if details else "")
        self.detail_label.setVisible(bool(details))

        if salary_text:
            self.salary_label.setText(f"{salary_text}")
            self.salary_label.setVisible(True)
        else:
            self.salary_label.setVisible(False)

        self.setVisible(True)
