import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/router/app_route.dart';
import 'package:niogu_app/core/enums/payment_status.dart';
import 'package:niogu_app/core/utils/time_zone.dart';
import 'package:niogu_app/features/report/transaction/presentation/providers/transaction_report_provider.dart';
import 'package:niogu_app/features/supplier/domain/entities/supplier.dart';
import 'package:sizer/sizer.dart';

class SupplierActivity extends ConsumerWidget {
  final SupplierActivities activity;
  const SupplierActivity({super.key, required this.activity});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final bool isTablet = 100.w >= 600;

    final status = activity.paymentStatus;

    final Color colorBadge = switch (status) {
      PaymentStatus.paid => Colors.green.shade50,
      PaymentStatus.partial => Colors.blue.shade50,
      PaymentStatus.debt => Colors.red.shade50,
    };

    final String paymentStatus = switch (status) {
      PaymentStatus.paid => "Lunas",
      PaymentStatus.partial => "Bayar Sebagian",
      PaymentStatus.debt => "Hutang",
    };

    final Color colorPaymentStatus = switch (status) {
      PaymentStatus.paid => Colors.green.shade700,
      PaymentStatus.partial => Colors.blue.shade700,
      PaymentStatus.debt => Colors.red.shade700,
    };

    return GestureDetector(
      onTap: () async {
        final purchaseDetail = await ref
            .read(transactionReportRepositoryProvider)
            .getPurchaseDetail(activity.id);

        FocusScope.of(context).unfocus();

        await Future.delayed(const Duration(milliseconds: 400));

        context.pushNamed(
          AppRoute.transactionReportPurchaseDetailScreen,
          extra: purchaseDetail,
        );
      },
      child: Container(
        margin: EdgeInsets.only(bottom: 1.5.h),
        padding: EdgeInsets.all(4.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(2.5.w),
          border: Border.all(color: Colors.grey[200]!),
        ),
        child: Row(
          children: [
            Container(
              padding: EdgeInsets.all(2.w),
              decoration: BoxDecoration(
                color: AppColor.primaryColor.withOpacity(0.1),
                shape: BoxShape.circle,
              ),
              child: Icon(
                Icons.shopping_bag_outlined,
                color: AppColor.primaryColor,
                size: 5.w,
              ),
            ),
            SizedBox(width: 4.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    activity.purchaseNumber,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1.25).sp
                          : (AppFontSize.small - 1.25).sp,
                    ),
                  ),
                  SizedBox(height: 0.75.h),
                  Container(
                    margin: EdgeInsets.only(top: 0.5.h),
                    padding: EdgeInsets.symmetric(
                      horizontal: 2.w,
                      vertical: 0.5.w,
                    ),
                    decoration: BoxDecoration(
                      color: colorBadge,
                      borderRadius: BorderRadius.circular(1.w),
                    ),
                    child: Text(
                      paymentStatus,
                      style: TextStyle(
                        fontSize: isTablet
                            ? (AppFontSize.medium - 1.25).sp
                            : (AppFontSize.small - 1.25).sp,
                        fontWeight: FontWeight.bold,
                        color: colorPaymentStatus,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Text(
              "${DateFormat("dd MMMM yyyy HH:mm").format(activity.purchaseDate)} ${TimeZone.getCurrentTimeZone()}",
              style: TextStyle(
                fontSize: isTablet
                    ? (AppFontSize.medium - 1.25).sp
                    : (AppFontSize.small - 1.25).sp,
                color: Colors.grey,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
