import 'package:english_learning/features/history/models/history_model.dart';
import 'package:english_learning/features/history/provider/history_provider.dart';
import 'package:english_learning/core/utils/styles/theme.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ExerciseHistoryCard extends StatelessWidget {
  final LearningHistory exercise;

  const ExerciseHistoryCard({
    super.key,
    required this.exercise,
  });

  @override
  Widget build(BuildContext context) {
    final historyProvider =
        Provider.of<HistoryProvider>(context, listen: false);
    final color = historyProvider.getColorForLevels(
      exercise.currentLevel,
      exercise.nextLevel,
      exercise.isPass,
    );

    return Card(
      color: AppColors.whiteColor,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.0),
      ),
      elevation: 0,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 18.0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    exercise.topicName,
                    overflow: TextOverflow.ellipsis,
                    style: AppTextStyles.tetriaryTextStyle.copyWith(
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  const SizedBox(height: 8),
                  RichText(
                    text: TextSpan(
                      style: AppTextStyles.greyTextStyle.copyWith(
                        fontSize: 14,
                        fontWeight: FontWeight.w600,
                      ),
                      children: [
                        TextSpan(
                          text: '${exercise.currentLevel}  →  ',
                          style: AppTextStyles.blackTextStyle.copyWith(),
                        ),
                        TextSpan(
                          text: exercise.isPass
                              ? 'Topic Finished'
                              : '${exercise.nextLevel}',
                          style: AppTextStyles.blackTextStyle.copyWith(
                            color: color,
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    'Submission: ${exercise.formattedDate}',
                    style: AppTextStyles.disableTextStyle.copyWith(
                      fontSize: 12,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(width: 10),
            Container(
              padding: const EdgeInsets.symmetric(
                horizontal: 16.0,
                vertical: 26.0,
              ),
              decoration: BoxDecoration(
                border: Border.all(
                  color: color,
                ),
                borderRadius: BorderRadius.circular(8.0),
              ),
              child: Text(
                '${exercise.score}/100',
                style: TextStyle(
                  color: color,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
