from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, PasswordField, TextAreaField, SelectField, SubmitField
from wtforms.validators import DataRequired, NumberRange, Optional, Length


class DeviceForm(FlaskForm):
    name = StringField("Name", validators=[DataRequired(), Length(max=100)])
    hostname = StringField("Hostname / IP", validators=[DataRequired(), Length(max=255)])
    port = IntegerField("SSH Port", default=22, validators=[DataRequired(), NumberRange(min=1, max=65535)])
    username = StringField("Username", validators=[DataRequired(), Length(max=100)])
    auth_type = SelectField(
        "Authentication",
        choices=[("password", "Password"), ("sshkey", "SSH Private Key")],
        default="password",
    )
    password = PasswordField(
        "Password",
        validators=[Optional()],
        description="Leave blank to keep the existing password when editing.",
    )
    private_key = TextAreaField(
        "SSH Private Key (PEM text)",
        validators=[Optional()],
        description="Leave blank to keep the existing key when editing.",
    )
    key_passphrase = PasswordField("Key Passphrase (if any)", validators=[Optional()])
    site = StringField("Site / Group", validators=[Optional(), Length(max=100)])
    notes = TextAreaField("Notes", validators=[Optional()])
    submit = SubmitField("Save")
