from pydantic import BaseModel, Field, field_validator
from typing import List, Optional
from bson import ObjectId
import bson
from datetime import datetime

class PinDTO(BaseModel):
    title: str
    description: str
    location: list
    complete_address: str
    files: List[str] = Field(default_factory=list)
    is_poi: bool = False
    user_id: Optional[str] = None
    date: Optional[datetime] = None

    @field_validator('files')
    @classmethod
    def validate_files(cls, files):
        for file_id in files:
            try:
                ObjectId(file_id)
            except bson.errors.InvalidId:
                raise ValueError(f"Invalid image ID format: {file_id}")
        return files
    
    @field_validator('user_id')
    @classmethod
    def validate_user_id(cls, v, info):
        if not info.data.get('is_poi') and not v:
            raise ValueError('user_id is required when is_poi is False ' + str(info.data))
        return v
    
class PinShareDTO(BaseModel):
    friend_id: str 