# app/schemas/group.py
from pydantic import BaseModel, ConfigDict, computed_field
from datetime import datetime, date
from typing import Optional, List

from .user import UserPublic # Import UserPublic to represent members
from .chore import ChoreHistoryPublic # Import for history

# Properties to receive via API on creation
class GroupCreate(BaseModel):
    name: str

# New schema for generating a schedule
class GroupScheduleGenerateRequest(BaseModel):
    start_date: date
    end_date: date
    member_ids: Optional[List[int]] = None # Optional: if not provided, use all members

# Properties to return to client
class GroupPublic(BaseModel):
    id: int
    name: str
    created_by_id: int
    created_at: datetime
    member_associations: Optional[List["UserGroupPublic"]] = None
    chore_history: Optional[List[ChoreHistoryPublic]] = []

    @computed_field
    @property
    def members(self) -> Optional[List[UserPublic]]:
        if not self.member_associations:
            return None
        return [assoc.user for assoc in self.member_associations if assoc.user]

    model_config = ConfigDict(from_attributes=True)

# Properties for UserGroup association
class UserGroupPublic(BaseModel):
    id: int
    user_id: int
    group_id: int
    role: str
    joined_at: datetime
    user: Optional[UserPublic] = None

    model_config = ConfigDict(from_attributes=True)

# Properties stored in DB (if needed, often GroupPublic is sufficient)
# class GroupInDB(GroupPublic):
#     pass

# We need to rebuild GroupPublic to resolve the forward reference to UserGroupPublic
GroupPublic.model_rebuild()