import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/auth/data/models/administrative_division.dart';
import 'package:sigap/src/features/auth/presentasion/controllers/signup/step/identity-verification/identity_verification_controller.dart';
import 'package:sigap/src/features/auth/presentasion/pages/signup/step/identity-verification/widgets/city_selection.dart';
import 'package:sigap/src/utils/constants/colors.dart';
import 'package:sigap/src/utils/constants/sizes.dart';

class PlaceOfBirthField extends StatelessWidget {
  final IdentityVerificationController controller;

  const PlaceOfBirthField({super.key, required this.controller});

  @override
  Widget build(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return Obx(
      () => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('Place of Birth', style: Theme.of(context).textTheme.bodyMedium),
          const SizedBox(height: TSizes.xs),
          GestureDetector(
            onTap: () => _navigateToLocationSelection(context),
            child: Container(
              padding: const EdgeInsets.symmetric(
                horizontal: TSizes.md,
                vertical: TSizes.md,
              ),
              decoration: BoxDecoration(
                border: Border.all(
                  color:
                      controller.placeOfBirthError.value.isNotEmpty
                          ? TColors.error
                          : isDark
                          ? TColors.darkGrey
                          : TColors.grey,
                ),
                borderRadius: BorderRadius.circular(TSizes.borderRadiusLg),
                color: Colors.transparent,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: Text(
                      controller.placeOfBirthController.text.isEmpty
                          ? 'Select Place of Birth'
                          : controller.placeOfBirthController.text,
                      style: TextStyle(
                        color:
                            controller.placeOfBirthController.text.isEmpty
                                ? Theme.of(context).hintColor
                                : Theme.of(context).textTheme.bodyMedium?.color,
                        overflow: TextOverflow.ellipsis,
                      ),
                      maxLines: 1,
                    ),
                  ),
                  Icon(
                    Icons.place,
                    size: TSizes.iconSm,
                    color: isDark ? TColors.white : TColors.dark,
                  ),
                ],
              ),
            ),
          ),
          if (controller.placeOfBirthError.value.isNotEmpty)
            Padding(
              padding: const EdgeInsets.only(top: TSizes.xs, left: TSizes.xs),
              child: Text(
                controller.placeOfBirthError.value,
                style: TextStyle(
                  color: TColors.error,
                  fontSize: 12,
                ),
              ),
            ),
          const SizedBox(height: TSizes.spaceBtwItems),
        ],
      ),
    );
  }

  void _navigateToLocationSelection(BuildContext context) async {
    final result = await Get.to<String>(
      () => LocationSelectionPage(
        initialDivisionType: DivisionType.province,
        breadcrumbs: [],
      ),
    );
    if (result != null && result.isNotEmpty) {
      controller.placeOfBirthController.text = result;
      controller.placeOfBirthError.value = '';
    }
  }
}
