import 'package:formz/formz.dart';

class Username extends FormzInput<String, String> {
  const Username.pure() : super.pure('');
  const Username.dirty([super.value = '']) : super.dirty();

  @override
  String? validator(String value) {
    if (value.isEmpty) return '请输入用户名';
    if (value.length < 3) return '用户名至少 3 个字符';
    if (value.length > 30) return '用户名最多 30 个字符';
    return null;
  }
}

class Email extends FormzInput<String, String> {
  const Email.pure() : super.pure('');
  const Email.dirty([super.value = '']) : super.dirty();

  static final _regex = RegExp(r'^[\w.-]+@[\w.-]+\.\w+$');

  @override
  String? validator(String value) {
    if (value.isEmpty) return '请输入邮箱';
    if (!_regex.hasMatch(value)) return '邮箱格式不正确';
    return null;
  }
}

class Password extends FormzInput<String, String> {
  const Password.pure() : super.pure('');
  const Password.dirty([super.value = '']) : super.dirty();

  @override
  String? validator(String value) {
    if (value.isEmpty) return '请输入密码';
    if (value.length < 6) return '密码至少 6 个字符';
    return null;
  }
}

class VerificationCode extends FormzInput<String, String> {
  const VerificationCode.pure() : super.pure('');
  const VerificationCode.dirty([super.value = '']) : super.dirty();

  @override
  String? validator(String value) {
    if (value.isEmpty) return '请输入验证码';
    if (!RegExp(r'^\d{6}$').hasMatch(value)) return '验证码必须是 6 位数字';
    return null;
  }
}
