import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/features/profile/benefits/domain/entities/benefit_entity.dart';
import 'package:krow/features/profile/benefits/domain/entities/benefit_record_entity.dart';

class BenefitHistoryWidget extends StatelessWidget {
  const BenefitHistoryWidget({super.key, required this.benefit});

  final BenefitEntity benefit;

  @override
  Widget build(BuildContext context) {
    return ExpansionTile(
      tilePadding: const EdgeInsets.symmetric(horizontal: 12),
      childrenPadding: const EdgeInsets.symmetric(horizontal: 12),
      dense: true,
      visualDensity: VisualDensity.compact,
      collapsedShape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      backgroundColor: AppColors.graySecondaryFrame,
      collapsedBackgroundColor: AppColors.graySecondaryFrame,
      iconColor: AppColors.blackBlack,
      collapsedIconColor: AppColors.blackBlack,
      title: Text(
        '${benefit.name} ${'history'.tr()}'.toUpperCase(),
        style: AppTextStyles.captionBold,
      ),
      children: [
        const Divider(
          thickness: 1,
          color: AppColors.tintGray,
        ),
        const SizedBox(height: 12),
        if (benefit.history.isEmpty)
           SizedBox(
            height: 80,
            child: Center(
              child: Text('no_history_yet'.tr()),
            ),
          )
        else
          SizedBox(
            height: 168,
            child: RawScrollbar(
              padding: EdgeInsets.zero,
              thumbVisibility: true,
              trackVisibility: true,
              thumbColor: AppColors.grayStroke,
              trackColor: AppColors.grayTintStroke,
              trackRadius: const Radius.circular(8),
              radius: const Radius.circular(8),
              trackBorderColor: Colors.transparent,
              thickness: 5,
              minOverscrollLength: 0,
              child: ListView.separated(
                padding: const EdgeInsetsDirectional.only(end: 10),
                itemCount: benefit.history.length,
                separatorBuilder: (context, index) =>
                    const SizedBox(height: 12),
                itemBuilder: (context, index) {
                  return _HistoryRecordWidget(record: benefit.history[index]);
                },
              ),
            ),
          ),
        const SizedBox(height: 12),
      ],
    );
  }
}

class _HistoryRecordWidget extends StatelessWidget {
  const _HistoryRecordWidget({required this.record});

  static final _dateFormat = DateFormat('d MMM, yyyy');

  final BenefitRecordEntity record;

  @override
  Widget build(BuildContext context) {
    final Color color = switch (record.status) {
      RecordStatus.pending => AppColors.primaryBlue,
      RecordStatus.submitted => AppColors.statusSuccess,
    };

    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          _dateFormat.format(record.createdAt),
          style: AppTextStyles.bodySmallReg.copyWith(
            color: AppColors.blackGray,
          ),
        ),
        DecoratedBox(
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(24)),
            border: Border.all(color: color),
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
            child: Text(
              switch (record.status) {
                RecordStatus.pending => 'pending'.tr(),
                RecordStatus.submitted => 'submitted'.tr(),
              },
              style: AppTextStyles.bodySmallReg.copyWith(
                color: color,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
