import 'package:flutter/material.dart';
import 'package:line_icons/line_icons.dart';

import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../domain/analytic/analytic.dart';

class PurchaseDailyTile extends StatelessWidget {
  final PurchasingAnalyticData data;
  final int index;
  final Animation<double> animation;

  const PurchaseDailyTile({
    super.key,
    required this.data,
    required this.index,
    required this.animation,
  });

  @override
  Widget build(BuildContext context) {
    return FadeTransition(
      opacity: animation,
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
        decoration: BoxDecoration(
          color: AppColor.surface,
          borderRadius: BorderRadius.circular(14),
          border: Border.all(color: AppColor.border.withOpacity(0.25)),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.04),
              blurRadius: 10,
              offset: const Offset(0, 3),
            ),
          ],
        ),
        child: ExpansionTile(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(14),
          ),
          collapsedShape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(14),
          ),
          leading: Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: AppColor.primary.withOpacity(0.1),
              borderRadius: BorderRadius.circular(10),
            ),
            child: Icon(
              LineIcons.calendar,
              color: AppColor.primary,
              size: 20,
            ),
          ),
          title: Text(
            data.date.toDate,
            style: AppStyle.md.copyWith(
              fontWeight: FontWeight.w700,
              color: AppColor.textPrimary,
            ),
          ),
          subtitle: Text(
            data.purchases.currencyFormatRp,
            style: AppStyle.sm.copyWith(
              color: AppColor.primary,
              fontWeight: FontWeight.w600,
            ),
          ),
          trailing: Container(
            padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            decoration: BoxDecoration(
              color: AppColor.info.withOpacity(0.1),
              borderRadius: BorderRadius.circular(20),
            ),
            child: Text(
              '${data.purchaseOrders} PO',
              style: AppStyle.xs.copyWith(
                color: AppColor.info,
                fontWeight: FontWeight.w600,
              ),
            ),
          ),
          children: [
            Padding(
              padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
              child: Row(
                children: [
                  Expanded(
                    child: _DetailItem(
                      icon: LineIcons.boxes,
                      label: 'Qty',
                      value: '${data.quantity} pcs',
                      color: AppColor.warning,
                    ),
                  ),
                  Expanded(
                    child: _DetailItem(
                      icon: LineIcons.leaf,
                      label: 'Bahan',
                      value: '${data.ingredients} item',
                      color: AppColor.secondary,
                    ),
                  ),
                  Expanded(
                    child: _DetailItem(
                      icon: LineIcons.truck,
                      label: 'Vendor',
                      value: '${data.vendors}',
                      color: AppColor.primaryDark,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _DetailItem extends StatelessWidget {
  final IconData icon;
  final String label;
  final String value;
  final Color color;

  const _DetailItem({
    required this.icon,
    required this.label,
    required this.value,
    required this.color,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          padding: const EdgeInsets.all(8),
          decoration: BoxDecoration(
            color: color.withOpacity(0.1),
            borderRadius: BorderRadius.circular(8),
          ),
          child: Icon(icon, color: color, size: 18),
        ),
        const SizedBox(height: 6),
        Text(
          label,
          style: AppStyle.xs.copyWith(color: AppColor.textSecondary),
        ),
        const SizedBox(height: 2),
        Text(
          value,
          style: AppStyle.sm.copyWith(
            color: AppColor.textPrimary,
            fontWeight: FontWeight.w700,
          ),
        ),
      ],
    );
  }
}
