from pydantic import BaseModel, ConfigDict, computed_field
from datetime import datetime, date
from typing import Optional, List   
from .user import UserPublic
from .chore import ChoreHistoryPublic

class GroupCreate(BaseModel):
    name: str

class GroupDelete(BaseModel):
    confirmation_name: str

class GroupScheduleGenerateRequest(BaseModel):
    start_date: date
    end_date: date
    member_ids: Optional[List[int]] = None

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)

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)

GroupPublic.model_rebuild()