from __future__ import annotations import re from src.domain import ChangeRequest, UNAVAILABLE_DATE, UNAVAILABLE_EMPLOYEE _EMPLOYEE = re.compile(r"(?:e|emp|employee|نفر|کارمند)\s*[-_ ]?(\d{1,2})", re.I) _DATE = re.compile(r"(20\d{2}-\d{2}-\d{2})") _WEEKDAY = { "monday": "Monday", "tuesday": "Tuesday", "wednesday": "Wednesday", "thursday": "Thursday", "friday": "Friday", "saturday": "Saturday", "sunday": "Sunday", "دوشنبه": "Monday", "سه شنبه": "Tuesday", "سه‌شنبه": "Tuesday", "سهشنبه": "Tuesday", "چهارشنبه": "Wednesday", "پنجشنبه": "Thursday", "پنج‌شنبه": "Thursday", "جمعه": "Friday", "شنبه": "Saturday", "یکشنبه": "Sunday", "یک‌شنبه": "Sunday", } _SHIFT = { "morning": "morning", "evening": "evening", "night": "night", "صبح": "morning", "عصر": "evening", "شب": "night", } def _employee_id(text: str) -> str | None: match = _EMPLOYEE.search(text) if match: return f"E-{int(match.group(1)):02d}" if "leila" in text.lower() or "لیلا" in text: return UNAVAILABLE_EMPLOYEE return None def _weekday(text: str) -> str | None: lowered = text.lower() for key, value in sorted(_WEEKDAY.items(), key=lambda item: len(item[0]), reverse=True): if key in lowered or key in text: return value return None def _shift_id(text: str) -> str | None: lowered = text.lower() for key, value in _SHIFT.items(): if re.search(rf"\b{re.escape(key)}\b", lowered) or key in text: return value return None def parse_change(text: str) -> ChangeRequest | None: raw = (text or "").strip() if not raw: return None lowered = raw.lower() employee_id = _employee_id(raw) date_match = _DATE.search(raw) day = date_match.group(1) if date_match else None weekday = _weekday(raw) shift_id = _shift_id(raw) if any(word in lowered or word in raw for word in ("not available", "unavailable", "leave", "off", "در دسترس نیست", "مرخصی", "نباشد")): if not employee_id: return ChangeRequest(action="unknown", note="Availability change needs an employee id. Example: E-07 is not available Tuesday.") if not day and not weekday: day = UNAVAILABLE_DATE weekday = "Tuesday" note = f"Mark {employee_id} unavailable" if day: note += f" on {day}" elif weekday: note += f" on every {weekday} in the horizon" note += ". Confirm before solving." return ChangeRequest(action="unavailable", employee_id=employee_id, date=day, weekday=weekday, note=note) if any(word in lowered or word in raw for word in ("cannot work night", "no night", "شب کار نمی", "شب کار نمی‌")): if not employee_id: return ChangeRequest(action="unknown", note="Night ban needs an employee id. Example: E-12 cannot work night.") return ChangeRequest(action="no_night", employee_id=employee_id, note=f"Set night_ok=0 for {employee_id}. Confirm before solving.") if any(word in lowered or word in raw for word in ("lock", "قفل")): if not employee_id or not shift_id or not (day or weekday): return ChangeRequest(action="unknown", note="Lock needs employee, day and shift. Example: lock E-03 Wednesday night.") return ChangeRequest( action="lock", employee_id=employee_id, date=day, weekday=weekday, shift_id=shift_id, note=f"Lock {employee_id} to {shift_id} on {day or weekday}. Confirm before solving.", ) return ChangeRequest(action="unknown", note="Supported intents: 'E-07 is not available Tuesday', 'E-12 cannot work night', 'lock E-03 Wednesday night'.")