import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:intl/intl.dart';
import 'package:krow/core/application/common/str_extensions.dart';
import 'package:krow/core/application/routing/routes.gr.dart';
import 'package:krow/core/entity/event_entity.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/icon_row_info_widget.dart';
import 'package:krow/features/invoice/data/models/invoice_model.dart';
import 'package:krow/features/invoice/domain/invoice_entity.dart';

class InvoiceInfoCardWidget extends StatelessWidget {
  final InvoiceModel item;

  const InvoiceInfoCardWidget({
    required this.item,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: KwBoxDecorations.white12,
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 24),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          _buildStatusRow(),
          const Gap(24),
          Text('INV-${item.id}', style: AppTextStyles.headingH1),
          Gap(4),
          GestureDetector(
            onTap: () {
              if(item.event == null) return;
              context.router.push(EventDetailsRoute(event: EventEntity.fromEventDto(item.event!)));
            },
            child: Text(item.event!.name,
                style: AppTextStyles.bodyMediumReg
                    .copyWith(color: AppColors.blackGray,decoration: TextDecoration.underline)),
          ),
          const Gap(24),
          IconRowInfoWidget(
            icon: Assets.images.icons.calendar.svg(),
            title: 'Date',
            value: DateFormat('MM.dd.yyyy').format(DateTime.parse(item.event?.date ?? '')),
          ),
          const Gap(24),
          IconRowInfoWidget(
            icon: Assets.images.icons.calendar.svg(),
            title: 'Due Date',
            value: DateFormat('MM.dd.yyyy').format(DateTime.parse(item.dueAt ?? '')),
          ),
          if (item.event?.purchaseOrder != null) ...[
            const Gap(24),
            IconRowInfoWidget(
              icon: Assets.images.icons.calendar.svg(),
              title: 'PO Number',
              value: item.event!.purchaseOrder!,
            ),
          ],
        ],
      ),
    );
  }

  Row _buildStatusRow() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          height: 48,
          width: 48,
          decoration: const BoxDecoration(
            color: AppColors.tintGreen,
            shape: BoxShape.circle,
          ),
          child: Center(
            child: Assets.images.icons.moneySend.svg(
              colorFilter: const ColorFilter.mode(
                  AppColors.statusSuccess, BlendMode.srcIn),
            ),
          ),
        ),
        Container(
            height: 28,
            padding: const EdgeInsets.symmetric(horizontal: 8),
            decoration: BoxDecoration(
              color: item.status.color,
              borderRadius: BorderRadius.circular(14),
            ),
            child: Center(
              child: Padding(
                padding: const EdgeInsets.only(bottom: 2.0),
                child: Text(
                  item.status.name.capitalize(),
                  style: AppTextStyles.bodySmallMed
                      .copyWith(color: AppColors.grayWhite),
                ),
              ),
            ))
      ],
    );
  }
}

extension on InvoiceStatus {
  Color get color {
    switch (this) {
      case InvoiceStatus.open:
        return AppColors.primaryBlue;
      case InvoiceStatus.disputed:
        return AppColors.statusWarning;
      case InvoiceStatus.resolved:
        return AppColors.statusSuccess;
      case InvoiceStatus.paid:
        return AppColors.blackGray;
      case InvoiceStatus.overdue:
        return AppColors.statusError;
      case InvoiceStatus.verified:
        return AppColors.bgColorDark;
    }
  }
}
