import 'package:flutter/material.dart';

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

class CostBreakdown extends StatelessWidget {
  final ProfitLossPurchasing purchasing;
  final int selectedTabIndex;
  final DateTime dateFrom;
  final DateTime dateTo;

  const CostBreakdown({
    super.key,
    required this.purchasing,
    required this.selectedTabIndex,
    required this.dateFrom,
    required this.dateTo,
  });

  @override
  Widget build(BuildContext context) {
    final isToday = selectedTabIndex == 0;
    final total = isToday ? purchasing.todayTotal : purchasing.mtdTotal;

    return Container(
      margin: const EdgeInsets.fromLTRB(16, 0, 16, 16),
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: AppColor.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: AppColor.textLight.withOpacity(0.08),
            spreadRadius: 1,
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Title row
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                context.lang.cost_breakdown,
                style: AppStyle.lg.copyWith(
                  fontWeight: FontWeight.w700,
                  color: AppColor.textPrimary,
                ),
              ),
              Text(
                _formatDateLabel(dateFrom, dateTo),
                style: AppStyle.sm.copyWith(
                  color: AppColor.textSecondary,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),

          const SizedBox(height: 16),

          // Item list
          ...purchasing.items.map((item) => _buildItemRow(item)),

          // Total row
          const Divider(height: 24, color: AppColor.borderLight),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                context.lang.total_cost,
                style: AppStyle.md.copyWith(
                  fontWeight: FontWeight.w700,
                  color: AppColor.textPrimary,
                ),
              ),
              Text(
                total.currencyFormatRp,
                style: AppStyle.md.copyWith(
                  fontWeight: FontWeight.w700,
                  color: AppColor.textPrimary,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildItemRow(ProfitLossPurchasingItem item) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10),
      child: Row(
        children: [
          Expanded(
            child: Text(
              item.item,
              style: AppStyle.md.copyWith(
                color: AppColor.textSecondary,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          Text(
            item.nominal.currencyFormatRp,
            style: AppStyle.md.copyWith(
              color: AppColor.textPrimary,
              fontWeight: FontWeight.w600,
            ),
          ),
        ],
      ),
    );
  }

  String _formatDateLabel(DateTime from, DateTime to) {
    const months = [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'Mei',
      'Jun',
      'Jul',
      'Agu',
      'Sep',
      'Okt',
      'Nov',
      'Des',
    ];

    if (from.year == to.year && from.month == to.month && from.day == to.day) {
      return '${from.day} ${months[from.month - 1]} ${from.year}';
    }
    return '${from.day} ${months[from.month - 1]} - ${to.day} ${months[to.month - 1]} ${to.year}';
  }
}
