"""Review form (Trust System — Phase 2).

Server-side validation for the "Leave a Review" page. Rating is a required 1–5
choice; the comment is optional and length-bounded to match the model.
"""

from __future__ import annotations

from flask_wtf import FlaskForm
from wtforms import RadioField, SubmitField, TextAreaField
from wtforms.validators import DataRequired, Length, Optional

from app.models.review import MAX_COMMENT_LENGTH


class ReviewForm(FlaskForm):
    """Leave a rating (and optional comment) for the other party in a sale."""

    # Choices are highest-first so the pure-CSS star widget (rendered row-reverse)
    # displays 1–5 left to right.
    rating = RadioField(
        "Rating",
        coerce=int,
        choices=[(5, "5"), (4, "4"), (3, "3"), (2, "2"), (1, "1")],
        validators=[DataRequired(message="Please choose a rating from 1 to 5.")],
    )
    comment = TextAreaField(
        "Comment (optional)",
        validators=[
            Optional(),
            Length(max=MAX_COMMENT_LENGTH, message=f"Keep your comment under {MAX_COMMENT_LENGTH} characters."),
        ],
        render_kw={"rows": 4, "placeholder": "Share how the exchange went (optional)."},
    )
    submit = SubmitField("Submit Review")
