import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../controllers/detail_screen_controller.dart';
import '../../providers/category_provider.dart';
import '../common/form_fields/base_text_field.dart';
import '../common/form_fields/currency_input_field.dart';
import '../common/form_fields/date_picker_field.dart';
// import '../common/form_fields/currency_selector.dart';
import '../common/form_fields/currency_dropdown_field.dart';
import '../common/form_fields/billing_cycle_selector.dart';
import '../common/form_fields/category_selector.dart';
import '../payment_card/payment_card_selector.dart';
import '../payment_card/payment_card_form_sheet.dart';
import '../../routes/app_routes.dart';
import '../../l10n/app_localizations.dart';

/// 상세 화면 폼 섹션
/// 구독 정보를 편집할 수 있는 폼 필드들을 포함합니다.
class DetailFormSection extends StatelessWidget {
  final DetailScreenController controller;
  final Animation<double> fadeAnimation;
  final Animation<Offset> slideAnimation;

  const DetailFormSection({
    super.key,
    required this.controller,
    required this.fadeAnimation,
    required this.slideAnimation,
  });

  @override
  Widget build(BuildContext context) {
    return Consumer<DetailScreenController>(
      builder: (context, controller, child) {
        final baseColor = controller.getCardColor();

        return FadeTransition(
          opacity: fadeAnimation,
          child: SlideTransition(
            position: Tween<Offset>(
              begin: const Offset(0.0, 0.6),
              end: Offset.zero,
            ).animate(CurvedAnimation(
              parent: controller.animationController!,
              curve: const Interval(0.3, 1.0, curve: Curves.easeOutCubic),
            )),
            child: Container(
              decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.surface,
                borderRadius: BorderRadius.circular(20),
                border: Border.all(
                  color: Theme.of(context)
                      .colorScheme
                      .outline
                      .withValues(alpha: 0.3),
                  width: 1,
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(24),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    // 서비스명 필드
                    BaseTextField(
                      controller: controller.serviceNameController,
                      focusNode: controller.serviceNameFocus,
                      label: AppLocalizations.of(context).subscriptionName,
                      hintText: AppLocalizations.of(context).serviceNameExample,
                      textInputAction: TextInputAction.next,
                      onEditingComplete: () {
                        controller.monthlyCostFocus.requestFocus();
                      },
                    ),
                    const SizedBox(height: 20),

                    // 월 지출 및 통화 선택
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 2,
                          child: CurrencyInputField(
                            controller: controller.monthlyCostController,
                            currency: controller.currency,
                            label: AppLocalizations.of(context).monthlyExpense,
                            focusNode: controller.monthlyCostFocus,
                            textInputAction: TextInputAction.next,
                            onEditingComplete: () {
                              controller.billingCycleFocus.requestFocus();
                            },
                          ),
                        ),
                        const SizedBox(width: 12),
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                AppLocalizations.of(context).currency,
                                style: TextStyle(
                                  fontSize: 16,
                                  fontWeight: FontWeight.w600,
                                  color:
                                      Theme.of(context).colorScheme.onSurface,
                                ),
                              ),
                              const SizedBox(height: 8),
                              CurrencyDropdownField(
                                currency: controller.currency,
                                onChanged: (value) {
                                  controller.currency = value;
                                },
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),

                    // 결제 주기
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          AppLocalizations.of(context).billingCycle,
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w600,
                            color: Theme.of(context).colorScheme.onSurface,
                          ),
                        ),
                        const SizedBox(height: 8),
                        BillingCycleSelector(
                          billingCycle: controller.billingCycle,
                          baseColor: baseColor,
                          onChanged: (value) {
                            controller.billingCycle = value;
                          },
                        ),
                      ],
                    ),
                    const SizedBox(height: 20),

                    // 다음 결제일
                    DatePickerField(
                      selectedDate: controller.nextBillingDate,
                      onDateSelected: (date) {
                        controller.nextBillingDate = date;
                      },
                      label: AppLocalizations.of(context).nextBillingDate,
                      // 과거 결제일을 가진 항목도 수정 가능하도록 범위 완화
                      firstDate: DateTime.now()
                          .subtract(const Duration(days: 365 * 10)),
                      lastDate:
                          DateTime.now().add(const Duration(days: 365 * 2)),
                      primaryColor: baseColor,
                    ),
                    const SizedBox(height: 20),

                    // 카테고리 선택
                    Consumer<CategoryProvider>(
                      builder: (context, categoryProvider, child) {
                        return Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              AppLocalizations.of(context).category,
                              style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.w600,
                                color: Theme.of(context).colorScheme.onSurface,
                              ),
                            ),
                            const SizedBox(height: 8),
                            CategorySelector(
                              categories: categoryProvider.categories,
                              selectedCategoryId: controller.selectedCategoryId,
                              baseColor: baseColor,
                              onChanged: (categoryId) {
                                controller.selectedCategoryId = categoryId;
                              },
                            ),
                          ],
                        );
                      },
                    ),
                    const SizedBox(height: 20),
                    Text(
                      AppLocalizations.of(context).paymentCard,
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: Theme.of(context).colorScheme.onSurface,
                      ),
                    ),
                    const SizedBox(height: 8),
                    PaymentCardSelector(
                      selectedCardId: controller.selectedPaymentCardId,
                      onChanged: (cardId) {
                        controller.selectedPaymentCardId = cardId;
                      },
                      onAddCard: () async {
                        final newCardId =
                            await PaymentCardFormSheet.show(context);
                        if (newCardId != null) {
                          controller.selectedPaymentCardId = newCardId;
                        }
                      },
                      onManageCards: () {
                        Navigator.of(context)
                            .pushNamed(AppRoutes.paymentCardManagement);
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
