/// Tests unitaires pour les validators
library validators_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:unionflow_mobile_apps/core/validation/validators.dart';

void main() {
  group('Validators', () {
    group('required', () {
      test('should return error for null value', () {
        final validator = Validators.required();
        expect(validator(''), equals('Ce champ est requis'));
      });

      test('should return error for empty string', () {
        final validator = Validators.required();
        expect(validator(''), equals('Ce champ est requis'));
      });

      test('should return error for whitespace only', () {
        final validator = Validators.required();
        expect(validator('   '), equals('Ce champ est requis'));
      });

      test('should return null for valid value', () {
        final validator = Validators.required();
        expect(validator('value'), isNull);
      });

      test('should use custom message', () {
        final validator = Validators.required(message: 'Custom error');
        expect(validator(''), equals('Custom error'));
      });
    });

    group('minLength', () {
      test('should return error when value is too short', () {
        final validator = Validators.minLength(5);
        expect(validator('abc'), equals('Minimum 5 caractères requis'));
      });

      test('should return null when value meets minimum', () {
        final validator = Validators.minLength(5);
        expect(validator('abcde'), isNull);
      });

      test('should return null when value exceeds minimum', () {
        final validator = Validators.minLength(5);
        expect(validator('abcdefgh'), isNull);
      });

      test('should trim value before checking length', () {
        final validator = Validators.minLength(5);
        expect(validator('  abc  '), equals('Minimum 5 caractères requis'));
      });
    });

    group('maxLength', () {
      test('should return error when value is too long', () {
        final validator = Validators.maxLength(5);
        expect(validator('abcdefgh'), equals('Maximum 5 caractères autorisés'));
      });

      test('should return null when value meets maximum', () {
        final validator = Validators.maxLength(5);
        expect(validator('abcde'), isNull);
      });

      test('should return null when value is under maximum', () {
        final validator = Validators.maxLength(5);
        expect(validator('abc'), isNull);
      });
    });

    group('email', () {
      test('should return null for valid email', () {
        final validator = Validators.email();
        expect(validator('test@example.com'), isNull);
        expect(validator('user.name@domain.co.uk'), isNull);
        expect(validator('user+tag@example.com'), isNull);
      });

      test('should return error for invalid email', () {
        final validator = Validators.email();
        expect(validator('invalid'), equals('Adresse email invalide'));
        expect(validator('test@'), equals('Adresse email invalide'));
        expect(validator('@example.com'), equals('Adresse email invalide'));
        expect(validator('test @example.com'), equals('Adresse email invalide'));
      });

      test('should return null for empty value (use required separately)', () {
        final validator = Validators.email();
        expect(validator(''), isNull);
      });
    });

    group('numeric', () {
      test('should return null for valid numbers', () {
        final validator = Validators.numeric();
        expect(validator('123'), isNull);
        expect(validator('123.45'), isNull);
        expect(validator('-123'), isNull);
        expect(validator('0'), isNull);
      });

      test('should return error for non-numeric values', () {
        final validator = Validators.numeric();
        expect(validator('abc'), equals('Veuillez entrer un nombre valide'));
        expect(validator('12.34.56'), equals('Veuillez entrer un nombre valide'));
      });

      test('should return null for empty value', () {
        final validator = Validators.numeric();
        expect(validator(''), isNull);
      });
    });

    group('minValue', () {
      test('should return error when value is below minimum', () {
        final validator = Validators.minValue(10);
        expect(validator('5'), equals('La valeur doit être au moins 10.0'));
      });

      test('should return null when value meets minimum', () {
        final validator = Validators.minValue(10);
        expect(validator('10'), isNull);
      });

      test('should return null when value exceeds minimum', () {
        final validator = Validators.minValue(10);
        expect(validator('15'), isNull);
      });

      test('should work with decimals', () {
        final validator = Validators.minValue(10.5);
        expect(validator('10.4'), contains('au moins'));
        expect(validator('10.5'), isNull);
        expect(validator('10.6'), isNull);
      });
    });

    group('maxValue', () {
      test('should return error when value exceeds maximum', () {
        final validator = Validators.maxValue(100);
        expect(validator('150'), equals('La valeur doit être au maximum 100.0'));
      });

      test('should return null when value meets maximum', () {
        final validator = Validators.maxValue(100);
        expect(validator('100'), isNull);
      });

      test('should return null when value is below maximum', () {
        final validator = Validators.maxValue(100);
        expect(validator('50'), isNull);
      });
    });

    group('range', () {
      test('should return error when value is below range', () {
        final validator = Validators.range(10, 100);
        expect(validator('5'), contains('entre'));
      });

      test('should return error when value is above range', () {
        final validator = Validators.range(10, 100);
        expect(validator('150'), contains('entre'));
      });

      test('should return null when value is within range', () {
        final validator = Validators.range(10, 100);
        expect(validator('10'), isNull);
        expect(validator('50'), isNull);
        expect(validator('100'), isNull);
      });
    });

    group('phone', () {
      test('should return null for valid phone numbers', () {
        final validator = Validators.phone();
        expect(validator('+33612345678'), isNull);
        expect(validator('06 12 34 56 78'), isNull);
        expect(validator('(123) 456-7890'), isNull);
      });

      test('should return error for invalid phone numbers', () {
        final validator = Validators.phone();
        expect(validator('abc'), equals('Numéro de téléphone invalide'));
        expect(validator('123'), equals('Numéro de téléphone trop court'));
      });

      test('should return null for empty value', () {
        final validator = Validators.phone();
        expect(validator(''), isNull);
      });
    });

    group('pattern', () {
      test('should validate against custom regex', () {
        final validator = Validators.pattern(
          RegExp(r'^[A-Z]{3}\d{3}$'),
          message: 'Format: 3 lettres majuscules + 3 chiffres',
        );

        expect(validator('ABC123'), isNull);
        expect(validator('XYZ999'), isNull);
        expect(validator('abc123'), equals('Format: 3 lettres majuscules + 3 chiffres'));
        expect(validator('AB123'), equals('Format: 3 lettres majuscules + 3 chiffres'));
      });
    });

    group('match', () {
      test('should return error when values do not match', () {
        final validator = Validators.match('password123');
        expect(validator('password456'), equals('Les valeurs ne correspondent pas'));
      });

      test('should return null when values match', () {
        final validator = Validators.match('password123');
        expect(validator('password123'), isNull);
      });
    });

    group('composeValidators', () {
      test('should run all validators in sequence', () {
        final validator = composeValidators([
          Validators.required(),
          Validators.minLength(5),
          Validators.maxLength(10),
        ]);

        expect(validator(''), equals('Ce champ est requis'));
        expect(validator('abc'), equals('Minimum 5 caractères requis'));
        expect(validator('12345678901'), equals('Maximum 10 caractères autorisés'));
        expect(validator('valid'), isNull);
      });

      test('should stop at first error', () {
        final validator = composeValidators([
          Validators.required(),
          Validators.email(),
        ]);

        // Should fail on required, not reach email validator
        expect(validator(''), equals('Ce champ est requis'));
      });
    });
  });

  group('FinanceValidators', () {
    group('amount', () {
      test('should return null for valid amounts', () {
        final validator = FinanceValidators.amount();
        expect(validator('100'), isNull);
        expect(validator('100.50'), isNull);
        expect(validator('0.01'), isNull);
      });

      test('should return error for negative or zero amounts', () {
        final validator = FinanceValidators.amount();
        expect(validator('0'), equals('Le montant doit être positif'));
        expect(validator('-10'), equals('Le montant doit être positif'));
      });

      test('should return error for invalid numbers', () {
        final validator = FinanceValidators.amount();
        expect(validator('abc'), equals('Montant invalide'));
      });

      test('should enforce minimum amount', () {
        final validator = FinanceValidators.amount(min: 100);
        expect(validator('50'), equals('Le montant minimum est 100.0'));
        expect(validator('100'), isNull);
        expect(validator('150'), isNull);
      });

      test('should enforce maximum amount', () {
        final validator = FinanceValidators.amount(max: 1000);
        expect(validator('1500'), equals('Le montant maximum est 1000.0'));
        expect(validator('1000'), isNull);
        expect(validator('500'), isNull);
      });

      test('should enforce max 2 decimals', () {
        final validator = FinanceValidators.amount();
        expect(validator('100.123'), equals('Maximum 2 décimales autorisées'));
        expect(validator('100.12'), isNull);
        expect(validator('100.1'), isNull);
      });
    });

    group('budgetLineName', () {
      test('should require name', () {
        final validator = FinanceValidators.budgetLineName();
        expect(validator(''), contains('requis'));
      });

      test('should enforce min length', () {
        final validator = FinanceValidators.budgetLineName();
        expect(validator('ab'), contains('Minimum 3 caractères'));
      });

      test('should enforce max length', () {
        final validator = FinanceValidators.budgetLineName();
        final longName = 'a' * 101;
        expect(validator(longName), contains('Maximum 100 caractères'));
      });

      test('should accept valid names', () {
        final validator = FinanceValidators.budgetLineName();
        expect(validator('Cotisations'), isNull);
        expect(validator('Ligne budgétaire test'), isNull);
      });
    });

    group('rejectionReason', () {
      test('should require reason', () {
        final validator = FinanceValidators.rejectionReason();
        expect(validator(''), contains('requis'));
      });

      test('should enforce min length', () {
        final validator = FinanceValidators.rejectionReason();
        expect(validator('short'), contains('min 10 caractères'));
      });

      test('should enforce max length', () {
        final validator = FinanceValidators.rejectionReason();
        final longReason = 'a' * 501;
        expect(validator(longReason), contains('Maximum 500 caractères'));
      });

      test('should accept valid reasons', () {
        final validator = FinanceValidators.rejectionReason();
        expect(validator('Cette transaction ne respecte pas les règles'), isNull);
      });
    });

    group('fiscalYear', () {
      test('should require year', () {
        final validator = FinanceValidators.fiscalYear();
        expect(validator(''), contains('requis'));
      });

      test('should reject invalid year format', () {
        final validator = FinanceValidators.fiscalYear();
        expect(validator('abc'), equals('Année invalide'));
      });

      test('should enforce year range', () {
        final validator = FinanceValidators.fiscalYear();
        final currentYear = DateTime.now().year;

        expect(validator('${currentYear - 10}'), contains('doit être entre'));
        expect(validator('${currentYear + 15}'), contains('doit être entre'));
      });

      test('should accept valid years', () {
        final validator = FinanceValidators.fiscalYear();
        final currentYear = DateTime.now().year;

        expect(validator('$currentYear'), isNull);
        expect(validator('${currentYear + 1}'), isNull);
        expect(validator('${currentYear - 1}'), isNull);
      });
    });
  });
}
