import 'package:auth/src/features/sign_up/models/sign_up_step_config.dart';
import 'package:auth/src/features/sign_up/presentation/state/sign_up_view_model.dart';
import 'package:auth/src/features/sign_up/presentation/screens/sign_up_address_screen.dart';
import 'package:auth/src/features/sign_up/presentation/screens/sign_up_password_screen.dart';
import 'package:auth/src/features/sign_up/presentation/screens/sign_up_personal_screen.dart';
import 'package:country_code_picker/country_code_picker.dart';
import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:sf_localizations/sf_localizations.dart';

const Map<String, String> documentType = <String, String>{
  'DNI': I18n.documentTypeDni,
  'NIE': I18n.documentTypeNie,
  'PASSPORT': I18n.documentTypePassport,
};
const Map<String, String> relationship = <String, String>{
  'FATHER': I18n.relationshipFather,
  'MOTHER': I18n.relationshipMother,
  'OTHER': I18n.relationshipTutor,
};

List<SignUpStepConfig> signUpSteps(BuildContext context) => [
  SignUpStepConfig(
    supertitle: context.translate(I18n.stepUserContactSupertitle),
    title: context.translate(I18n.stepUserContactTitle),
    subtitle: context.translate(I18n.stepUserContactSubtitle),
    bodyBuilder: (context, ref) {
      final theme = ref.watch(themePortProvider);
      final vm = ref.read(signUpViewModelProvider.notifier);
      final state = ref.watch(signUpViewModelProvider);
      final documentLabel = <String, String>{
        for (final e in documentType.entries) e.key: context.translate(e.value),
      };

      final documentKeys = <String, String>{
        for (final e in documentLabel.entries) e.value: e.key,
      };

      final String? documentTypeSelected = state.documentType.trim().isEmpty
          ? null
          : documentLabel[state.documentType];
      return SignupPersonalScreen(
        firstNameTextFieldController: vm.firstNameController,
        lastNameTextFieldController: vm.lastNameController,
        documentNumberTextFieldController: vm.documentNumberController,
        phoneTextFieldController: vm.phoneController,
        emailTextFieldController: vm.emailController,

        documentTypeSelected: documentTypeSelected,

        onDocumentTypeChanged: (label) {
          final documentKey = documentKeys[label ?? ''] ?? '';
          vm.setDocumentType(documentKey);
        },

        acceptTerms: state.acceptTerms,
        onAcceptTermsPressed: (v) => vm.setAcceptTerms(v ?? false),
        termsText: context.translate(I18n.termsText),
        theme: theme,

        firstNameLabel: context.translate(I18n.firstNameLabel),
        firstNameHint: context.translate(I18n.firstNameHint),
        lastNameLabel: context.translate(I18n.lastNameLabel),
        lastNameHint: context.translate(I18n.lastNameHint),

        documentTypeOptions: documentLabel.values.toList(),
        documentTypeHint: context.translate(I18n.documentTypeHint),

        documentNumberLabel: '',
        documentNumberHint: context.translate(I18n.documentNumberHint),
        phoneLabel: context.translate(I18n.phoneLabel),
        phoneHint: context.translate(I18n.phoneHint),
        emailLabel: context.translate(I18n.emailLabel),
        emailHint: context.translate(I18n.emailHint),

        onDialCodeChanged: (CountryCode value) {
          vm.updateDialCode(value.dialCode ?? state.dialCode);
        },
      );
    },
  ),
  SignUpStepConfig(
    supertitle: context.translate(I18n.stepPersonalDataSupertitle),
    title: context.translate(I18n.stepPersonalDataTitle),
    subtitle: context.translate(I18n.stepPersonalDataSubtitle),
    bodyBuilder: (context, ref) {
      final vm = ref.read(signUpViewModelProvider.notifier);
      final state = ref.watch(signUpViewModelProvider);

      final relationshipLabel = <String, String>{
        for (final e in relationship.entries) e.key: context.translate(e.value),
      };
      final relationshipKeys = <String, String>{
        for (final e in relationshipLabel.entries) e.value: e.key,
      };
      final String? relationshipSelected = state.relationship.trim().isEmpty
          ? null
          : relationshipLabel[state.relationship];

      return SignupAddressScreen(
        bornAtController: vm.bornAtController,
        onPickBornAt: () => vm.pickBornAt(context),

        relationshipSelected: relationshipSelected,
        onRelationshipChanged: (label) {
          final key = relationshipKeys[label ?? ''] ?? '';
          vm.setRelationship(key);
        },
        relationshipOptions: relationshipLabel.values.toList(),
        relationshipHint: context.translate(I18n.relationshipHint),
        relationshipLabel: context.translate(I18n.relationshipLabel),

        placeOfBirthController: vm.placeOfBirthController,
        birthCountryController: vm.birthCountryController,

        streetController: vm.addressStreetController,
        cityController: vm.addressCityController,
        provinceController: vm.addressProvinceController,
        stateController: vm.addressStateController,

        addressCountrySelected: state.address.country.trim().isEmpty
            ? null
            : state.address.country,
        onAddressCountryChanged: (CountryCode value) {
          vm.setAddressCountry(name: value.name ?? '');
        },
        addressCountryLabel: context.translate(I18n.addressCountryLabel),
        addressCountryController: vm.addressCountryController,

        postCodeController: vm.addressPostCodeController,

        birthDateLabel: context.translate(I18n.birthDateLabel),
        birthDateHint: context.translate(I18n.birthDateHint),

        placeOfBirthLabel: context.translate(I18n.placeOfBirthLabel),
        placeOfBirthHint: context.translate(I18n.placeOfBirthHint),

        birthCountryLabel: context.translate(I18n.birthCountryLabel),
        birthCountryHint: context.translate(I18n.birthCountryHint),
        onBirthCountryChanged: (CountryCode value) {
          vm.setBirthCountryFromPicker(
            name: value.name ?? '',
            code: value.code ?? '',
          );
        },

        streetLabel: context.translate(I18n.streetLabel),
        streetHint: context.translate(I18n.streetHint),

        cityLabel: context.translate(I18n.cityLabel),
        cityHint: context.translate(I18n.cityHint),

        provinceLabel: context.translate(I18n.provinceLabel),
        provinceHint: context.translate(I18n.provinceHint),

        stateLabel: context.translate(I18n.stateLabel),
        stateHint: context.translate(I18n.stateHint),

        postCodeLabel: context.translate(I18n.postCodeLabel),
        postCodeHint: context.translate(I18n.postCodeHint),
      );
    },
  ),
  SignUpStepConfig(
    supertitle: context.translate(I18n.stepAddressSupertitle),
    title: context.translate(I18n.stepAddressTitle),
    subtitle: context.translate(I18n.passwordRulesSubtitle),
    bodyBuilder: (context, ref) {
      final vm = ref.read(signUpViewModelProvider.notifier);
      final state = ref.watch(signUpViewModelProvider);

      return SignUpPasswordScreen(
        isPasswordVisible: state.isShowPassword,
        passwordTextFieldController: vm.passwordController,
        repeatPasswordTextFieldController: vm.repeatPasswordController,
      );
    },
  ),
];
