"""
점심/저녁 토글 컨트롤러.

main_window.py에서 toggle_lunch_break / toggle_dinner_break / update_lunch_status /
update_dinner_status 가 합쳐져 있던 것을 분리. 1Hz hot path 외 사용자 액션 응답.

단위 테스트가 가능하도록 window 의존성을 명시적으로 받음.
"""
from __future__ import annotations
from datetime import datetime

from core.i18n import tr


class MealController:
    """점심/저녁 토글 + 상태 라벨 갱신."""

    def __init__(self, window):
        self.window = window
        self.db = window.db

    # -------- 토글 --------
    def toggle_lunch(self) -> None:
        w = self.window
        w.lunch_break_enabled = w.lunch_button.isChecked()
        self.refresh_lunch_label()

        # 사용자가 직접 토글하면 자동 적용 플래그를 처리됨으로 간주 (중복 알림 방지)
        if w.lunch_break_enabled:
            w.auto_lunch_applied_today = True

        if w.is_clocked_in:
            today = datetime.now().date().isoformat()
            self.db.update_lunch_break(today, w.lunch_break_enabled)

    def toggle_dinner(self) -> None:
        w = self.window
        w.dinner_break_enabled = w.dinner_button.isChecked()
        self.refresh_dinner_label()

        if w.is_clocked_in:
            today = datetime.now().date().isoformat()
            self.db.update_dinner_break(today, w.dinner_break_enabled)

    # -------- 라벨 --------
    def refresh_lunch_label(self) -> None:
        w = self.window
        w.lunch_button.setText(
            tr('btn.lunch_applied') if w.lunch_break_enabled else tr('btn.lunch_add')
        )

    def refresh_dinner_label(self) -> None:
        w = self.window
        w.dinner_button.setText(
            tr('btn.dinner_applied') if w.dinner_break_enabled else tr('btn.dinner_add')
        )
