from datetime import datetime
from pydantic import BaseModel, Field

from app.schemas.user import UserPublic
from app.schemas.challenge import ChallengeResponse


class DisputeCreate(BaseModel):
    """Request to create a dispute"""
    reason: str = Field(..., min_length=10, max_length=1000)


class DisputeCommentCreate(BaseModel):
    """Request to add a comment to a dispute"""
    text: str = Field(..., min_length=1, max_length=500)


class DisputeVoteCreate(BaseModel):
    """Request to vote on a dispute"""
    vote: bool  # True = valid (proof is OK), False = invalid (proof is not OK)


class DisputeCommentResponse(BaseModel):
    """Comment in a dispute discussion"""
    id: int
    user: UserPublic
    text: str
    created_at: datetime

    class Config:
        from_attributes = True


class DisputeVoteResponse(BaseModel):
    """Vote in a dispute"""
    user: UserPublic
    vote: bool  # True = valid, False = invalid
    created_at: datetime

    class Config:
        from_attributes = True


class DisputeResponse(BaseModel):
    """Full dispute information"""
    id: int
    raised_by: UserPublic
    reason: str
    status: str  # "open", "valid", "invalid"
    comments: list[DisputeCommentResponse]
    votes: list[DisputeVoteResponse]
    votes_valid: int
    votes_invalid: int
    my_vote: bool | None  # Current user's vote, None if not voted
    expires_at: datetime
    created_at: datetime
    resolved_at: datetime | None

    class Config:
        from_attributes = True


class AssignmentDetailResponse(BaseModel):
    """Detailed assignment information with proofs and dispute"""
    id: int
    challenge: ChallengeResponse
    participant: UserPublic
    status: str
    proof_url: str | None  # External URL (YouTube, etc.)
    proof_image_url: str | None  # Uploaded file URL
    proof_comment: str | None
    points_earned: int
    streak_at_completion: int | None
    started_at: datetime
    completed_at: datetime | None
    can_dispute: bool  # True if <24h since completion and not own assignment
    dispute: DisputeResponse | None

    class Config:
        from_attributes = True


class ReturnedAssignmentResponse(BaseModel):
    """Returned assignment that needs to be redone"""
    id: int
    challenge: ChallengeResponse
    original_completed_at: datetime
    dispute_reason: str

    class Config:
        from_attributes = True
