import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/main/registration_form_controller.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/step/viewer-information/personal_info_controller.dart';
import 'package:sigap/src/shared/widgets/form/form_section_header.dart';
import 'package:sigap/src/shared/widgets/navigation/step_navigation_buttons.dart';
import 'package:sigap/src/shared/widgets/text/custom_text_field.dart';
import 'package:sigap/src/utils/constants/sizes.dart';
import 'package:sigap/src/utils/validators/validation.dart';

class PersonalInfoStep extends StatelessWidget {
  const PersonalInfoStep({super.key});

  @override
  Widget build(BuildContext context) {
    final formKey = GlobalKey<FormState>();
    final controller = Get.find<PersonalInfoController>();
    final mainController = Get.find<FormRegistrationController>();
    mainController.formKey = formKey;

    return Form(
      key: formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const FormSectionHeader(
            title: 'Personal Information',
            subtitle: 'Please provide your personal details',
          ),

          const SizedBox(height: TSizes.spaceBtwItems),

          // First Name field
          Obx(
            () => CustomTextField(
              label: 'First Name',
              controller: controller.firstNameController,
              validator:
                  (value) =>
                      TValidators.validateUserInput('First name', value, 50),
              errorText: controller.firstNameError.value,
              textInputAction: TextInputAction.next,
              hintText: 'e.g., John',
              onChanged: (value) {
                controller.firstNameController.text = value;
                controller.firstNameError.value = '';
              },
            ),
          ),

          // Last Name field
          Obx(
            () => CustomTextField(
              label: 'Last Name',
              controller: controller.lastNameController,
              validator:
                  (value) => TValidators.validateUserInput(
                    'Last name',
                    value,
                    50,
                    required: true,
                  ),
              errorText: controller.lastNameError.value,
              textInputAction: TextInputAction.next,
              hintText: 'e.g., Doe',
              onChanged: (value) {
                controller.lastNameController.text = value;
                controller.lastNameError.value = '';
              },
            ),
          ),

          // Phone field
          Obx(
            () => CustomTextField(
              label: 'Phone Number',
              controller: controller.phoneController,
              validator: TValidators.validatePhoneNumber,
              errorText: controller.phoneError.value,
              keyboardType: TextInputType.phone,
              textInputAction: TextInputAction.next,
              hintText: 'e.g., 081234567890',
              onChanged: (value) {
                controller.phoneController.text = value;
                controller.phoneError.value = '';
              },
            ),
          ),

          // Address field
          Obx(
            () => CustomTextField(
              label: 'Address',
              controller: controller.addressController,
              validator:
                  (value) =>
                      TValidators.validateUserInput('Address', value, 255),
              errorText: controller.addressError.value,
              textInputAction: TextInputAction.done,
              maxLines: 3,
              hintText: 'e.g., 123 Main St, City, Country',
              onChanged: (value) {
                controller.addressController.text = value;
                controller.addressError.value = '';
              },
            ),
          ),

          // Bio field
          Obx(
            () => CustomTextField(
              label: 'Bio',
              controller: controller.bioController,
              validator:
                  (value) => TValidators.validateUserInput(
                    'Bio',
                    value,
                    255,
                    required: false,
                  ),
              errorText: controller.bioError.value,
              textInputAction: TextInputAction.next,
              maxLines: 3,
              hintText: 'Tell us a little about yourself (optional)',
              onChanged: (value) {
                controller.bioController.text = value;
                controller.bioError.value = '';
              },
            ),
          ),

          const SizedBox(height: TSizes.spaceBtwSections),

          // Navigation buttons
          Obx(
            () => StepNavigationButtons(
              showPrevious: mainController.currentStep.value > 0,
              isLastStep: false,
              onPrevious: mainController.previousStep,
              onNext: () => controller.handleNextStep(formKey, mainController),
              errorMessage: controller.personalInfoError.value,
              nextButtonText: 'Continue',
            )
          ),
        ],
      ),
    );
  }
}
