import 'package:be_happy/presentation/components/gradient_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../../domain/entities/subscription.dart';
import '../event/subscription_list_event.dart';

class SubscriptionCard extends StatelessWidget {
  final Subscription subscription;
  final bool isActive;
  final DateTime? expiredAt;
  final VoidCallback? onRefresh;

  const SubscriptionCard({super.key, required this.subscription, required this.isActive, this.expiredAt, this.onRefresh});

  @override
  Widget build(BuildContext context) {
    final minPriceOption = subscription.options.isNotEmpty
        ? subscription.options.reduce((a, b) => a.price < b.price ? a : b)
        : null;

    /*final maxDaysOption = subscription.options.isNotEmpty
        ? subscription.options.reduce((a, b) => a.days > b.days ? a : b)
        : null;*/

    return Container(
      margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: const Color(0xFF0D143C),
        borderRadius: BorderRadius.circular(24),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: isActive ? MainAxisAlignment.spaceBetween : MainAxisAlignment.start,
            children: [
              Text(
                subscription.title,
                style: const TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
              ),
              if (isActive)
                Container(
                width: 100,
                alignment: Alignment.center,
                padding: EdgeInsets.all(4),
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.3),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Text(
                  "АКТИВНА",
                  style: TextStyle(
                    color:  Colors.greenAccent,
                    fontSize: 12,
                    fontWeight: FontWeight.bold,
                    letterSpacing: 0.5,
                  ),
                ),
              ),

            ]
          ),

          const SizedBox(height: 12),
          Text(
            subscription.shortDescription,
            style: TextStyle(color: Colors.white.withOpacity(0.7), fontSize: 14),
          ),
          Spacer(),
          if (isActive && expiredAt != null) ...[
            const SizedBox(height: 16),
            Builder(
              builder: (context) {
                final day = expiredAt!.day.toString().padLeft(2, '0');
                final month = expiredAt!.month.toString().padLeft(2, '0');
                final year = expiredAt!.year;

                return Text(
                  "Период действия: до $day.$month.$year",
                  style: const TextStyle(color: Colors.white, fontSize: 14),
                );
              },
            ),
          ],
          const SizedBox(height: 20),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if (minPriceOption != null)
                Text(
                  "от ${minPriceOption.pricePrint}",
                  style: const TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
                )
              else
                const SizedBox.shrink(),
              GradientButton(
                onTap: () async {
                  final isSubscribed = await context.push<bool>("/home/subscriptions/${subscription.id}");
                  if (isSubscribed == true && onRefresh != null) {
                    onRefresh!();
                  }
                },
                text: "Подробнее",
                enabled: true,
                width: 120,
                height: 40,
              ),
            ],
          ),
        ],
      ),
    );
  }
}